《C++ Primer》第五版课后习题解答_第三章(5)(37-45)
系统环境: windows 10 1703
编译环境:Visual studio 2017
3.37
程序想要将字符数组 ca 中的元素输出。因为 ca 的结尾没有空字符,所以会发生未定义行为(undefined behavior)。
3.38
摘抄stack overflow 上的一个回答:
Pointer addition is forbidden in C++, you can only subtract two pointers.
The reason for this is that subtracting two pointers gives a logically explainable result - the offset in memory between two pointers. Similarly, you can subtract or add an integral number to/from a pointer, which means "move the pointer up or down". Adding
a pointer to a pointer is something which is hard to explain. What would the resulting pointner represent?
If by any chance you explicitly need a pointer to a place in memory whose address is the sum of some other two addresses, you can cast the two pointers to int, add ints, and cast back to a pointer. Remember though, that this solution needs huge care about
the pointer arithmetic and is something you really should never do.
3.39
#include <iostream> #include <string> using std::cout; using std::endl; using std::string; // String int main() { string s1 = "This is string 1"; string s2 = "This is string 2"; if (s1 != s2) { cout << "Not Equal!!!" << endl; } else { cout << "The two strings are equal" << endl; } return 0; }
#include <iostream> #include <string> using std::cout; using std::endl; using std::string; // C-style charater string int main() { const char ch1[] = "This is C-style charater string 1"; const char ch2[] = "This is C-style charater string 2"; if (strcmp(ch1, ch2) < 0) { cout << "Not Equal!!!" << endl; } else { cout << "The two strings are equal" << endl; } return 0; }
3.40
这道题搞了好久也没搞出来。去 github 上找了讨论帖,链接和答案如下:
https://github.com/Mooophy/Cpp-Primer/pull/241
#include <iostream> #include <cstring> using std::cout; using std::endl; constexpr size_t constexpr_strlen(const char *s) { return sizeof(s) / sizeof(*s); } constexpr size_t merge_size(const char* cs1, const char* cs2) { return constexpr_strlen(cs1) + constexpr_strlen(" ") + constexpr_strlen(cs2) + 1; } int main() { constexpr const char cs1[] = "Hi"; constexpr const char cs2[] = "frank67"; char cs3[merge_size(cs1, cs2)]; strcpy(cs3, cs1); strcat(cs3, " "); strcat(cs3, cs2); cout << cs3 << endl; return 0; }
3.41
#include <iostream> #include <string> #include <cstring> #include <vector> using std::cout; using std::endl; using std::vector; using std::string; int main() { int ich[] = { 1, 2, 3, 4, 5, 6, 7 }; vector<int> ivec(std::begin(ich), std::end(ich)); for (auto a : ivec) { cout << a << endl; } return 0; }
3.42
#include <iostream> #include <string> #include <cstring> #include <vector> using std::cout; using std::endl; using std::vector; using std::string; int main() { vector<int> ivec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int ich[10] = { }; for (int i = 0; i != ivec.size(); ++i) { ich[i] = ivec[i]; } for (auto a : ich) { cout << a << endl; } return 0; }
3.43
#include <iostream> #include <string> #include <cstring> #include <vector> using std::cout; using std::endl; using std::vector; using std::string; using std::begin; using std::end; int main() { int ia[2][3] = { {1, 2, 3}, {4, 5, 6} }; //1. 范围 for for (const int(&i)[3] : ia) { for (int j : i) { cout << j << endl; } cout << endl; } //2. 普通 for 循环 for (size_t i = 0; i != 2; ++i) { for (size_t j = 0; j != 3; ++j) { cout << ia[i][j] << endl; } cout << endl; } //3. 指针 for (int (*i)[3] = begin(ia) ; i != end(ia); ++i) { for (int *j = begin(*i); j != end(*i); ++j) { cout << *j << endl; } cout << endl; } return 0; }
3.44
#include <iostream> #include <string> #include <cstring> #include <vector> using std::cout; using std::endl; using std::vector; using std::string; using std::begin; using std::end; int main() { int ia[2][3] = { {1, 2, 3}, {4, 5, 6} }; using int_array = int[3]; //1. 范围 for for (int_array &i : ia) { for (int j : i) { cout << j << endl; } cout << endl; } //2. 普通 for 循环 for (size_t i = 0; i != 2; ++i) { for (size_t j = 0; j != 3; ++j) { cout << ia[i][j] << endl; } cout << endl; } //3. 指针 for (int_array *i = begin(ia) ; i != end(ia); ++i) { for (int *j = begin(*i); j != end(*i); ++j) { cout << *j << endl; } cout << endl; } return 0; }
3.45
#include <iostream> #include <string> #include <cstring> #include <vector> using std::cout; using std::endl; using std::vector; using std::string; using std::begin; using std::end; int main() { int ia[2][3] = { {1, 2, 3}, {4, 5, 6} }; using int_array = int[3]; //1. 范围 for for (auto &i : ia) { for (auto j : i) { cout << j << endl; } cout << endl; } //2. 普通 for 循环 for (auto i = 0; i != 2; ++i) { for (auto j = 0; j != 3; ++j) { cout << ia[i][j] << endl; } cout << endl; } //3. 指针 for (auto *i = begin(ia) ; i != end(ia); ++i) { for (auto *j = begin(*i); j != end(*i); ++j) { cout << *j << endl; } cout << endl; } return 0; }