C++ Primer 中文版(第 5 版)练习解答合集
自己写的解答,如有错误之处,烦请在评论区指正!
- a. 非法,
buf_size
不是常量表达式;
b. 合法;
c. 非法,txt_size()
的返回值不是constexpr
;
d. 非法,没有空间存放字符串尾的\0
。 sa
:10 个空字符串;
ia
:10 个 0;
sa2
和ia2
中元素的值未定义。- 插入、删除元素繁琐,初始化时必须规定长度,缺少一些
vector
特有的成员函数。 ia
长度是 10,元素下标从 0 到 9。
#include <iostream>
int main() {
int a[10];
for (int i = 0; i != 10; ++i)
a[i] = i;
return 0;
}
#include <iostream>
int main() {
int a[10];
for (int i = 0; i != 10; ++i)
a[i] = i;
int b[10];
for (int i = 0; i != 10; ++i)
b[i] = a[i];
return 0;
}
// vector 版本
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a(10, 0);
for (int i = 0; i != a.size(); ++i)
a[i] = i;
vector<int> b = a;
return 0;
}
scores
数组中元素的值都是未定义的,或者说是未知的,原本的那段内存空间上可以是任意值,于是统计就会出现错误。- 功能类似于
p1 = p2;
,一般都是合法的。
#include <iostream>
using namespace std;
int main() {
int a[100];
for (int *p = a; p != end(a); ++p)
*p = 0;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a[5], b[5];
for (int i = 0; i < 5; ++i)
cin >> a[i];
for (int i = 0; i < 5; ++i)
cin >> b[i];
for (int i = 0; i < 5; ++i) {
if (a[i] != b[i]) {
cout << "Not equal." << endl;
break;
}
}
cout << "Equal." << endl;
return 0;
}
// vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
int input;
vector<int> a, b;
for (int i = 0; i < 5; ++i) {
cin >> input;
a.push_back(input);
}
for (int i = 0; i < 5; ++i) {
cin >> input;
b.push_back(input);
}
if (a == b)
cout << "Equal." << endl;
else
cout << "Not equal." << endl;
return 0;
}
- 用
cp
遍历字符串ca
,并输出其中每一个字符(相当于输出ca
)。结果是hello
。 - 指针的值本质上是内存地址,而内存地址的差表示两个地址之间的差距,内存地址的和本来就没有意义,所以指针可以相减,不能相加。
// string
#include <iostream>
#include <string>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
if (a > b)
cout << "a > b" << endl;
else if (a < b)
cout << "a < b" << endl;
else
cout << "a == b" << endl;
}
// C 风格
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char a[100], b[100];
cin >> a >> b;
if (strcmp(a, b) > 0)
cout << "a > b" << endl;
else if (strcmp(a, b) < 0)
cout << "a < b" << endl;
else
cout << "a == b" << endl;
}
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char a[] = "Hello", b[] = " world!";
char c[50];
strcpy(c, a);
strcat(c, b);
cout << c << endl;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
int a[10] = {};
vector<int> ivec(begin(a), end(a));
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
int a[10] = {};
vector<int> ivec(begin(a), end(a));
for (int i = 0; i != ivec.size(); ++i)
a[i] = ivec[i];
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
int ia[3][4] = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6},
};
cout << "Range for:\n";
for (int (&p)[4] : ia)
for (int q : p)
cout << q << " ";
cout << "\n\n";
cout << "Normal for with index:\n";
for (int i = 0; i != 3; ++i)
for (int j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << "\n\n";
cout << "Normal for with pointer:\n";
for (int (*p)[4] = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
using arr_int = int[4];
int main(){
int ia[3][4] = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6},
};
cout << "Range for:\n";
for (arr_int &p : ia)
for (int q : p)
cout << q << " ";
cout << "\n\n";
cout << "Normal for with index:\n";
for (int i = 0; i != 3; ++i)
for (int j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << "\n\n";
cout << "Normal for with pointer:\n";
for (arr_int *p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
int ia[3][4] = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6},
};
cout << "Range for:\n";
for (auto &i : ia)
for (auto &j : i)
cout << j << " ";
cout << "\n\n";
cout << "Normal for with index:\n";
for (auto i = 0; i != 3; ++i)
for (auto j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << "\n\n";
cout << "Normal for with pointer:\n";
for (auto i = begin(ia); i != end(ia); ++i)
for (auto j = begin(*i); j != end(*i); ++j)
cout << *j << " ";
return 0;
}