Chapter 3. Strings, Vectors, and Arrays

//Exercises Section 3.5.1
//Exercise 3.27: Assuming txt_size is a function that takes no arguments
//and returns an int value, which of the following definitions are illegal ?
//Explain why.
//unsigned buf_size = 1024;
//(a) int ia[buf_size];	//illegal, the demension value must be a constant
//(b) int ia[4 * 7 - 14];
//(c) int ia[txt_size()];	//illegal
//(d) char st[11] = "fundamental";	//size wrong


//Exercise 3.28: What are the values in the following arrays ?
string sa[10];//""
int ia[10];// 0
int main() {
	string sa2[10];// ""
	int ia2[10]; // uncertain
}


//Exercises Section 3.5.2
//Exercise 3.30: Identify the indexing errors in the following code :
int main() {
	constexpr size_t array_size = 10;
	int ia[array_size];
	for (size_t ix = 0; ix < array_size; ++ix)
		ia[ix] = ix;
	return 0;
}


//Exercise 3.31: Write a program to define an array of ten ints.Give each
//element the same value as its position in the array.
int main() {
	int arr[10];
	for (auto i = 0; i < 10; ++i) arr[i] = i;
	for (auto i : arr) cout << i << " ";
	cout << endl;
	return 0;
}



//Exercise 3.32: Copy the array you defined in the previous exercise into
//another array.Rewrite your program to use vectors.
int main() {
	int arr[10];
	for (auto i = 0; i < 10; ++i) arr[i] = i;
	for (auto i : arr) cout << i << " ";
	cout << endl;
	int arr2[10];
	for (int i = 0; i < 10; ++i) arr2[i] = arr[i];

	vector<int> v(10);
	for (int i = 0; i != 10; ++i) v[i] = arr[i];
	vector<int> v2(v);
	for (auto i : v2) cout << i << " ";
	cout << endl;
	return 0;
}



//Exercises Section 3.5.3
//Exercise 3.34: Given that p1 and p2 point to elements in the same array,
//what does the following code do ? Are there values of p1 or p2 that make
//this code illegal ?
//p1 += p2 - p1;
//p1, p2 points the same address.

//Exercise 3.36: Write a program to compare two arrays for equality.Write a
//similar program to compare two vectors.

bool compare(int *const pb1, int *const pe1, int *const pb2, int *const pe2) {
	if ((pe1 - pe2) != (pe2 - pb2)) return false;
	else {
		for (int *i = pb1, *j = pb2; (i != pe1) && (j != pe2); ++i, ++j)
			if (*i != *j) return false;
	}
	return true;
}

int main() {
	int a1[] = { 0, 1, 2 };
	int a2[] = { 0, 2, 4 };
	if (compare(begin(a1), end(a1), begin(a2), end(a2)))
		cout << "equal !\n";
	else cout << "not equal !\n";

	vector<int> vec1 = { 0, 1, 2 };
	vector<int> vec2 = { 0, 1, 2 };
	if(vec1 == vec2) cout << "equal !\n";
	else cout << "not equal !\n";
	return 0;
}




//Exercises Section 3.5.4
//Exercise 3.37: What does the following program do ?

int main() {
	const char ca[] = { 'h', 'e', 'l', 'l', 'o' };
	const char *cp = ca;
	while (*cp) {
		cout << *cp << endl;
		++cp;
	}
	return 0;
}

//Exercise 3.38: In this section, we noted that it was not only illegal but
//meaningless to try to add two pointers.Why would adding two pointers be
//meaningless ?
//it make no sense to add two address.




//Exercises Section 3.6
//Exercise 3.43: Write three different versions of a program to print the
//elements of ia.One version should use a range for to manage the
//iteration, the other two should use an ordinary for loop in one case using
//subscripts and in the other using pointers.In all three programs write all the
//types directly.That is, do not use a type alias, auto, or decltype to
//simplify the code.

int main() {
	int arr[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};
	for (const int(&row)[4] : arr)
		for (int col : row) cout << col << " ";
	cout << endl;

	for (size_t i = 0; i != 3; ++i)
		for (size_t j = 0; j != 4; ++j) cout << arr[i][j] << " ";
	cout << endl;

	for (int(*row)[4] = arr; row != arr + 3; ++row)
		for (int *col = *row; col != *row + 4; ++col)
			cout << *col << " ";
	cout << endl;
	return 0;
}



//Exercise 3.44: Rewrite the programs from the previous exercises using a
//type alias for the type of the loop control variables.

int main() {
	int arr[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};
	using int_array = int[4];
	for (int_array &p : ia)
		for (int q : p)
			cout << q << " ";
	cout << endl;
	return 0;
}


//Exercise 3.45: Rewrite the programs again, this time using auto.

int main() {
	int arr[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};
	for (auto &p : arr)
		for (int q : p)
			cout << q << " ";
	cout << endl;

	for (size_t i = 0; i != 3; ++i)
		for (size_t j = 0; j != 4; ++j)
			cout << arr[i][j] << " ";
	cout << endl;

	for (auto p = arr; p != arr + 3; ++p)
		for (int *q = *p; q != *p + 4; ++q)
			cout << *q << " ";
	cout << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值