C++ Primer-第三章练习题

3.31

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     constexpr int cnt = 10;
 8     int ia[cnt];
 9     for (string::size_type i = 0; i < cnt; ++i) {
10         ia[i] = i;
11     }
12 
13     for (auto c : ia) {
14         cout << c << endl;
15     }
16     system("pause");
17     return 0;
18 
19 }
View Code

3.32

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main() 
 5 {
 6     constexpr int cnt = 10;
 7     int ia[cnt];
 8     int ia2[cnt];
 9     for (string::size_type i = 0; i < cnt; ++i) {
10         ia[i] = i;
11     }
12     for (string::size_type i = 0; i < cnt; ++i) {
13         ia2[i] = ia[i];
14     }
15     for (auto c: ia2) {
16         cout << c << " ";
17     }
18     cout << endl;
19     system("pause");
20     return 0;
21 }
View Code

用vector实现:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 int main() 
 6 {
 7     vector<int> text(10,0);
 8     for (string::size_type i = 0; i < text.size(); ++i) {
 9         text[i] = i;
10     }
11     for (auto c : text) {
12         cout << c << " ";
13     }
14     cout << endl;
15 
16     vector<int> text1(text);
17     for (auto c : text1) {
18         cout << c << " ";
19     }
20     cout << endl;
21     system("pause");
22     return 0;
23 }
View Code

小结:vector可以拷贝,数组不能拷贝,只能遍历。

3.33

不初始化的话,其值不确定,可能有数据,可能没有数据。

P106

使用数组指针:

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main()
 6 {
 7     int ia[] = { 0,1,2,3,4,5,6,7,8,9 };
 8     //int *b = &ia[sizeof(ia) + 1];
 9     cout << size(ia) << endl;
10     system("pause");
11     return 0;
12 
13 }
View Code

找到第一个负数:

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {    
 5     int ia[] = { 1,2,3,4,5,6,7,9,10,-10,1,2,3,4,5,6};
 6     int *pbeg = begin(ia), *pend = end(ia);
 7     while (pbeg != pend && *pbeg >= 0) {
 8         ++pbeg;
 9     }
10     cout << *pbeg << endl; 
11 
12     system("pause");
13     return 0;
14 }
View Code

 P107

#include <iostream>
using namespace std;
int main()
{
    constexpr size_t sz = 5;
    int arr[sz] = { 1,2,3,4,5 };
    int *ip = arr;
    int *ip2 = ip + 4;//初始化ip+4是地址,ip2是 int* 类型
    cout << *ip2 << endl;
    system("pause");
    return 0;
}

遍历:

#include <iostream>
using namespace std;
int main()
{
    constexpr size_t sz = 5;
    int arr[sz] = { 1,2,3,4,5 };
    int *b = arr, *e = arr + size(arr);
    while (b < e) {
        cout << *b << " ";
        ++b;
    }
    cout << endl;
    system("pause");
    return 0;
}

 3.35

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int nums[] = { 1,2,3,4,5,6,7,8,9,10 };
 6     for (int *pbeg = nums, *pend = nums + size(nums); pbeg != pend; ++pbeg) {
 7         *pbeg = 0;
 8     }
 9     for (auto c : nums) {
10         cout << c << " ";
11     }
12     cout << endl;
13     system("pause");
14     return 0;
15 }    
View Code

3.36

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main()
 5 {
 6     string str1[] = { "a","b","c","d" }, str2[] = {"a","b","c","d"};
 7     if (str1->size() == str2->size()) {
 8         for (string *pbeg = str1, *pbeg2 = str2, *pend = str1 + size(str1); pbeg != pend; ++pbeg,++pbeg2) {
 9             if (*pbeg == *pbeg2) {
10                 cout << "equal" << endl;
11             }
12             else {
13                 cout << "unequal" << endl;
14             }
15         }
16     }
17     else
18         cout << "unequal" << endl;
19     system("pause");
20     return 0;
21 }    
View Code

比较vector对象:

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 using namespace std;
 5 int main()
 6 {
 7     vector<string> str1 = { "a","b","c","d" }, str2 = {"a","b","c","d"};
 8     if (str1 == str2) {
 9         cout << "equal" << endl;
10     }
11     
12     system("pause");
13     return 0;
14 }    
View Code

 3.37

常量指针p指向字符数组ca的第一个元素‘h’,然后遍历ca,逐个输出ca的元素,但是由于数组尾部没有以‘\0’结尾,所以会输出不确定的字符。

3.38

指针指向的是对象的地址,地址相加没有明确意义,如果相加结果超过指针长度将引发错误。

3.39

比较string的大小:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main()
 5 {
 6     string str1 = "A string example";
 7     string str2 = "A different string";
 8     if (str1 < str2)
 9         cout << "str1 is smaller than str2" << endl;
10     else if (str1 > str2)
11         cout << "str1 is bigger than str2" << endl;
12     else
13         cout << "str1 is equal to str2" << endl;
14     system("pause");
15     return 0;
16 }
View Code

比较char大小:

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     const char ca1[] = "A string example";
 6     const char ca2[] = "A different string";
 7     if (strcmp(ca1, ca2) < 0)
 8         cout << "ca1 is smaller than ca2" << endl;
 9     else if (strcmp(ca1, ca2) > 0)
10         cout << "ca1 is bigger than ca2" << endl;
11     else
12         cout << "ca1 is equal to ca2" << endl;
13     system("pause");
14     return 0;
15 
16 }
View Code

3.40

很奇怪,会报错。需要在strcpy、strcat后面添加_s才对

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int main()
 5 {
 6 
 7     char ca1[50] = "hello world";
 8     char ca2[100] = "I am ArcherZon";
 9     char ca3[200];
10     strcpy_s (ca3, ca1);
11     strcat_s (ca3, ", ");
12     strcat_s (ca3, ca2);
13     cout << ca3 << endl;
14 
15     system("pause");
16     return 0;
17 }
View Code

 3.41

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int main()
 5 {
 6     int nums[] = { 0,1,2,3,4,5,6,7,8,9,10 };
 7     vector<int> ivec(nums, end(nums));
 8     for (auto c : ivec)
 9         cout << c << " ";
10     cout << endl;
11         system("pause");
12     return 0;
13 
14 }
View Code

3.42

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int main()
 5 {
 6     int nums[] = { 1,2,3,4,5,6,7,8,9,10 };
 7     vector<int> ivec(nums, end(nums));
 8     int nums1[10];
 9     for (int i = 0; i < ivec.size(); ++i) {
10         nums1[i] = ivec[i];
11     }
12     for (auto c : nums1) {
13         cout << c << " ";
14     }
15     cout << endl;
16     system("pause");
17     return 0;
18 }
View Code

 P113

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int main()
 5 {
 6     constexpr  size_t  rowCnt = 10, colCnt = 10;
 7     int nums[rowCnt][colCnt];
 8     for (size_t i = 0; i != rowCnt;++i) {
 9         for (size_t j = 0; j != colCnt; ++j) {
10             nums[i][j] = i * colCnt + j;
11             cout << nums[i][j] << " ";
12         }
13     }
14     cout << endl;
15     system("pause");
16     return 0;
17 
18 }
View Code

使用范围for语句:

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     constexpr size_t rowCnt = 50, colCnt = 50;
 6     int nums[rowCnt][colCnt];
 7     /*
 8     for (size_t i = 0; i != rowCnt; ++i) {
 9         for (size_t j = 0; j != colCnt; ++j) {
10             nums[i][j] = i * colCnt + j;
11             cout << nums[i][j] << " ";
12         }
13     }
14     cout << endl; */
15     size_t cnt = 0;
16     for (auto &row : nums) {
17         for (auto &col : row) {
18             col = cnt;
19             ++cnt;
20             cout << col << " ";
21         }
22     }
23     cout << endl;
24     system("pause");
25     return 0;
26 }
View Code

 P115

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int ia[3][4];
 6     for (size_t i = 0; i < 3; ++i) {
 7         for (size_t j = 0; j < 4; ++j) {
 8             ia[i][j] = i * 4 + j;
 9         }
10     }
11     for (auto p = ia; p != ia + 3; ++p) {
12         for (auto q = *p; q != *p + 4; ++q) {
13             cout << *q << " ";
14         }
15     }
16     cout << endl;
17     system("pause");
18     return 0;
19 }
View Code

3.43

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     constexpr size_t rowCnt = 3, colCnt = 4;
 6     int ia[rowCnt][colCnt];
 7     for (size_t i = 0; i < rowCnt; ++i) {
 8         for (size_t j = 0; j < colCnt; ++j) {
 9             ia[i][j] = i * colCnt + j;
10         }
11     }
12     //范围for语句
13     for (const int (&row)[4] : ia) {
14         for (int col : row) {
15             cout << col << " ";
16         }
17     }
18     cout << endl;
19     //下标运算符
20     for (size_t i = 0; i < rowCnt; ++i) {
21         for (size_t j = 0; j < colCnt; ++j) {
22             cout << ia[i][j] << " ";
23         }
24     }
25     cout << endl;
26     //指针
27     for (int (*p)[4] = ia; p != end(ia); ++p) {
28         for (int *q = *p; q != end(*p); ++q) {
29             cout << *q << " ";
30         }
31     }
32     cout << endl;
33     system("pause");
34     return 0;
35 }
View Code

3.44

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     constexpr size_t rowCnt = 3, colCnt = 4;
 6     int ia[rowCnt][colCnt];
 7     using int_arr=int[4];
 8     typedef int intarr[4];
 9     for (size_t i = 0; i < rowCnt; ++i) {
10         for (size_t j = 0; j < colCnt; ++j) {
11             ia[i][j] = i * colCnt + j;
12         }
13     }
14     //范围for语句
15     for (int_arr &p : ia) {
16         for (int q : p) {
17             cout << q << " ";
18         }
19     }
20     cout << endl;
21     //下标运算符
22     for (size_t i = 0; i < rowCnt; ++i) {
23         for (size_t j = 0; j < colCnt; ++j) {
24             ia[i][j] = i * colCnt + j;
25             cout << ia[i][j] << " ";
26         }
27     }
28     cout << endl;
29     //指针
30     for (int_arr *p = ia; p != end(ia); ++p) {
31         for (int *q = *p; q != end(*p); ++q) {
32             cout << *q << " ";
33         }
34     }
35     cout << endl;
36     system("pause");
37     return 0;
38 }
View Code

3.45

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     constexpr size_t rowCnt = 3, colCnt = 4;
 6     int ia[rowCnt][colCnt];
 7     for (size_t i = 0; i < rowCnt; ++i) {
 8         for (size_t j = 0; j < colCnt; ++j) {
 9             ia[i][j] = i * colCnt + j;
10         }
11     }
12     //范围for语句
13     for (auto &p : ia) {
14         for (auto &q : p) {
15             cout << q << " ";
16         }
17     }
18     cout << endl;
19     //下标运算符
20     for (size_t i = 0; i < rowCnt; ++i) {
21         for (size_t j = 0; j < colCnt; ++j) {
22             ia[i][j] = i * colCnt + j;
23             cout << ia[i][j] << " ";
24         }
25     }
26     cout << endl;
27     //指针
28     for (auto p = ia; p != end(ia); ++p) {
29         for (auto q = *p; q != end(*p); ++q) {
30             cout << *q << " ";
31         }
32     }
33     cout << endl;
34     system("pause");
35     return 0;
36 }
View Code

 

posted @ 2018-09-07 21:26  archerzon  阅读(210)  评论(0编辑  收藏  举报