C++ Primer 第五版 第三章 字符串、向量和数组
3.3
- string对象会自动忽略开头的空白(即空格符、换行符、制表符等)并从第一个真正的字符开始读起,直到遇见下一处空白字符为止;
- getline不会忽略开头的空白,它将输入流中的内容读入到string对象中,直到遇到换行符为止,换行符也被读进来了,但是不会保存到string对象;
3.4
#include <iostream>
2 #include <string>
3 using namespace std;
4
5 int main()
6 {
7 string a1, a2;
8 cin >> a1 >> a2;
9 //比较是否相等
10 if (a1 != a2)
11 {
12
13 cout << ((a1 > a2) ? a1: a2) << endl;
14 }
15 if (a1.size() != a2.size())
16 {
17 cout << (a1.size() > a2.size() ? a1 : a2) << endl;;
18 }
19 return 0;
20 }
3.5
1 #include <iostream>
2 #include <string>
3 #include <limits>
4 #include <sstream>
5 using namespace std;
6
7 int main()
8 {
9 string st, sum;
10
11 while (cin >> st)
12 sum += st;
13 cout << endl;
14 cout << sum << endl;
15
16 //用空格把输入的多个字符串分割开来
17
18 cin.ignore(std::numeric_limits<streamsize>::max());//清理输入流中所有的数据
19 cin.clear();//清除上一个文件结束符输入
20 string sum2;
21
22 while (cin >> st)
23 {
24 sum2 += st;
25 sum2 += ' ';
26 }
27 cout << endl;
28 sum5.pop_back();//去除最后一个多余的空格符;
29 cout << sum2 << endl;
30
31 return 0;
32 }
3.6
#include <iostream>
2 #include <cctype>
3 #include <string>
4 using namespace std;
5
6 int main()
7 {
8 string s("hello world");
9
10 for (decltype(s.size()) i = 0; i < s.size(); ++i)
11 s[i] = 'X';
12 cout << s << endl;
13 return 0;
14 }
3.7
结果无变化
1 #include <iostream>
2 #include <cctype>
3 #include <string>
4 using namespace std;
5
6 int main()
7 {
8 string s("hello world");
9
10 cout << s << endl;
11 for (char i = 0; i < s.size(); ++i)
12 {
13 cout << i;
14 s[i] = 'X';
15 }
16 cout << s << endl;
17 return 0;
18 }
3.8
在for循环中,循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。两种形式各有优点,但它们在功能上是等价的,可以相互转换。
#include <iostream>
2 #include <cctype>
3 #include <string>
4 using namespace std;
5
6 int main()
7 {
8 string s("hello world");
9
10 for (decltype(s.size()) i = 0; i < s.size(); ++i)
11 s[i] = 'X';
12 cout << s << endl;
13
14 string s1("hello world");
15
16 decltype(s1.size()) i = 0;
17 while (i < s.size())
18 {
19 s[i] = 'X';
20 ++i;
21 }
22 cout << s << endl;
23 return 0;
24 }
3.9
发挥字符串s的第一个字符,但是由于string默认初始化为一个空的string,使用下标访问空string也会引发不可预知的结果,所以是非法的。
3.10
#include <iostream>
2 #include <cctype>
3 #include <string>
4
5 using namespace std;
6
7 int main()
8 {
9 string s;
10 cin >> s;
11
12 for (auto &c : s)
13 if (ispunct(c))
14 c = '\0';//将标点符号处的字符都置为空字符
15 cout << s << endl;
16 return 0;
17 }
3.11
合法,c是string类型;auto一般会忽略掉顶层const,如果希望推断出的auto类型是一个顶层const,需要明确指出:const auto &c : s.
3.12
- 正确,定义了一个元素对象是vector的vector;
- 错误,两者类型不一致;
- 正确,初始化svec有10个“null”元素对象;
3.14
#include <iostream>
2 #include <string>
3 #include <vector>
4
5 using namespace std;
6
7
8 int main()
9 {
10 vector<int> vc;
11 int it;
12 while (cin >> it)
13 vc.push_back(it);
14 cout << endl;
15 //打印出所有元素
16 vector<int>::iterator begin = vc.begin(), end = vc.end();
17 while (begin != end)
18 cout << *begin++ << ' ';
19 cout << endl;
20 return 0;
21 }
3.17
#include <iostream>
2 #include <cctype>
3 #include <string>
4 #include <vector>
5
6 using namespace std;
7
8 int main()
9 {
10 vector<string> vs;
11 string st;
12 while (cin >> st)
13 {
14 for (auto &c : st)
15 if (isalpha(c))
16 c = toupper(c);
17 vs.push_back(st);
18 }
19 cout << endl;
20 for (auto &c : vs)
21 cout << c << endl;;
22
23 return 0;
24 }
3.18
不合法,vector ivec; ivec.push_back(42);
3.19
- vector vc(10,42);
- vector vc{42,42,42,42,42,42,42,42,42,42};
- vector vc; for(int i =0; i <10; i++) vc.push_back(42);
3.20
#include <iostream>
2 #include <string>
3 #include <vector>
4 using namespace std;
5
6 int main()
7 {
8 vector<int> v1;
9 int e;
10 while (cin >> e)
11 v1.push_back(e);
12 cout << endl;
13 for (auto c : v1)
14 cout << c << ' ';
15 cout << endl;
16 //输出相邻整数的和
17 for (decltype(v1.size()) i = 0; i < v1.size() - 1; ++i)
18 cout << v1[i] + v1[i+1] << ' ';
19 cout << endl;
20 //输出首位整数的和
21 for (decltype(v1.size()) i = 0; i < v1.size() /2; ++i)
22 cout << v1[i] + v1[v1.size() - 1 - i] << ' ';
23 cout << endl;
24
25 return 0;
26 }
3.22
1 #include <iostream>
2 #include <string>
3 #include <cctype>
4 using namespace std;
5
6 int main()
7 {
8 for (auto it = text.begin(); it != text.end() && !it->empty(); ++it)
9 {
10 *it = toupper(*it);
11 cout << *it ;
12 }
13 return 0;
14 }
3.23
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 int main()
6 {
7 vector<int> it(10,1);
8 vector<int>::iterator b = it.begin();
9 for (; b != it.end(); ++b )
10 {
11 *b = *b * 2;
12 cout << *b << ' ';
13 }
14 cout << endl;
15 return 0;
16 }
~
3.25
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 int main()
6 {
7 vector<int> scores(11, 0);
8 unsigned grade;
9
10 while (cin >> grade)
11 {
12 if (grade <= 100)
13 ++(*(scores.begin() + grade / 10));
14 }
15 return 0;
16 }
~
3.26 vector和string迭代器都不支持+运算
3.27
原则:
- 维度必须保证大于0,所以c错误,a是正确的;
- 维度必须是一个常量表达式,b是正确的;
3.字符串字面值的结尾处还有一个空字符,d没有空间可存放空字符,d是错误的;
3.28
-如果是内置类型的变量未被显式初始化,它的值由定义的位置决定。定义于任何函数体之外的变量被初始化为0,而定义在函数体内部的内置类型将不被初始化,是未定义的。所以sa是空字符,ia是0,sa2和ia2是未定义的。
3.29
数组是静态的,维度是固定的,而vector可以看成是动态数组,大小可以变化
3.30
越界访问;
3.33
如果不初始化定义在函数内部的内置类型,那么此对象是未定义的。
3.34
p1 = p2
3.36
1 #include <iostream>
2 #include <vector>
3 #include <iterator>
4 using namespace std;
5
6 int main()
7 {
8 //两数组比较大小
9 int it1[4] = {1, 2, 3, 4};
10 int it2[5] = {1, 2, 4, 3};
11 auto beg1 = begin(it1);
12 auto beg2 = begin(it2);
13 auto last1 = end(it1);
14 auto last2 = end(it2);
15 bool flag = 1;
16 if ((last1 - beg1) == (last2 - beg2))
17 {
18 for(; beg1 != last1 && beg2 != last2; ++beg1, ++beg2)
19 {
20 if (*beg1 != *beg2)
21 {
22 flag = 0;
23 break;
24 }
25 }
26 }
27 else
28 {
29 flag = 0;
30 }
31 if (flag == 1)
32 {
33 cout << "it1 equal it2" << endl;
34 }
35 else
36 cout << "it1 not equal it2!!" << endl;
37
38 //两vectro对象比较大小
39 vector<int> vc1(10, 2);
40 vector<int> vc2(9, 2);
41 vector<int> vc3(10, 1);
42
43 if (vc1 == vc2)
44 cout << "vc1 equal vc2" << endl;
45 else
46 cout << "vc1 not equal vc2" << endl;
47 if (vc1 == vc2)
48 cout << "vc1 equal vc3" << endl;
49 else
50 cout << "vc1 not equal vc3" << endl;
51
52
53 }
3.37
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 const char ca[] = {'h', 'e', '1', '1', '0'};
7 const char *cp = ca;
8 while (*cp)
9 {
10 cout << *cp << endl;
11 ++cp;
12 }
13 }
输出结果:
3.38
指针存放的是对象的地址信息,相加不仅可能会超出元素存储的范围,而且对访问对象无作用。
3.39
1 #include <iostream>
2 #include <string>
3 #include <iterator>
4 #include <cstring>
5 using namespace std;
6
7 int main()
8 {
9 const char ch0[] = "zhang";
10 const char ch1[] = "zheng";
11
12 //比较两个C风格字符串;
13 if (!strcmp(ch0, ch1))
14 cout << "ch0 equal ch1" << endl;
15 else
16 cout << "ch0 not equal ch1" << endl;
17
18 //比较两个string对象
19 string st0 = "zhang";
20 string st1 = "zhang";
21
22 if (st0 == st1)
23 cout << "ch0 equal st1" << endl;
24 else
25 cout << "ch0 not equal st1" << endl;
26
27
28
29 return 0;
30 }
3.40
1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4
5 int main()
6 {
7 char ch0[] = "Hello, ";
8 char ch1[] = "world";
9 char ch2[] = {};
10
11 strcat(ch0, ch1);
12 cout << ch0 << endl;
13 strcpy(ch2, ch0);
14 cout << ch2 << endl;
15
16 return 0;
17 }
3.41-3.42
1 #include <iostream>
2 #include <vector>
3 #include <iterator>
4 using namespace std;
5
6 int main()
7 {
8 int it[] = {1, 2, 3, 4, 5};
9 //用整型数组初始化一个vector对象
10 vector<int> vc(begin(it), end(it));
11 for (auto c:vc)
12 cout << c << endl;
13 //将整型vector对象拷贝给整型数组
14 int it1[vc.size()];
15 auto b = vc.begin();
16 for (int i = 0; i < vc.size(); i++)
17 it1[i] = *b++;
18
19 for (auto c:it1)
20 cout << c << endl;
21
22 return 0;
23 }
3.43
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int it[2][3] = {1, 2, 3, 4, 5, 6};
7 //版本1
8 for (auto &c:it)
9 for (auto a:c)
10 cout << a << ' ';
11 cout << endl;
12 //版本2
13 for (int r = 0; r < 2; r++)
14 for (int c = 0; c < 3; c++)
15 cout << it[r][c] << ' ';
16 cout << endl;
17 //版本3
18 for (int (*p)[3] = it; p < it + 2; p++)
19 for (int *q = *p; q < *p + 3; q++)
20 cout << *q << ' ';
21 cout << endl;
22 return 0;
23 }
3.44
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 typedef int T;
7 using T_array = T[3];
8
9 T it[2][3] = {1, 2, 3, 4, 5, 6};
10 //版本1
11 for (auto &c:it)
12 for (auto a:c)
13 cout << a << ' ';
14 cout << endl;
15 //版本2
16 for (T r = 0; r < 2; r++)
17 for (T c = 0; c < 3; c++)
18 cout << it[r][c] << ' ';
19 cout << endl;
20 //版本3
21 for (T_array *p = it; p < it + 2; p++)
22 for (T *q = *p; q < *p + 3; q++)
23 cout << *q << ' ';
24 cout << endl;
25 return 0;
26 }
~
3.45
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int it[2][3] = {1, 2, 3, 4, 5, 6};
7 //版本1
8 for (auto &c:it)
9 for (auto a:c)
10 cout << a << ' ';
11 cout << endl;
12 //版本2
13 for (int r = 0; r < 2; r++)
14 for (int c = 0; c < 3; c++)
15 cout << it[r][c] << ' ';
16 cout << endl;
17 //版本3
18 for (auto p = it; p < it + 2; p++)
19 for (auto q = *p; q < *p + 3; q++)
20 cout << *q << ' ';
21 cout << endl;
22 return 0;
23 }