C++Primer第五版 第十章习题答案(1~10)

1:这个比较简单,类比下 find() 函数也就知道了。

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void main()
{
	int number = 0;
	int arr[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	vector<int> arr1;
	for (auto a : arr)
		arr1.push_back(a);
	cout << "there are " << count(arr1.begin(), arr1.end(), number) << " times " << number << endl;
	system("pause");
}

2:同1,类比下就知道了。

#include<iostream>
#include<algorithm>
#include<list>
#include<string>
using namespace std;
void main()
{
	string  example("a");
	string str_a[] = {"a","b","c","d"};
	list<string> str;
	for (auto a : str_a)
		str.push_back(a);
	cout << "there are " << count(str.begin(), str.end(), example) << " times " << example << endl;
	system("pause");
}

3:知识点:

1、accumulate() 在头文件:numeric中。

2、accumulate(vec.cbegin(), vec.cend(), 0):第三个参数要与vec所存元素可加。

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
void main()
{
	vector<int> arr{ 0, 1, 2, 3, 4, 5 };
	int sum = accumulate(arr.begin(), arr.end(), 0);
	cout << sum << endl;
	system("pause");
}

4:accumulate(v.cbegin(), v.cend(), 0):相加必须进行类型转换,而v.cbegin() 和 v.cend() 为const。当类型转化时,v 中元素会损失精度,这与其为 const 的要求冲突。(然而编译器没报错)。

5:string 也是容器,有迭代器的。而单纯的 char 数组不存在迭代器。但可以用容器存 char字符, 比较没什么变化。

6:知识点:确保fill_n(vec.begin(), n, num) 中,n的值不能超过容器元素个数。

7:知识点:(两个都是错误的)

1、传递给copy 的目的序列至少要包含与输入序列一样多的元素。

2、reverse() 改变的是容器容量,并没有改变其大小,可用resize() 函数。

8:back_inserter 的作用:接受一个指向容器的引用,返回一个与该容器绑定的插入迭代器。只有我们通过此迭代器赋值时,赋值运算符会调用容器操作 push_back() 将一个给定元素添加到容器中,而这与插入迭代器无关。

9:知识点:
1、sort(): 按字典排序,把重复的单词放在一起,以便查找。                                                                                                            2、unique(): 重排输入序列,将相邻的重复项“消除”,并返回一个指向不重复范围的尾后迭代器。(unique 返回的迭代器指向最后一个不重复元素之后的位置,此位置之后的元素任然存在,但我们不知道他们的值是什么)                                                        3、标准库算法对迭代器而不是容器进行操作。因此,算法不能(直接)添加或删除元素。为了真正删除无用元素,我们必须使用容器操作,本例中使用的是erase(P311)。                                                             

#include<iostream>
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
using namespace std;
void elimDups(vector<string> &words)
{
	cout << "排序前:" << endl;
	for (auto a : words)
	{
		cout << a << ends;
	}
	sort(words.begin(), words.end());    //sort排序
	cout << "\n按字典序排序:" << endl;
	for (auto b : words)
	{
		cout << b << ends;
	}
	vector<string>::iterator vec = unique(words.begin(), words.end());    //unique排序
	cout << "\n重排输入范围:" << endl;
	for (auto c : words)
	{
		cout << c << ends;
	}
	words.erase(vec, words.end());    //erase()操作
	cout << "\n消去重复项:" << endl;
	for (auto d : words)
	{
		cout << d << ends;
	}

}
void main()
{
	vector<string> words{ "a", "b", "b", "a", "c", "b", "c" };
	elimDups(words);
	system("pause");
}

10:算法基于迭代器来操作以实现泛型,而当需要在容器中添加或删除元素时, 不知道容器和容器元素的具体类型,就不可能知道需要增加或减少多少空间,就无法实现容器添加或删除元素的目的。 添加或删除容器元素的操作是与类型强相关的,而泛型的算法做不到这点。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值