目录
第十章 范型算法
10.1 概述(10.1 ~ 10.2)
10.2 初识范型算法
10.2.1 只读算法(10.3 ~ 10.5)
10.2.2 写容器元素的算法(10.6 ~ 10.8)
10.2.3?重排容器元素的算法(10.9 ~ 10.10)
10.3 定制操作
10.3.1 向算法传递函数(10.11 ~ 10.13)
10.3.2 lambda表达式(10.14 ~ 10.18)
未完待续
工作的间隙看的,所以输出比较慢,希望能巩固基础,再往后深入。
一直有参考这位同学的blog的答案:C++Primer第五版——习题答案+详解(完整版)_MISAYAONE的博客-CSDN博客,不过好像这位同学看的很快有很一些些不是很正确,看评论也有都一一修正。
这个答案也是自己看书然后输出的,也可能有问题,如果有发现什么问题,欢迎评论一起讨论!!
默认大家都有了第5版的纸质书或电子书,这里就只记录题号和答案(其实对原书的截图有点侵犯版权的感觉,狗头保命)
第十章 范型算法
10.1 概述(10.1 ~ 10.2)
10.1:
int a[11] = {1,2,3,4,5,6,7,8,9,10,10};
vector<int> ivec(a, a + 11);
auto result = count(ivec.cbegin(), ivec.cend(), 10);
cout << result << endl;
// 2
10.2:
string a[11] = {"1","2","3","4","5","6","7","8","9","10","10"};
list<string> ivec(a, a + 11);
auto result = count(ivec.cbegin(), ivec.cend(), "11");
cout << result << endl;
// 0
10.2 初识范型算法
10.2.1 只读算法(10.3 ~ 10.5)
10.3:
vector<int> a = {1,2,3,4,5,6,7,8,9,10,10};
int sum = accumulate(a.cbegin(), a.cend(), 0);
cout << sum << endl;
// 65
10.4:
会将double类型强制转换为int,损失精度。
10.5:
没有问题,一切正常。
10.2.2 写容器元素的算法(10.6 ~ 10.8)
10.6:
vector<int> a = {1,2,3,4,5,6,7,8,9,10,10};
fill_n(a.begin(), a.size(), 0);
for(const auto& x : a) {
cout << x << " ";
}
cout << endl;
// 0 0 0 0 0 0 0 0 0 0 0
10.7:
vec是空向量,语句未定义。
reserve是改变容器容量,并没有改变其大小,也是未定义语句。
10.8:
back_inserter会创建一个插入迭代器,然后使用这个迭代器进行插入操作。
10.2.3?重排容器元素的算法(10.9 ~ 10.10)
10.9:
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
for(const auto& i : words){
cout << i << " ";
}
cout << endl;
// fox jumps over quick red slow the turtle
10.10:
算法操作的都是迭代器,不对容器进行操作,不能(直接)添加或删除元素。
10.3 定制操作
10.3.1 向算法传递函数(10.11 ~ 10.13)
10.11:
void elimDups(vector<string> &words) {
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
bool isShorter(const string &s1, const string &s2) {
return s1.size() < s2.size();
}
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
elimDups(words);
sort(words.begin(), words.end(), isShorter);
for(const auto& i : words){
cout << i << " ";
}
cout << endl;
}
// fox red the over slow jumps quick turtle
10.12:
class Sales_data {
public:
Sales_data(const string &s) : isbn_(s) { }
string isbn() const { return isbn_; }
private:
string isbn_;
};
bool compareIsbn(const Sales_data &s1, const Sales_data &s2) {
return s1.isbn().size() < s2.isbn().size();
}
int main()
{
vector<Sales_data> vec = { Sales_data("the"), Sales_data("quick"), Sales_data("red"), Sales_data("fox"), Sales_data("jumps"), Sales_data("over"), Sales_data("the"), Sales_data("slow"), Sales_data("red"), Sales_data("turtle") };
sort(vec.begin(), vec.end(), compareIsbn);
for(const auto &s : vec) {
cout << s.isbn() << " ";
}
}
// the red fox the red over slow quick jumps turtle
10.13:
bool PartitionString(const string &s) {
return s.size() >= 5;
}
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
auto end_partition = partition(words.begin(), words.end(), PartitionString);
for(auto it = words.begin(); it != end_partition; it++) {
cout << *it << " ";
}
}
// turtle quick jumps
10.3.2 lambda表达式(10.14 ~ 10.18)
10.14:
auto countTwoIntSum = [](int a, int b){ return a + b; };
cout << countTwoIntSum(1, 2);
// 3
10.15:
int tmp = 10;
auto countTwoIntSum = [tmp](int a){ return a + tmp; };
cout << countTwoIntSum(1);
// 11
10.16:
没有太理解题目的意思,要写成什么样,看了别的同学的答案是"题上的biggies已经写的很好了",那这题就略过吧~
10.17:
class Sales_data {
public:
Sales_data(const string &s) : isbn_(s) { }
string isbn() const { return isbn_; }
private:
string isbn_;
};
int main()
{
vector<Sales_data> vec = { Sales_data("the"), Sales_data("quick"), Sales_data("red"), Sales_data("fox"), Sales_data("jumps"), Sales_data("over"), Sales_data("the"), Sales_data("slow"), Sales_data("red"), Sales_data("turtle") };
sort(vec.begin(), vec.end(), [](const Sales_data &s1, const Sales_data &s2){
return s1.isbn().size() < s2.isbn().size();});
for(const auto &s : vec) {
cout << s.isbn() << " ";
}
}
// the red fox the red over slow quick jumps turtle
10.18:
void elimDups(vector<string> &words) {
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
string make_plural(size_t ctr, const string &word, const string &ending) {
return ctr > 1 ? word + ending : word;
}
void biggies(vector<string> &words, vector<string>::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string &a, const string &b){ return a.size() < b.size();});
auto wc = partition(words.begin(), words.end(), [sz](const string &a){ return a.size() >= sz; });
auto count = wc - words.begin();
cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl;
for_each(words.begin(), wc, [](const string &s){ cout << s << " "; });
cout << endl;
}
int main()
{
vector<string> words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
biggies(words, 5);
}
// 3 words of length 5 or longer
// turtle quick jumps
未完待续
|