第三章 字符串、向量和数组

牧云@^-^@ 提交于 2020-03-07 11:04:58

3.2

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cin;
 5 using std::cout;    
 6 using std::endl;
 7 using std::string;
 8 
 9 int main() 
10 {
11     string line;
12     while (getline(cin, line))
13         cout << line << endl;
14     return 0;
15 }
一次读入一行
 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cin;
 5 using std::cout;    
 6 using std::endl;
 7 using std::string;
 8 
 9 int main() 
10 {
11     string word;
12     while (cin >> word)
13         cout << word << endl;
14     return 0;
15 }
一次读入一个单词

 

3.3

输入运算符:自动忽略string类对象开头的空白(即空格符、换行符、制表符等),并从第一个真正的字符开始读起,直到遇见下一处空白为止。

getline函数:从给定的输入流中读入内容,直到遇到换行符为止(换行符也被读进来了),然后把所读的内容存入到那个 string 对象中去(不存换行符)。

 

3.4

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cin;
 5 using std::cout;    
 6 using std::endl;
 7 using std::string;
 8 
 9 void is_equal(string &s1, string &s2)
10 {
11     if (s1 == s2) {
12         cout << s1 << " is equal to " << s2 << endl;
13     }
14     else {
15         if (s1 < s2)    cout << s1 << " is smaller than " << s2 << endl; 
16         else    cout << s1 << " is bigger than " << s2 << endl;
17     }
18 }
19 
20 void is_same_length(string &s1, string &s2)
21 {
22     if (s1.size() == s2.size()){
23         cout << s1 << "'s length is equal to " << s2 << endl ;
24     }
25     else {
26         if (s1.size() < s2.size())    cout << s1 << "'s length is shorter than " << s2 << endl;
27         else    cout << s1 << "'s length is longer than " << s2 << endl;
28     }
29 } 
30 
31 int main() 
32 {
33     string s1, s2;
34     cin >> s1 >> s2;
35     is_equal(s1, s2);
36     is_same_length(s1, s2); 
37     return 0;
38 }
View Code

 

3.5

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cin;
 5 using std::cout;    
 6 using std::endl;
 7 using std::string;
 8 
 9 void solve1()
10 {
11     string s1, s2 = "";
12     while (getline(cin, s1)) {
13         s2 += s1;
14         cout << s2 << endl; 
15     }
16 }
17 
18 void solve2()
19 {
20     string s1, s2 = "";
21     while (getline(cin, s1)) {
22         s2 = s2 + s1 + " ";
23         cout << s2 << endl;
24     }
25     
26 }
27 
28 int main() 
29 {
30     solve1();
31     solve2();
32     return 0;
33 }
View Code

 

3.6

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cin;
 5 using std::cout;    
 6 using std::endl;
 7 using std::string;
 8 
 9 int main() 
10 {
11     string s("Hello World!!!");
12     for (auto &i : s)
13         i = 'X';
14     cout << s << endl;
15     return 0;
16 }
View Code

 

3.10

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cin;
 5 using std::cout;    
 6 using std::endl;
 7 using std::string;
 8 
 9 int main() 
10 {
11     string s("Hello World!!!");
12     for (auto i : s)
13         if(!ispunct(i))    cout << i;
14     cout << endl;
15     return 0;
16 }
View Code

 

3.12

(a)正确,会创建一个元素类型为vector的vector对象;(c)正确,会创建一个元素类型为string的vector对象svec,sevc有10个元素,每个元素的初始值都是"null";

(b)错误,ivec的元素类型与svec不同,故不能执行拷贝初始化。

 

3.14

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 using std::cin;
 6 using std::cout;    
 7 using std::endl;
 8 using std::vector;
 9 using std::string;
10 
11 int main() 
12 {
13     vector<int> ivec;
14     int i;
15     while (cin >> i)    ivec.push_back(i);
16     return 0;
17 }
View Code

 

3.15

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 using std::cin;
 6 using std::cout;    
 7 using std::endl;
 8 using std::vector;
 9 using std::string;
10 
11 int main() 
12 {
13     vector<string> ivec;
14     string s;
15     while (cin >> s)    ivec.push_back(s);
16     return 0;
17 }
View Code

 

3.16

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 using std::cin;
 6 using std::cout;    
 7 using std::endl;
 8 using std::vector;
 9 using std::string;
10 
11 int main() 
12 {
13     vector<int> v1, v2(10), v3(10,42), v4{10}, v5{10, 42};
14     vector<string> v6{10}, v7{10, "hi"};
15     if (v1.empty())    cout << "v1 is empty!";
16     else    for (auto i : v1)    cout << i << " ";    cout << endl;
17     for (auto i : v2)    cout << i << " ";    cout << endl;
18     for (auto i : v3)    cout << i << " ";    cout << endl;
19     for (auto i : v4)    cout << i << " ";    cout << endl;
20     for (auto i : v5)    cout << i << " ";    cout << endl;
21     for (auto i : v6)    cout << i << " ";    cout << endl;
22     for (auto i : v7)    cout << i << " ";    cout << endl;
23     return 0;
24 }
View Code

 

3.17

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <string>
 5 
 6 using std::cin;
 7 using std::cout;    
 8 using std::endl;
 9 using std::vector;
10 using std::string;
11 
12 int main() 
13 {
14     vector<string> svec;
15     string word;
16     while (cin >> word)
17         svec.push_back(word);
18     for (decltype(svec.size()) i = 0; i != svec.size(); i++)
19         for (int j = 0; j != svec[i].size(); j++)
20             if (islower(svec[i][j]))    
21                 svec[i][j] = toupper(svec[i][j]);
22     for (decltype(svec.size()) i = 0; i != svec.size(); i++)
23         cout << svec[i] << endl;
24     return 0;
25 }
View Code

 

3.19

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <string>
 5 
 6 using std::cin;
 7 using std::cout;    
 8 using std::endl;
 9 using std::vector;
10 using std::string;
11 
12 int main() 
13 {
14     vector<int> v1 = {42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
15     vector<int> v2(10, 42);
16     vector<int> v3;
17     for (decltype(v3.size()) i = 0; i < 10; i++)    v3.push_back(42);
18     return 0;
19 }
View Code

 

3.20

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <string>
 5 
 6 using std::cin;
 7 using std::cout;    
 8 using std::endl;
 9 using std::vector;
10 using std::string;
11 
12 vector<int> ivec;
13 
14 void solve1()
15 {
16     int flag = ivec.size() % 2;
17     if (flag == 0) {
18         for (decltype(ivec.size()) i = 0; i != ivec.size(); i = i + 2) {
19             cout << ivec[i] + ivec[i+1] << endl;
20         }
21     }
22     else {
23         for (decltype(ivec.size()) i = 0; i != ivec.size() -1; i = i + 2) {
24             cout << ivec[i] + ivec[i+1] << endl;
25         }
26         cout << ivec[ivec.size()-1] << endl;
27     }
28 }
29 
30 void solve2()
31 {
32     int flag = ivec.size() % 2;
33     if (flag == 0) {
34         for (decltype(ivec.size()) i = 0; i != ivec.size() / 2; i++) {
35             cout << ivec[i] + ivec[ivec.size() - 1 - i] << endl;
36         }
37     }
38     else {
39         for (decltype(ivec.size()) i = 0; i != ivec.size() / 2; i++) {
40             cout << ivec[i] + ivec[ivec.size() - 1 - i] << endl;
41         }
42         cout << ivec[ivec.size() / 2] << endl;
43     }
44 }
45 
46 int main() 
47 {
48     int i;
49     while (cin >> i)     ivec.push_back(i);
50     cout << "第一种情况的输出为:\n";
51     solve1();
52     cout << "第二种情况的输出为:\n";
53     solve2();
54     return 0;
55 }
View Code

 

3.21

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 using std::cin;
 6 using std::cout;    
 7 using std::endl;
 8 using std::vector;
 9 using std::string;
10 
11 int main() 
12 {
13     vector<int> v1, v2(10), v3(10,42), v4{10}, v5{10, 42};
14     vector<string> v6{10}, v7{10, "hi"};
15     if (v1.empty())    cout << "v1 is empty!";
16     else    for (auto it = v1.cbegin(); it != v1.cend(); it++)    cout << *it << " ";    cout << endl;
17     for (auto it = v2.cbegin(); it != v2.cend(); it++)    cout << *it << " ";    cout << endl;
18     for (auto it = v3.cbegin(); it != v3.cend(); it++)    cout << *it << " ";    cout << endl;
19     for (auto it = v4.cbegin(); it != v4.cend(); it++)    cout << *it << " ";    cout << endl;
20     for (auto it = v5.cbegin(); it != v5.cend(); it++)    cout << *it << " ";    cout << endl;
21     for (auto it = v6.cbegin(); it != v6.cend(); it++)    cout << *it << " ";    cout << endl;
22     for (auto it = v7.cbegin(); it != v7.cend(); it++)    cout << *it << " ";    cout << endl;
23     return 0;
24 }
View Code

 

3.22

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 using std::cin;
 6 using std::cout;    
 7 using std::endl;
 8 using std::vector;
 9 using std::string;
10 
11 int main() 
12 {
13     vector<string> text;
14     string s;
15     while (getline(cin, s)) {
16         text.push_back(s);
17     }
18     for (auto it = text.begin(); it != text.end() && !it->empty(); it++) {
19         for (int i = 0; i < (*it).size(); i++)    
20             (*it)[i] = toupper((*it)[i]);
21     }
22     for (auto it = text.begin(); it != text.end() && !it->empty(); it++) {
23         cout << *it << endl;
24     }
25     return 0;
26 }
View Code

 

3.23

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 using std::cin;
 6 using std::cout;    
 7 using std::endl;
 8 using std::vector;
 9 using std::string;
10 
11 int main() 
12 {
13     vector<int> ivec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
14     for (auto it = ivec.begin(); it != ivec.end(); it++)
15         (*it) *= 2;
16     for (auto it = ivec.begin(); it != ivec.end(); it++)
17         cout << *it << endl;
18     return 0;
19 }
View Code

 

3.24

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <string>
 5 
 6 using std::cin;
 7 using std::cout;    
 8 using std::endl;
 9 using std::vector;
10 using std::string;
11 
12 vector<int> ivec;
13 
14 void solve1()
15 {
16     int flag = ivec.size() % 2;
17     if (flag == 0) {
18         for (auto i = ivec.begin(); i != ivec.end(); i = i + 2) {
19             cout << *i + *(i+1) << endl;
20         }
21     }
22     else {
23         for (auto i = ivec.begin(); i != ivec.end() - 1; i = i + 2) {
24             cout << *i + *(i+1) << endl;
25         }
26         cout << *(ivec.end()-1) << endl;
27     }
28 }
29 
30 void solve2()
31 {
32     int flag = ivec.size() % 2;
33     if (flag == 0) {
34         for (auto i = ivec.begin(); i != ivec.begin() + (ivec.end() - ivec.begin()) / 2; i++) {
35             cout << *i + *(ivec.begin() + (ivec.end() - 1 - i)) << endl;
36         }
37     }
38     else {
39         for (auto i = ivec.begin(); i != ivec.begin() + (ivec.end() - ivec.begin()) / 2; i++) {
40             cout << *i + *(ivec.begin() + (ivec.end() - 1 - i)) << endl;
41         }
42         cout << *(ivec.begin() + (ivec.end() - ivec.begin()) / 2) << endl;
43     }
44 }
45 
46 int main() 
47 {
48     int i;
49     while (cin >> i)     ivec.push_back(i);
50     cout << "第一种情况的输出为:\n";
51     solve1();
52     cout << "第二种情况的输出为:\n";
53     solve2();
54     return 0;
55 }
View Code

 

3.25

#include <iostream>
#include <vector>
#include <cctype>
#include <string>

using std::cin;
using std::cout;    
using std::endl;
using std::vector;
using std::string;

int main() 
{
    vector<int> ivec(11, 0);
    int i;
    while (cin >> i){
        auto it = ivec.begin();
        it = it + i/10;
        (*it)++;
    }
//    for (auto i : ivec)
//        cout << i << endl;    
    return 0;
}
View Code

 

3.26

对于“mid = (beg + end) / 2;”,它的错误是:迭代器之间不支持加法运算。

补充:之所以使用“mid = beg + (end - beg) / 2;”而非“mid = (end - beg) / 2;”,是因为mid是迭代器,而end - beg得到的是一个int型数(距离),只有再在前面加上一个迭代器beg才能使左右类型平衡!

 

3.27

(a)(c)(d)非法。因为buf_size是个int型变量(不是常量表达式),所以(a)非法;因为txt_size()的返回值为int(不是constexpr),所以(c)非法;对于(d),没有空间存放空字符'\0'。

 

3.33

不初始化为0,各个分数段的计数值将可能不准确。

 

3.34

功能:使p1指向p2所指的元素。

 

3.35

不知道为什么我的编译器没有包含begin()和end()函数!!我就用另一种方法做了。

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 
 7 using std::cin;
 8 using std::cout;    
 9 using std::endl;
10 using std::vector;
11 using std::string;
12 
13 int main() 
14 {
15     int a[] = {1,2,3};
16 /*    
17     int *p = begin(a);
18     int *pend = end(a);
19     while (p != pend) {
20         *p = 0;
21         p++;
22     }
23 */
24     int *p = a;
25     for (int i = 0; i < 3; i++){
26         *(p+i) = 0;
27     }
28     return 0;
29 }
View Code

 

3.36

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 
 7 using std::cin;
 8 using std::cout;    
 9 using std::endl;
10 using std::vector;
11 using std::string;
12 
13 int main() 
14 {
15     int flag = 0;
16     int a[] = {1, 2, 4};
17     int b[] = {1, 2, 3};
18     int numa = sizeof(a) / sizeof(a[0]), numb = sizeof(b) / sizeof(b[0]);
19     int *pa = a, *pb = b;
20     if (numa == numb) {
21         for(int i = 0; i < numa; i++) {
22             if (*(pa+i) != *(pb+i)){
23                 flag = 2;
24                 break;
25             }
26         }
27         if (flag == 0)    flag = 1;
28     }
29     if (flag == 1)    cout << "Equal!\n";
30     else    cout << "Not equal!\n";
31     return 0;
32 }
View Code
 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 
 7 using std::cin;
 8 using std::cout;    
 9 using std::endl;
10 using std::vector;
11 using std::string;
12 
13 int main() 
14 {
15     int flag = 0;
16     vector<int> a = {1, 2, 4};
17     vector<int> b = {1, 2, 3};
18     auto ita = a.begin();
19     vector<int>::iterator itb = b.begin();
20     if (a.size() == b.size()) {
21         for (; ita != a.end(); ita++, itb++) {
22             if (*ita != *itb) {
23                 flag = 2;
24                 break;
25             }
26         }
27         if (flag == 0)    flag = 1;
28     }
29     if (flag == 1)    cout << "Equal!\n";
30     else    cout << "Not equal!\n";
31     return 0;
32 }
View Code

 

3.37

首先一定会打印出:h[\n]e[\n]l[\n]l[\n]o[\n],然后会打印在这些地址后的内存中的值,直到遇到'\0'。

 

3.38

指针是个地址,指向某个内存单元。如果两个地址相加,那将毫无意义。

 

3.39

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using std::cin;
 9 using std::cout;    
10 using std::endl;
11 using std::vector;
12 using std::string;
13 
14 void solve1()
15 {
16     char str1[] = {'H', 'e', 'l', 'l', 'o', '\0'};
17     char str2[] = {'H', 'a', 'p', 'p', 'y', '\0'};
18     int flag = strcmp(str1, str2);
19     if (flag == 0)    cout << "Equal.\n";
20     else {
21         if (flag > 0)    cout << "str1 is bigger than str2.\n";
22         else    cout << "str1 is smaller than str2.\n";
23     }
24 }
25 
26 void solve2()
27 {
28     string s1 = "Hello", s2 = "Happy";
29     if (s1 == s2)    cout << "Equal.\n";
30     else {
31         if (s1 > s2)    cout << "str1 is bigger than str2.\n";
32         else    cout << "str1 is smaller than str2.\n";
33     }
34 }
35 
36 int main() 
37 {
38     solve1();
39     solve2();
40     return 0;
41 }
View Code

 

3.40

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using std::cin;
 9 using std::cout;    
10 using std::endl;
11 using std::vector;
12 using std::string;
13 
14 int main() 
15 {
16     char str1[20] = "Hello";
17     char str2[20] = "World";
18     char str3[20];
19     strcat(str1, " ");
20     strcat(str1, str2);
21     strcat(str1, "!");
22     strcpy(str3, str1);
23     char *p = str3;
24     while (*p != '\0') {
25         cout << *p;
26         p++;
27     }
28     cout << endl;
29     return 0;
30 }
View Code

 

3.41

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using std::cin;
 9 using std::cout;    
10 using std::endl;
11 using std::vector;
12 using std::string;
13 
14 int main() 
15 {
16     int a[3] = {1, 2, 3};
17 //    vector<int> ivec(begin(a), end(a));
18     vector<int> ivec(a, a+2);
19     return 0;
20 }
View Code

 

3.42

 1 #include <iostream>
 2 #include <vector>
 3 #include <cctype>
 4 #include <iterator>
 5 #include <string>
 6 #include <cstring>
 7 
 8 using std::cin;
 9 using std::cout;    
10 using std::endl;
11 using std::vector;
12 using std::string;
13 
14 int main() 
15 {
16     int a[3], k = 0;
17     vector<int> ivec(3, 1);
18     for (auto it = ivec.begin(); it != ivec.end(); it++) {
19         a[k++] = *it;
20     }
21     return 0;
22 }
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!