目录
第十章 泛型算法
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.19)
10.3.3 lambda捕获和返回(10.20 ~ 10.21)
10.3.4 参数绑定(10.22 ~ 10.25)
10.4 再探迭代器
10.4.1 插入迭代器(10.26 ~ 10.28)
10.4.2 iostream迭代器(10.29 ~ 10.33)
10.4.3 反向迭代器(10.34 ~ 10.37)
10.5 泛型算法结构
10.5.1 5类迭代器(10.38 ~ 10.40)
10.5.2 算法形参模式(无)
10.5.3 算法命名规范(10.41)
10.6 特定容器算法(10.42)
工作的间隙看的,所以输出比较慢,希望能巩固基础,再往后深入。
一直有参考这位同学的blog的答案:C++Primer第五版——习题答案+详解(完整版)_MISAYAONE的博客-CSDN博客,不过好像这位同学看的很快有很一些些不是很正确,看评论也有都一一修正。
这个答案也是自己看书然后输出的,也可能有问题,如果有发现什么问题,欢迎评论一起讨论!!
默认大家都有了第5版的纸质书或电子书,这里就只记录题号和答案(其实对原书的截图有点侵犯版权的感觉,狗头保命)
10.1:
int a[11] = {1,2,3,4,5,6,7,8,9,10,10};
vector 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 ivec(a, a + 11);
auto result = count(ivec.cbegin(), ivec.cend(), "11");
cout << result << endl;
// 0
10.3:
vector 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.6:
vector 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.9:
vector 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.11:
void elimDups(vector &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 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 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 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.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 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 &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 &words, vector::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 words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
biggies(words, 5);
}
// 3 words of length 5 or longer
// turtle quick jumps
10.19:
void elimDups(vector &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 &words, vector::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string &a, const string &b){ return a.size() < b.size();});
auto wc = stable_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 words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
biggies(words, 5);
}
// 3 words of length 5 or longer
// jumps quick turtle
10.20:
vector words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
auto count_result = count_if(words.begin(), words.end(), [](const string &str){ return str.size() > 6; });
cout << count_result << endl;
// 0
10.21:
int main()
{
int t = 2;
auto f = [&t]()->bool{
if(t > 0){
t--;
return false;
}
else{
return true;
}
};
cout << "t:" << t << ",是否为0:"<< f() << endl;
cout << "t:" << t << ",是否为0:"<< f() << endl;
cout << "t:" << t << ",是否为0:"<< f() << endl;
cout << "t:" << t << ",是否为0:"<< f() << endl;
}
//t:2,是否为0:0
//t:1,是否为0:0
//t:0,是否为0:1
//t:0,是否为0:1
10.22:
bool check_size(const string &s, string::size_type sz) {
return s.size() <= sz;
}
int main()
{
vector words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
auto count_result = count_if(words.begin(), words.end(), bind(check_size, std::placeholders::_1, 6));
cout << count_result << endl;
}
// 10
10.23:
n表示占位符的个数的话,bind的参数数为n+1或者n+2?
1为函数名,2为函数名和类名?
n的个数有看到20个也有看到29个的?
10.24:
bool check_size(const string &s, string::size_type sz) {
return s.size() < sz;
}
int main()
{
string str = "turtle";
vector vsizes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto wc = find_if(vsizes.begin(), vsizes.end(), bind(check_size, str, std::placeholders::_1));
cout << *wc << endl;
}
// 7
10.25:
bool check_size(const string &s, string::size_type sz) {
return s.size() >= sz;
}
void elimDups(vector &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 &words, vector::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(), bind(check_size, std::placeholders::_1, 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 words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
biggies(words, 5);
}
// 3 words of length 5 or longer
// turtle quick jumps
10.26:
10.27:
vector words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
vector words2{};
sort(words.begin(), words.end());
unique_copy(words.begin(), words.end(), inserter(words2, words2.begin()));
for(const auto& word : words2){
cout << word << " ";
}
// fox jumps over quick red slow the turtle
10.28:
vector numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
vector numbers1{}, numbers2{};
list numbers3{};
copy(numbers.begin(), numbers.end(), inserter(numbers1, numbers1.begin()));
copy(numbers.begin(), numbers.end(), back_inserter(numbers2));
copy(numbers.begin(), numbers.end(), front_inserter(numbers3));
for(const auto& number : numbers1){
cout << number << " ";
}
cout << endl;
for(const auto& number : numbers2){
cout << number << " ";
}
cout << endl;
for(const auto& number : numbers3){
cout << number << " ";
}
cout << endl;
// 1 2 3 4 5 6 7 8 9
// 1 2 3 4 5 6 7 8 9
// 9 8 7 6 5 4 3 2 1
10.29:
fstream fstrm("test.txt");
istream_iterator in_iter(fstrm), eof;
vector vec{};
while(in_iter != eof){
vec.push_back(*in_iter++);
}
ostream_iterator out_iter(cout, "\n");
copy(vec.begin(), vec.end(), out_iter);
// https://www.baidu.com
// https://www.google.com
10.30:
istream_iterator in_iter(cin), eof;
vector vec{};
int n = 0;
while(in_iter != eof){
vec.push_back(*in_iter++);
n++;
}
sort(vec.begin(), vec.end());
ostream_iterator out_iter(cout, " ");
copy(vec.begin(), vec.end(), out_iter);
// 9 8 7 6 5 4 3 2 1
// 9 8 7 6 5 4 3 2 1
// ^Z
// 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
10.31:
istream_iterator in_iter(cin), eof;
vector vec{};
int n = 0;
while(in_iter != eof){
vec.push_back(*in_iter++);
n++;
}
sort(vec.begin(), vec.end());
ostream_iterator out_iter(cout, " ");
unique_copy(vec.begin(), vec.end(), out_iter);
// 9 8 7 6 5 4 3 2 1
// 9 8 7 6 5 4 3 2 1
// ^Z
// 1 2 3 4 5 6 7 8 9
10.32:
略 哈哈哈哈
10.33:
fstream fstrm1("1.txt");
istream_iterator in_iter(fstrm1), eof;
vector vec2{};
vector vec3{};
while(in_iter != eof){
if(*in_iter % 2 == 0){
vec2.push_back(*in_iter++);
}
else {
vec3.push_back(*in_iter++);
}
}
fstream fstrm2("2.txt");
fstream fstrm3("3.txt");
ostream_iterator out_iter2(fstrm2, "\n");
ostream_iterator out_iter3(fstrm3, " ");
copy(vec2.begin(), vec2.end(), out_iter2);
copy(vec3.begin(), vec3.end(), out_iter3);
10.34:
vector vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto r_iter = vec.crbegin(); r_iter != vec.crend(); ++r_iter) {
cout << *r_iter << " ";
}
// 9 8 7 6 5 4 3 2 1 0
10.35:
vector vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto r_iter = vec.cend(); r_iter != vec.cbegin(); --r_iter) {
cout << *(r_iter - 1) << " ";
}
// 9 8 7 6 5 4 3 2 1 0
10.36:
list listi = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
auto iter = find(listi.rbegin(), listi.rend(), 0);
if(iter == listi.rbegin()){
cout << *iter << endl;
}
10.37:
vector vec = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
list lis(vec.rbegin() + 3, vec.rbegin() + 8);
for(const auto& x : lis){
cout << x << " ";
}
// 6 5 4 3 2
10.38:
略。
10.39:
list是双向迭代器,vector是随机迭代器。
10.40:
copy第一二两个参数是输入迭代器,第三个参数是输出迭代器。
reverse是双向迭代器。
unique是前向迭代器。
没有很理解,抄的答案。
10.41:
遍历beg到end,找到old_val就用new_val替换;
遍历beg到end,找到令pred为真的就用new_val替换;
遍历beg到end,找到old_val就用new_val替换,并将其拷贝至dest;
遍历beg到end,找到令pred为真的就用new_val替换,并将其拷贝至dest;
10.42:
list words = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
words.sort();
words.unique();
for(const auto& i : words){
cout << i << " ";
}
cout << endl;
// fox jumps over quick red slow the turtle