C++ Primer练习笔记-第三章 字符串、向量和数组
程序员文章站
2022-06-22 16:46:12
第三章 字符串、向量和数组1. 第三章 字符串、向量和数组练习1.1. pratice 3.121.2. pratice 3.131.3. pratice 3.141.4. pratice 3.151.5. pratice 3.161.6. pratice 3.171.7. pratice 3.181.8. pratice 3.191.9. pratice 3.201.10. pratice 3.211.11. pratice 3.221.12. pratice 3.231.13. pratice 3.24...
前言
最近被拉到导师公司实习,闲来无事学习一下cpp。大学课程讲过基础的一点cpp语法,晃过几年,全还给老师了。这次拿《cpp primer》(第五版)慢慢补一下基础,为了防止遗忘,将一些有意义的课后练习做一些简单地记录,也供大家做一些参考,因为本人能力与精力有限,题解或有部分错误,恳请各位指正。
文章目录
- 前言
- 第三章 字符串、向量和数组练习
- pratice 3.12
- pratice 3.13
- pratice 3.14
- pratice 3.15
- pratice 3.16
- pratice 3.17
- pratice 3.18
- pratice 3.19
- pratice 3.20
- pratice 3.21
- pratice 3.22
- pratice 3.23
- pratice 3.24
- pratice 3.25
- pratice 3.26
- pratice 3.27
- pratice 3.28
- pratice 3.29
- pratice 3.30
- pratice 3.31
- pratice 3.32
- pratice 3.33
- pratice 3.34
- pratice 3.35
- pratice 3.36
- pratice 3.37
- pratice 3.38
- pratice 3.39
- pratice 3.40
- pratice 3.41
- pratice 3.42
- pratice 3.43
- pratice 3.44
- pratice 3.45
第三章 字符串、向量和数组练习
pratice 3.12
(a) 正确
(b) 错误,svec是string的vector, 但是ivec是int型的vector
© 正确
pratice 3.13
(a) 0
(b) 10 0
© 10 42
(d) 1 10
(e) 2 {10, 42}
(f) 10 “”*10
(g) 10 “hi”*10
pratice 3.14
#include<iostream>
#include<vector>
using namespace std;
int main(){
int num, temp;
vector<int> ivec;
cin >> num;
for(int i=0;i<num;i++){
cin >> temp;
ivec.push_back(temp);
}
cout << sizeof(ivec) << endl;
return 0;
}
pratice 3.15
#include<iostream>
#include<vector>
using namespace std;
int main(){
int num;
string temp;
vector<string> ivec;
cin >> num;
for(int i=0;i<num;i++){
cin >> temp;
ivec.push_back(temp);
}
cout << ivec.size() << endl;
return 0;
}
pratice 3.16
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> val(10,42);
for(auto x:val)
cout << x << endl;
return 0;
}
pratice 3.17
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
int val;
cin >> val;
string temp;
vector<string> s;
for(int i=0;i<val;i++){
cin >> temp;
for(auto &c:temp)
c = toupper(c);
s.push_back(temp);
}
for(auto word:s)
cout << word << endl;
return 0;
}
pratice 3.18
不合法,需要事先对向量进行初始化,修改为以下代码
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
int val;
cin >> val;
string temp;
vector<string> s;
for(int i=0;i<val;i++){
cin >> temp;
for(auto &c:temp)
c = toupper(c);
s.push_back(temp);
}
for(auto word:s)
cout << word << endl;
return 0;
}
pratice 3.19
vector<int> val(10,42);
vector<int> val(42,42,42,42,42,42,42,42,42,42);
vector<int> val2(val);
第一种方法定义最为简单
pratice 3.20
#include<iostream>
#include<vector>
using namespace std;
int main(){
int num, temp;
cin >> num;
vector<int> ivec;
for(int i=0;i<num;i++){
cin >> temp;
ivec.push_back(temp);
}
// 输出相邻整数的和
for(int i=1;i<num;i++){
cout << ivec[i-1] + ivec[i] << " ";
}
cout << endl;
// 输出头尾整数的和
for(int i=0;i<num/2;i++){
cout << ivec[i] + ivec[num-i-1] << " ";
}
cout << endl;
return 0;
}
pratice 3.21
#include<iostream>
#include<vector>
using namespace std;
int main(){
int num, temp;
vector<int> ivec;
cin >> num;
for(int i=0;i<num;i++){
cin >> temp;
ivec.push_back(temp);
}
for(auto it=ivec.begin(); it != ivec.end();it++){
cout << *it << " ";
}
return 0;
}
pratice 3.22
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
vector<string> text = {"madao", "","is","mp"};
for(auto it = text.begin(); it != text.end() && !it->empty();++it){
for(auto &ch : *it)
ch = toupper(ch);
}
for(auto it = text.begin();it != text.end();it++)
cout << *it << " ";
return 0;
}
pratice 3.23
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> ivec;
for(int i=0;i<10;i++)
ivec.push_back(i);
for(auto it = ivec.begin(); it != ivec.end(); it++){
*it = 2*(*it);
cout << *it << " ";
}
return 0;
}
pratice 3.24
#include<iostream>
#include<vector>
using namespace std;
int main(){
int num, temp;
cin >> num;
vector<int> ivec;
for(int i=0;i<num;i++){
cin >> temp;
ivec.push_back(temp);
}
// 输出相邻整数的和
for(auto it = ivec.begin();it!=ivec.end()-1;it++)
cout << *it + *(it+1) << " ";
cout << endl;
// 输出头尾整数的和
for(auto it = ivec.begin(), it2 = ivec.end()-1;it<it2;it++, it2--)
cout << *it + *it2 << " ";
cout << endl;
return 0;
}
pratice 3.25
#include<iostream>
#include<vector>
using namespace std;
int main(){
unsigned grade;
vector<unsigned> scores(11,0);
auto it = scores.begin();
while(cin >> grade) {
if (grade <= 100)
*(it+grade/10)++;
}
for(;it!=scores.end();it++)
cout << *it << " ";
return 0;
}
pratice 3.26
迭代器是相对位置,所以需要加上begin()
对应的起始位置
pratice 3.27
(d) 非法,因为其"fundamental"有11个字符,包含最后的结束符,需要至少12个字符大小的char数组
pratice 3.28
null
pratice 3.29
- 数组定义需要事先确定维度,不够灵活
- 数组的大小进过定义后,就不可修改
pratice 3.30
数组的指针从0开始
pratice 3.31
#include<iostream>
using namespace std;
int main(){
int num[10];
for(int i=0;i<10;i++)
num[i] = i;
for(int i=0;i<10;i++)
cout << num[i] << " ";
cout << endl;
return 0;
}
pratice 3.32
#include<iostream>
#include<vector>
using namespace std;
int main(){
// 数组实现
int num[10],num2[10];
for(int i=0;i<10;i++)
num[i] = i;
for(int i=0;i<10;i++){
num2[i] = num[i];
cout << num2[i] << " ";
}
cout << endl;
// vector实现
vector<int> ivec1;
for(int i=0;i<10;i++)
ivec1.push_back(i);
vector<int> ivec2(ivec1);
for (auto val:ivec2)
cout << val << " ";
cout << endl;
return 0;
}
pratice 3.33
如果不初始化scores
数组,其初始值不一定为0,得到的结果不正确
pratice 3.34
p1 = p1 + p2 - p1;
p1 = p2;
即将p2
指针赋给p1
指针
pratice 3.35
#include<iostream>
using namespace std;
int main(){
int a[10];
int *p = a;
for(int i=0;i<10;i++)
*(p+i) = 0;
for(int i=0;i<10;i++)
cout << *(p+1) << " ";
return 0;
}
pratice 3.36
#include<iostream>
#include<vector>
using namespace std;
int main(){
int a1[10]={10,9,8,7,6,5,4,3,2}, a2[10]={10,9,8,7,6,5,4,3,2,1};
// vector<int>a1,a2;
for(int i = 0; i < 10; i++){
if(a1[i] != a2[i]){
cout << "The two arraies are NOT the same!" << endl;
return -1;
}
}
cout << "The two arraies are the same!" << endl;
return 0;
}
pratice 3.37
#include<iostream>
using namespace std;
int main(){
const char ca[] = {'h', 'e', 'l', 'l', 'o'};
const char *cp = ca;
while(*cp){
cout << *cp << endl;
++cp;
}
return 0;
}
程序是输出hello
字符串,每个字符隔行输出
pratice 3.38
指针用来表示内存地址,指针的数值是该地址也就是0位地址的偏移量,也可以称为坐标。坐标相加得到的新值是没有什么意义的,坐标相减则是距离,坐标加距离则是新坐标,后两者是有意义的。
pratice 3.39
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main(){
string s1 = "hello", s2 = "hello";
const char cs1[] = "hello", cs2[] = "hello";
cout << "The two strings are " << ((s1 == s2)? "": "not ") << "the same!" << endl;
cout << "The two char strings are " << (strcmp(cs1, cs2)==0?"":"not ") << "the same!" << endl;
return 0;
}
pratice 3.40
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[] = "Life is like a box of chocolate, ";
char s2[] = "you never know what you gonna get.-<Forrest.Gump>.";
char s3[100]; // s3字符串保证空间足够存下s1和s2的所有内容
strcat(s1,s2);
strcpy(s3,s1);
cout << s3 << endl;
return 0;
}
pratice 3.41
#include<iostream>
#include<vector>
using namespace std;
int main(){
int a[] = {0, 1, 2, 3, 4, 5};
vector<int> ivec(begin(a), end(a));
for(auto i:ivec)
cout << i << endl;
return 0;
}
pratice 3.42
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> ivec{0,1,2,3,4,5};
int a[10], count = 0;
for(auto i:ivec){
a[count++] = i;
}
for(int i=0;i<count;i++)
cout << a[i] << endl;
return 0;
}
pratice 3.43
#include<iostream>
using namespace std;
int main(){
int a[2][4] = {0,1,2,3,4,5,6,7};
// 版本一,范围for语句处理多维数组
cout << "version 1:" << endl;
for(int (&row)[4]:a){
for(int col:row){
cout << col << " ";
}
cout << endl;
}
// 版本二,下标运算符
cout << "version 2:" << endl;
for(int i=0; i<2;i++){
for(int j=0; j<4;j++){
cout << a[i][j];
}
cout << endl;
}
// 版本三,使用指针遍历
cout << "version 3:" << endl;
for(int (*p)[4] = begin(a); p!=end(a);p++){
for(int *q = begin(*p);q != end(*p);++q)
cout << *q << " ";
cout << endl;
}
}
pratice 3.44
#include<iostream>
using namespace std;
using int_array = int[4];
typedef int int_array[4];
int main(){
int a[2][4] = {0,1,2,3,4,5,6,7};
for(int_array *p = a; p!=a+2;p++){
for(int *q = *p; q!=*p+4;++q)
cout << *q << ' ';
cout << endl;
}
return 0;
}
pratice 3.45
#include<iostream>
using namespace std;
int main(){
int a[2][4]={0,1,2,3,4,5,6,7};
for(auto &p:a){
for(auto q:p)
cout << q << ' ';
cout << endl;
}
return 0;
}
本文地址:https://blog.csdn.net/qq_36178962/article/details/107318129
上一篇: 未来生活的智能机器人
下一篇: PAT A1136 回文数