C++Primer第五版——习题答案详解(九)
习题答案目录: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;
}
后续部分之后再更新。。
C++Primer第五版——习题答案详解(九)的更多相关文章
- C++Primer第五版——习题答案详解(一)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- C++Primer第五版——习题答案详解(五)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- C++Primer第五版——习题答案详解(七)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...
- C++Primer第五版——习题答案详解(八)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...
- C++Primer第五版——习题答案详解(十)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...
随机推荐
- [hdu P4334] Trouble
[hdu P4334] Trouble Hassan is in trouble. His mathematics teacher has given him a very difficult pro ...
- 201621123075 week8-集合
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 indexOf中对 ...
- 软工作业No.7 甜美女孩第五周--测试与发布
Alpha版本发布说明 一.功能介绍 本团队所做的多模式自定义2048是用来进行纸牌模式以及正常基础模式版本的2048小游戏的.Alpha版本具有的功能大体如下: 初始界面- 纸牌模式- 基础模式- ...
- kolla-ansible源码分析
一.kolla-ansible 源码的目录结构 kolla-ansible是从kolla项目分离出来的一个可交付的项目,kolla-ansible负责部署容器化的openstack各个服务和基础设施组 ...
- cocos2d-x3.17 整体概述
首先,cocos引擎有三个版本:C++,Lua,Js.其底层代码是由C++编写,通过脚本文件绑定到Lua与Js,所以我们之后解析的都是cocos2d-x.其次,cocos安装等就不概述了,百度一大堆. ...
- RHEL 6 和 RHEL 7 的一些有关运行级别,服务管理,服务启动等方面的区别介绍
systemd是7中的新命令组,集成了service和chkconfig的功能.system命令可参考:https://www.cnblogs.com/ray-bk/p/10415173.html 运 ...
- 解决android 9上无法使用http协议
用户反应本来好用的app,突然无法访问服务器,不能正常用了,拿到手机,从头检查权限,重新安装都不能解决,网络是正常的,怎么就不能访问网络了呢?所有想到的办法都用了而不能解决,最后想起看一下androi ...
- 剑指Offer 38. 二叉树的深度 (二叉树)
题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 题目地址 https://www.nowcoder.com/prac ...
- «面向对象程序设计(java)»第三周学习总结 周强 201771010141
实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3)掌握Java语言构造基本程序语法知识(ch1-ch3): (4)利用已 ...
- 神州数码OSPF路由协议
实验要求:熟练掌握OSPF配置方法 拓扑如下 R1 enable 进入特权模式 config 进入全局模式 hostname R1 修改名称 interface s0/1 进入端口 ip addres ...