C++ Primer 中文第 5 版练习答案 第 3 章 字符串、向量和数组(27~45)

C++ Primer 中文版(第 5 版)练习解答合集

自己写的解答,如有错误之处,烦请在评论区指正!


  1. a. 非法,buf_size不是常量表达式;
    b. 合法;
    c. 非法,txt_size()的返回值不是constexpr;
    d. 非法,没有空间存放字符串尾的\0
  2. sa:10 个空字符串;
    ia:10 个 0;
    sa2ia2中元素的值未定义。
  3. 插入、删除元素繁琐,初始化时必须规定长度,缺少一些vector特有的成员函数。
  4. ia长度是 10,元素下标从 0 到 9。
#include 
int main() {
	int a[10];
	for (int i = 0; i != 10; ++i)
		a[i] = i;
	return 0;
}
#include 
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 
#include 
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;
}
  1. scores数组中元素的值都是未定义的,或者说是未知的,原本的那段内存空间上可以是任意值,于是统计就会出现错误。
  2. 功能类似于p1 = p2;,一般都是合法的。
#include 
using namespace std;
int main() {
	int a[100];
	for (int *p = a; p != end(a); ++p)	
		*p = 0;
	return 0;
}
#include 
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 
#include 
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;
}
  1. cp遍历字符串ca,并输出其中每一个字符(相当于输出ca)。结果是hello
  2. 指针的值本质上是内存地址,而内存地址的差表示两个地址之间的差距,内存地址的和本来就没有意义,所以指针可以相减,不能相加。
// string
#include 
#include 
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 
#include 
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 
#include 
using namespace std;
int main() {
	char a[] = "Hello", b[] = " world!";
	char c[50];
	strcpy(c, a);
	strcat(c, b);
	cout << c << endl;
}
#include 
#include 
using namespace std;
int main() {
	int a[10] = {};
	vector<int> ivec(begin(a), end(a));
	return 0;
}
#include 
#include 
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 
#include 
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 
#include 
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 
#include 
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;
} 

你可能感兴趣的:(C++,Primer)