习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第10章 泛型算法


练习10.1

#include<iostream>
#include<algorithm>
#include<vector>using namespace std;int main() {int t, n;vector<int> vec;cout << "请输入序列个数:" << endl;cin >> n;cout << "请输入序列:" << endl;for (int i = 0;i < n;i++) {cin >> t;vec.push_back(t);}cout << "请输入要统计的值:" << endl;int num;cin >> num;cout << count(vec.begin(), vec.end(), num) << endl;system("pause");return 0;
}

练习10.2

#include<iostream>
#include<algorithm>
#include<list>
#include<string>using namespace std;int main() {int n;string t;list<string> lst;cout << "请输入字符串个数:" << endl;cin >> n;cout << "请输入" << n << "个字符串:" << endl;for (int i = 0;i < n;i++) {cin >> t;lst.push_back(t);}cout << "请输入要统计的字符串:" << endl;string num;cin >> num;cout << count(lst.begin(), lst.end(), num) << endl;system("pause");return 0;
}

练习10.3

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

练习10.4
初始值设为0表示返回值为int类型,会有精度损失

练习10.5
equal会比较指针地址,而不是字符串值,比较的结果与string类型的不一致。

练习10.6

fill_n(vec.begin(),vec.size(),0);

练习10.7
a.lst和vec之间的大小未保证相同,vec.resize(lst.size)
b.vec.resize(10);

练习10.9

#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;void elimDups(vector<string> &words) {sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());cout << "unique后:";for (auto i : words) {cout << i << " ";}cout << endl;cout << "erase后:";words.erase(end_unique, words.end());for (auto i : words) {cout << i << " ";}cout << endl;
}int main() {vector<string> words = { "abc","abc","abc","bcd","efg","bcd","eqd" };elimDups(words);system("pause");return 0;
}

练习10.10
标准库算法对迭代器而不是容器进行操作。

练习10.11

#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;bool isShorter(const string &s1, const string &s2) {return s1.size() < s2.size();
}void elimDups(vector<string> &words) {sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());words.erase(end_unique, words.end());
}int main() {vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };elimDups(words);stable_sort(words.begin(), words.end(), isShorter);for (auto &i : words) {cout << i << " ";}cout << endl;system("pause");return 0;
}

练习10.12

bool compareIsbn(const Sales_data &a, const Sales_data &b) {return a.isbn() < b.isbn();
}

练习10.13

#include<iostream>
#include<numeric>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;bool cmp(const string &a) {return a.size() >= 5;
}int main() {vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };partition(words.begin(), words.end(), cmp);for (auto &i : words) {cout << i << " ";}cout << endl;system("pause");return 0;
}

练习10.14

#include<iostream>
using namespace std;
int main() {auto f = [](int a,int b) {return a + b;};cout << f(1, 2) << endl;return 0;
}

练习10.15

#include<iostream>
using namespace std;int main() {int a = 1;auto f = [a](int b) {return a + b;};cout << f(2) << endl;system("pause");return 0;
}

练习10.16

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>using namespace std;vector<string> &elimDups(vector<string> &words)
{sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());words.erase(end_unique, words.end());return words;
}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 = find_if(words.begin(), words.end(),[sz](const string &a){ return a.size() >= sz; });auto count = words.end() - wc;cout << count << endl;for(const auto s : words)cout << s << " ";cout << endl;
}int main()
{vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };biggies(vs, 5);return 0;
}

练习 10.18-10.19

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>using namespace std;vector<string> &elimDups(vector<string> &words)
{sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());words.erase(end_unique, words.end());return words;
}void biggies(vector<string> &words, vector<string>::size_type sz)
{elimDups(words);auto wc = partition(words.begin(), words.end(),[sz](const string &a){ return a.size() >= sz; });//auto wc = stable_partition(words.begin(), words.end(),
//[sz](const string &a)
//{ return a.size() >= sz; });auto count = words.end() - wc;cout << count << endl;for (const auto s : words)cout << s << " ";cout << endl;
}int main()
{vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };biggies(vs, 5);system("pause");return 0;
}

练习10.20

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>using namespace std;int main()
{vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };string::size_type sz = 6;auto wc = count_if(words.begin(), words.end(),[sz](const string &a){ return a.size() >= sz; });cout << wc << endl;system("pause");return 0;
}

练习10.21

#include<iostream>
#include<algorithm>using namespace std;int main() {int v = 5;auto f = [&v]()->bool{if (v <= 0) return false;else {--v;return true;}};while (f()) {cout << v << endl;}system("pause");return 0;
}

练习10.22

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<functional>using namespace std;bool judge_size(string &s, string::size_type sz) {return s.size() >= sz;
}int main() {vector<string> words = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };cout << count_if(words.begin(), words.end(), bind(judge_size, placeholders::_1, 6)) << endl;system("pause");return 0;
}

练习10.23

假设要绑定的函数有n个参数,绑定取n + 1个参数。另外一个是函数本身的绑定。

练习10.24

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>using namespace std;
bool check_size(string &s, int sz)
{return s.size() < sz;
}int main()
{vector<int> vi = { 1,2,3,4,5,6 };string s("aaaa");auto iter = find_if(vi.begin(), vi.end(), bind(check_size, s, placeholders::_1));cout << *iter << endl;system("pause");return 0;
}

练习10.25

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>using namespace std;vector<string> &elimDups(vector<string> &words)
{sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());words.erase(end_unique, words.end());return words;
}bool check_size(const string &s, string::size_type sz)
{return s.size() >= sz;
}int main()
{vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };auto iter = partition(vs.begin(), vs.end(), bind(check_size, placeholders::_1, 5));for (const auto s : vs)cout << s << " ";cout << endl;vs.erase(iter, vs.end());for (const auto s : vs)cout << s << " ";cout << endl;system("pause");return 0;
}

后续部分之后再更新。。

转载于:https://www.cnblogs.com/Mered1th/p/10574615.html

C++Primer第五版——习题答案详解(九)相关推荐

  1. C++Primer第五版——习题答案+详解(完整版)

     C++Primer第五版--习题答案详解 新手入门必看的书.知识是一个系统化并且相互关联的体系,零散的东西每天收获如果不形成自己的体系的话,那将是毫无意义的,所以我觉得有必要将这本书先啃一遍,消化其 ...

  2. C++Primer第五版——习题答案详解

     C++Primer第五版--习题答案详解 新手入门必看的书.知识是一个系统化并且相互关联的体系,零散的东西每天收获如果不形成自己的体系的话,那将是毫无意义的,所以我觉得有必要将这本书先啃一遍,消化其 ...

  3. C++Primer第五版——习题答案详解(六)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public:std::s ...

  4. C++Primer第五版 习题答案 目录

    C++Primer第五版 习题答案 本文当作我学习C++的一个记录,这里的习题答案并不是标准答案,仅仅是我的理解,如有错误,请大家指出,谢谢! 参考的文章会在文末列出. 使用的系统为:ubuntu 1 ...

  5. C++ primer 第五版习题答案, Stanley B. Lippman( 斯坦利 李普曼)(持续更新中)

    最新重新看c++ primer 5 第五版本,看到网上很多人在找答案,而这本书是2013年9月份的样子出来的,网上肯定是没有标准答案的,而很多人冒天下之大不韪用c plus plus的答案来来骗取分数 ...

  6. C++Primer第五版 习题答案 第十二章 动态内存

    12.1 b1有四个元素: b2被销毁: 12.2 strBlob.h #ifndef STRBLOB_H_ #define STRBLOB_H_#include<string> #inc ...

  7. 《Java 2实用教程》(第5版)(清华大学出版社)作者:张跃平、耿祥义习题答案详解

    <Java 2实用教程>(第5版)(清华大学出版社)作者:张跃平.耿祥义习题答案详解 **此答案与详解是本人做作业时所写部分答案,如有错误之处请指出 ** 习题2 1.问答题 (3) 逻辑 ...

  8. 计算机组成原理中计算机主要包括哪几部分,计算机组成原理本科生期末试卷(五)部分答案详解...

    <计算机组成原理本科生期末试卷(五)部分答案详解>由会员分享,可在线阅读,更多相关<计算机组成原理本科生期末试卷(五)部分答案详解(3页珍藏版)>请在人人文库网上搜索. 1.计 ...

  9. C++ Primer第五版习题集答案

    学习了C++ Primer一段时间,老是看别人的答案,觉得好像啥也没学到.so 决定从现在开始写. 已经看到101页了,就从101页开始把答案贴在这里吧. 参照博客https://blog.csdn. ...

最新文章

  1. 前端每日实战:116# 视频演示如何用 CSS 和原生 JS 开发一个监控网络连接状态的页面...
  2. 天津大学仁爱学院c语言期末考试题,天津大学《C语言程序设计》2016年7月考试期末大作业...
  3. (十三)react hooks
  4. oracle ora 16014,ORACLE ORA-16014+ORA-00312 数据库在线重做日志没有归档
  5. MyBatis 配制文件层次表
  6. 程序设计入门-C语言基础知识-翁恺-第六周:数组-详细笔记(六)
  7. HTML5中的webSocket、ajax、http
  8. SpringMVC异常处理 自定义异常
  9. sharepoint2013的审核日志的时间区域设置
  10. linux rsync 目录同步,linux下使用rsync同步目录
  11. iOS 银行卡号识别
  12. 【Windows远程连接】登录显示密码过期
  13. 使用python+ffmpeg批量将视频水平翻转
  14. 【verbs】IBV_WR API(3) Libibverbs Programmer’s Manual
  15. OKCC坐席号和分机号有什么区别?
  16. sicily 1209
  17. 微信小程序获取当前日期及时间
  18. php 递归无限极分类和层级展示(适用于权限管理和分类管理功能)
  19. vs2019运行程序提示 程序无法正常启动(0xc000007b)解决方案
  20. 数学老师用数学课件制作工具快速三等分线段

热门文章

  1. mongo shell连接到mongoDB及shell提示符下执行js脚本
  2. Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)
  3. Linux命令执行顺序— ||和和; 比较
  4. python 操作mysql数据库
  5. rhel5.5下安装awstats实现网站流量监控
  6. QQ卡机问题解决方法
  7. The proctime attribute ‘rowtime‘ must not replace an existing field.
  8. Pycharm中配置Pyflink
  9. 云主机superset接入redis缓存
  10. 基于基站定位数据的商圈分析代码详细解释