C++ primer 第5版 练习题记录

博主分享了在阅读C++ Primer过程中的学习体验,记录了第三章部分练习题的解答,包括字符串操作、数组处理和迭代器使用等。通过代码示例展示了如何处理输入、转换字符串、计算相邻整数和以及处理向量。博主还表达了对后续章节学习的期待,并鼓励读者交流学习心得。

最近在看c++ primer,在此开个贴记录一下自己的练习题完成情况,也算是一种督促。希望能跟搜到此贴的小伙伴进行一些学习上的交流。

本文是第三章的练习,第四章的在这里:

C++ primer 第五版 练习题记录_Chloe0333的博客-CSDN博客icon-default.png?t=M4ADhttps://blog.csdn.net/Chloe0333/article/details/124824267

新增第五章

http://t.csdn.cn/EBmaGhttp://t.csdn.cn/EBmaG

1.5.1节练习

练习1.20

http://www.informit.com/title/0321714113

3.4.1节练习

练习3.21 略

练习3.22

头文件和using声明是为了练习方便一直带着的,就直接贴上来了。

#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
	string str1;
	vector <string> str;
	while (cin >> str1) 
	{
		if (str1 == "*")
			break;
		str.push_back(str1);
	}
	for (auto i = str.begin(); i != str.end() && !(*i).empty(); ++i) 
	{
		for (int j = 0; j < (*i).size(); ++j) 
		{
			(*i)[j] = toupper((*i)[j]);
		}
		cout << *i << endl;
	}
	return 0;
}

练习3.23

#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
	int num;
	vector <int> num_set;
	while (cin >> num) 
	{
		num_set.push_back(num);
	}
	for (auto it = num_set.begin(); it != num_set.end(); ++it ) 
	{
		*it = 2 * (*it);
	}
	for (auto a : num_set)
	cout << a << endl;
	return 0;
}

3.4.2节练习

练习3.24

相邻整数和

#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
	int num;
	vector <int> num_set;
	while (cin >> num) 
	{
		num_set.push_back(num);
	}
	for (auto it = num_set.begin(); it < num_set.end()-1; ++it ) 
	{
		*it = *it+(*(it+1));
		cout << *it << endl;
	}
	return 0;
}

首尾和

#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
	int num;
	vector<int> num_set;
	while(cin >> num)
	{
		num_set.push_back(num);
	}
	for (auto it = num_set.begin(), it1 = num_set.end(); it != it1; ++it)
	{
		--it1;
		cout << *it + *it1<< endl;
	}

	return 0;
}

练习3.25

#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
	vector<unsigned> scores(11,0);
	unsigned grade;
	while (cin >> grade)
	{
		auto it = scores.begin(); 
			*(it + grade/10) = *(it + grade/10) + 1;
	}
	for (auto a : scores)
		cout << a << endl;
	return 0;

练习3.26

end - beg 返回的是两个迭代器之间的距离,迭代器与迭代器之间不支持加法。

3.5.1 节练习

练习3.27

a)非法,buf_size不是constexpr类型;

b) 合法;

c)当txt_size是constexpr时合法,否则非法;

d)非法,没有空间可存放空字符。

练习3.28

string是标准库,使用时需要先声明。int是内置库,使用时不需要先声明。

sa 空;ia 10个0;sa2空;ia2 10个0.

练习3.29

不够灵活,编译时要声明维度,不能随意向数组中增加元素,不能将数组的内容拷贝给其他数组作为其初始值,也不能用数组为其他数组赋值。

3.5.2节练习

练习3.30

ix <= array_size

错误,会超出索引。

练习3.31

int main()
{
	int arr[10];
	for (auto i = 0; i < 10; ++i)
		arr[i] = i;
	for(auto a : arr)
		cout << a << endl;
	return 0;
}

练习3.32

拷贝

int main()
{
	int arr[10];
	int b[10];
	for (auto i = 0; i < 10; ++i)
	{
		arr[i] = i;
		b[i] = arr[i];
	}
	for(auto a : b)
		cout << a << endl;
	return 0;
}

vector重写

int main()
{
	vector<int> arr(10,0);
	for (auto i = 0; i < 10; ++i)
	{
		arr[i] = i;
	}
	vector<int> vec = arr;
	for(auto a : vec)
		cout << a << endl;
	return 0;
}

练习3.33

若在函数内部定义了某种内置类型的数组,那么默认初始化会令数组含有未定义的值。

3.5.3节练习

练习3.34

将p1指向p2所指元素,p1、p2合法该程序就合法,否则非法。 

练习3.35

int main()
{
	int arr[] = { 1,2,3,4,5,6 },*p = arr;
	int* beg = begin(arr),*last = end(arr);
	while (beg != last)
	{
		*beg = 0;
		++beg;
	}
	for (auto a : arr)
		cout << a << endl;
	return 0;
}

练习3.36

int main()
{
	int arr[] = { 1,2,3,4,5 }, b[] = { 1,2,3,4,5,6};
	int* beg1 = begin(arr), * last1 = end(arr);
	int* beg2 = begin(b), * last2 = end(b);
	int count = 0;
	if (last1 - beg1 == last2 - beg2)
	{
		for (auto i = 0; i < last1 - beg1; ++i)
		{
			if (arr[i] == b[i])
			{
				++count;
			}
		}

	}
	if (count == last1 - beg1)
		cout << "The two arrays are equal.";
	else
		cout << "The two arrays are not equal.";


	return 0;
}

vector

int main()
{
	vector<int> arr = { 1,2,3,4,5 }, b = { 1,2,3,4,5 };
	int count = 0;
	if (arr.size() == b.size())
	{
		for (auto i = 0; i < arr.size(); ++i)
		{
			if (arr[i] == b[i])
			{
				++count;
			}
		}
	}
	if (count == arr.size())
		cout << "The two vectors are equal.";
	else
		cout << "The two vectors are not equal.";
	return 0;
}

3.5.4节练习

练习 3.37

输出 hello

练习3.38

因为指针指向的是对象的地址,地址相加无意义。

练习3.39

string 

int main()
{
	string ca = "Hello worl";
	string cb = "Hello world";
	int flag = 0;
	if (ca > cb)
		++flag;
	else
		--flag;
	cout << flag << endl;
	return 0;
}

c风格字符串

int main()
{
	const char ca[] = "hello world";
	const char cb[] = "Hello world";
	cout << strcmp(ca, cb) << endl;
	return 0;
}

练习3.30

int main()
{
    char ca[10] = "Hello";
	char cb[10] = "World";
	char a1[20];
	strcpy_s(a1, ca);  // 按照题目要求这里的函数应该是strcpy,但是编译器
	strcat_s(a1, " "); // 报错说unsafe,无法运行,就改成了strcpy_s,strcat_s.
	strcat_s(a1, cb);
	cout << a1 << endl;
	return 0;
}

3.5.5节练习

练习 3.41

int main()
{
	int a[] = { 1,2,3,4 };
	vector<int> vec(begin(a), end(a));
	for(auto c : vec)
	cout << c << endl;
	return 0;
}

练习3.42

int main()
{
	vector<int> vec(10, 5);
	int a[10];
	for (auto i = 0; i < 10; ++i)
	{
		a[i] = vec[i];
      }
	for (auto c:a)
	cout << c << endl;
	return 0;
}

练习3.43

int main()
{
	int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
	for (int(&i)[4] : ia)
	{
		for (int j : i)
			cout << j << " ";
	}
	cout << "\n";
	for (int i = 0; i < 3; ++i)
	{
		for (int j = 0; j < 4; ++j)
			cout << ia[i][j] << " ";
	}
	cout << "\n";
	for (int (*p)[4] = begin(ia); p != end(ia); ++p)
	{
		for (int* q = begin(*p); q != end(*p); ++q)
			cout << *q << " ";
	}
	return 0;
}

练习3.44

int main()
{
	int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
	using arr = int(&)[4];
	for (arr i : ia)
	{
		for (int j : i)
			cout << j << " ";
	}
	cout << "\n";
	using int_ref = int;
	for (int_ref i = 0; i < 3; ++i)
	{
		for (int_ref j = 0; j < 4; ++j)
			cout << ia[i][j] << " ";
	}
	cout << "\n";
	using int_arr = int(*)[4];
	for (int_arr p = begin(ia); p != end(ia); ++p)
	{
		for (int* q = begin(*p); q != end(*p); ++q)
			cout << *q << " ";
	}
	return 0;
}

练习3.45

int main()
{
	int ia[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
	for (auto &i : ia)
	{
		for (auto j : i)
			cout << j << " ";
	}
	cout << "\n";
	for (auto i = 0; i < 3; ++i)
	{
		for (auto j = 0; j < 4; ++j)
			cout << ia[i][j] << " ";
	}
	cout << "\n";
	for (auto p = begin(ia); p != end(ia); ++p)
	{
		for (auto q = begin(*p); q != end(*p); ++q)
			cout << *q << " ";
	}
	return 0;
}

第三章完结撒花

在此立个Flag,下周结束前看完第四章和第五章。加油鸭!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值