将就着看吧
#include <iostream>
#include <fstream>
#include <iterator>
#include <list>
#include <algorithm>
#include <functional>
#include <time.h>
using namespace std;
using namespace std::placeholders;
/* 10.11所用函数 */
inline bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
/* 10.12所用简化Sales_data类 */
class Sales_data
{
public:
Sales_data() = default;
Sales_data(const string &s) : bookNo(s) { }
string isbn() const { return bookNo; }
private:
string bookNo;
};
/* 10.12所用bookNo比较函数 */
inline bool isbnShorter(const Sales_data &b1, const Sales_data &b2)
{
return b1.isbn() < b2.isbn();
}
/* 10.13所用函数 */
inline bool longer_than_5(string &s)
{
return s.size() > 5;
}
/* 10.22所用函数 */
bool longer(const string &s, size_t sz) { return s.size() > sz; }
/* 10.24 10.25所用函数 */
bool check_siaz(const string &s, size_t sz)
{
return s.size() >= sz;
}
int main()
{
cout << "C++ Primer 第十章所有练习答案。" << endl;
/*
*10.1
*查找特定数字在vector中一共出现了几次
int num;
vector<int> a;
while(cin >> num)
a.push_back(num);
cout << "10一共出现了" << count(a.cbegin(), a.cend(), 10) << "次" <<endl;
*/
/*
*10.2
*查找特定string在list中一共出现了几次
string str;
list<string> a;
while(cin >> str)
a.push_back(str);
cout << "我爱你一共出现了" << count(a.cbegin(), a.cend(), "我爱你") << "次" << endl;
*/
/*
*10.3
*求一个vector中int之和
int num;
vector<int> a;
while(cin >> num)
a.push_back(num);
cout << "数字之和为" << accumulate(a.cbegin(), a.cend(), 0) << endl;
*/
/*10.4
*提出错误
求和的初始值为0,默认为int类型,会导致精度丢失,应该用0.0
*/
/*10.4
*会怎么样
会报错,因为C风格字符串没有迭代器,应该用cbegin()和cend()函数返回的指针
*/
/*
*10.6
*使用fill_n将序列中的int改为0
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fill_n(begin(a), 10, 0);
for(auto num : a)
cout << num << " ";
*/
/*
*10.7
*改错
vector<int> vec;
list<int> lst;
int i;
while(cin >> i)
lst.push_back(i);
copy(vec.begin(), vec.end(), back_inserter(lst));
for(auto num : lst)
cout << num << " ";
vec.clear();
vec.reserve(10);
fill_n(back_inserter(vec), 10, 0);
for(auto num : vec)
cout << num << " ";
*/
/*
*10.8
因为改变容器大小的是插入迭代器,并不是泛型算法
*/
/*
*10.9
*排序,并删除重复元素
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end());
for(auto name : a)
cout << name << " ";
cout << endl;
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
for(auto name : a)
cout << name << " ";
*/
/*
*10.9
因为我们使用算法的时候使用的是迭代器或指向元素的指针,算法并不不知道容器的实际状况,它们只需要提取迭代器或指针范围内的元素就够了
*/
/*
*10.11
*在10.9的基础上再加个长度排序
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end());
for(auto name : a)
cout << name << " ";
cout << endl;
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
for(auto name : a)
cout << name << " ";
cout << endl;
stable_sort(a.begin(), a.end(), isShorter);
for(auto name : a)
cout << name << " ";
*/
/*
*10.12
*根据Sales_data内bookNo排序
vector<Sales_data> a{Sales_data("5"), Sales_data("1"), Sales_data("3"), Sales_data("2"), Sales_data("4")};
sort(a.begin(), a.end(), isbnShorter);
for(auto data : a)
cout << data.isbn() << " ";
*/
/*
*10.13
*打印出向量中大于5的string
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
auto end_str = partition(a.begin(), a.end(), longer_than_5);
for(auto word : vector<string>(a.begin(), end_str))
cout << word << " ";
*/
/*
*10.14
*编写lambda表达式,返回两int之和
int num1, num2;
cin >> num1 >> num2;
auto a = [] (int num1, int num2) {return num1 + num2;};
cout << a(num1, num2);
*/
/*
*10.15
*编写lambda表达式,捕获一个int,接受一个int,返回两int之和
int num1, num2;
cin >> num1 >> num2;
auto a = [num1] (int num2) {return num1 + num2;};
cout << a(num2);
*/
/*
*10.16
*编写bigggies
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end()); //10.9所用排序删除函数
cout << endl;
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
stable_sort(a.begin(), a.end(), [](const string &str1, const string &str2) { return str1.size() < str2.size(); });
cin.clear(); //重置cin
size_t len;
cout << "需要大于几" <<endl;
cin >> len;
vector<string>::iterator wc = find_if(a.begin(), a.end(), [len](const string &str1) { return str1.size() > len; });
if(wc == a.end())
cout << "没找到";
else
for(const auto &s : vector<string>(wc, a.end()))
cout << s << " ";
*/
/*
*10.17
*使用lambda表达式重写10.12
vector<Sales_data> a{Sales_data("5"), Sales_data("1"), Sales_data("3"), Sales_data("2"), Sales_data("4")};
sort(a.begin(), a.end(), [](const Sales_data &s1, const Sales_data &s2) { return s1.isbn() < s2.isbn(); });
for(auto data : a)
cout << data.isbn() << " ";
*/
/*
*10.18
*使用lambda表达式重写10.16
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end()); //10.9所用排序删除函数
cout << endl;
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
stable_sort(a.begin(), a.end(), [](const string &str1, const string &str2) { return str1.size() < str2.size(); });
cin.clear(); //重置cin
size_t len;
cout << "需要大于几" <<endl;
cin >> len;
vector<string>::iterator wc = partition(a.begin(), a.end(), [len](const string &str1) { return str1.size() > len; });
if(wc == a.begin())
cout << "没找到";
else
for(const auto &s : vector<string>(a.begin(), wc))
cout << s << " ";
*/
/*
*10.19
*使用稳定排序算法重写10.18
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end()); //10.9所用排序删除函数
cout << endl;
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
stable_sort(a.begin(), a.end(), [](const string &str1, const string &str2) { return str1.size() < str2.size(); });
cin.clear(); //重置cin
size_t len;
cout << "需要大于几" <<endl;
cin >> len;
vector<string>::iterator wc = stable_partition(a.begin(), a.end(), [len](const string &str1) { return str1.size() > len; });
if(wc == a.begin())
cout << "没找到";
else
for(const auto &s : vector<string>(a.begin(), wc))
cout << s << " ";
*/
/*
*10.20
*统计长度超过6的单词
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
int ct, sz = 6;
ct = count_if(a.begin(), a.end(), [sz](const string &s){ return s.size() > sz; });
cout << "超过6的共有" << ct << "个";
*/
/*
*10.21
*编写lambda表达式递减捕获的int,返回它是否为零
int num;
cin >> num;
auto a = [&num]() -> bool
{
if(!num)
return true;
else
while(num)
--num;
return false;
};
cout << a() << endl;
cout << num;
*/
/*
*10.22
*用bind函数重写10.20
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
int ct;
size_t sz = 6;
auto b = bind(longer, _1, sz);
ct = count_if(a.begin(), a.end(), b);
cout << "超过6的共有" << ct << "个";
*/
/*
*10.23
*bind接受几个参数
准确的说是两个,一个是函数指针,一个是参数列表
*/
/*
*10.24
*用bind查找string的长度
string str("abc");
vector<int> a{9, 8, 7, 6, 5, 4, 3, 2, 1};
auto fun1 = bind(check_siaz, str, _1);
cout << "一共" << *find_if(a.begin(), a.end(), fun1) << "位";
*/
/*
*10.25
*使用bind重写10.19
vector<string> a;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end()); //10.9所用排序删除函数
cout << endl;
auto it = unique(a.begin(), a.end());
a.erase(it, a.end());
stable_sort(a.begin(), a.end(), [](const string &str1, const string &str2) { return str1.size() < str2.size(); });
cin.clear(); //重置cin
size_t len;
cout << "需要大于几" <<endl;
cin >> len;
auto fun1 = bind(check_siaz, _1, len);
vector<string>::iterator wc = find_if(a.begin(), a.end(), fun1);
if(wc == a.end())
cout << "没找到";
else
for(const auto &s : vector<string>(wc, a.end()))
cout << s << " ";
*/
/*
*10.26
*解释不同处
三者都是在操作对本身迭代器执行insert操作,不同点在于插入操作后inserter和back_inserter绑定的迭代器不会变,front_inserter在操作后绑定其插入元素迭代器
*/
/*
*10.27
*用unique_copy将一个vector中不重复的元素拷贝到一个空的list中
vector<string> a;
list<string> b;
string str;
while(cin >> str)
a.push_back(str);
sort(a.begin(), a.end());
cout << endl;
unique_copy(a.begin(), a.end(), back_inserter(b));
for(auto &s : b)
cout << s << " ";
cout << endl;
for(auto &s : b)
cout << s << " ";
*/
/*
*10.28
*将1到9分别用三种插入迭代器添加到三个容器中
vector<int> a{1, 2, 3, 4, 5, 6, 7, 8, 9},
b, c, d;
list<int> _d;
insert_iterator<vector<int>> insb = inserter(b, b.begin());
back_insert_iterator<vector<int>> binsc = back_inserter(c);
//vetcor不支持push_front,此处题目有问题,可以先用list代替
//front_insert_iterator<vector<int>> finsd = front_inserter(d);
front_insert_iterator<list<int>> finsd = front_inserter(_d);
for(auto num : a)
{
insb = num;
binsc = num;
finsd = num;
}
cout << endl << "原向量" << endl;
for(auto num : a)
cout << num << " ";
cout << endl << "inserter" << endl;
for(auto num : b)
cout << num << " ";
cout << endl << "back_inserter" << endl;
for(auto num : c)
cout << num << " ";
cout << endl << "front_inserter" << endl;
for(auto num : _d)
cout << num << " ";
*/
/*
*10.29
*利用流迭代器读取一个文本文件,存入一个vector
ifstream os("a.txt");
istream_iterator<string> os_it(cin), end_it;
vector<string> a;
while(os_it != end_it)
a.push_back(*os_it++);
for(const auto &s : a)
cout << s << endl;
*/
/*
*10.30
*使用流迭代器、sort、copy从标准输入读取一个整数数列,排序并打印在标准输出
istream_iterator<int> in_it(cin), end_it;
ostream_iterator<int> out_it(cout, " ");
vector<int> a(in_it, end_it);
sort(a.begin(), a.end());
copy(a.begin(), a.end(), out_it);
*/
/*
*10.31
*修改上题,删除重复元素
istream_iterator<int> in_it(cin), end_it;
ostream_iterator<int> out_it(cout, " ");
vector<int> a(in_it, end_it);
sort(a.begin(), a.end());
unique_copy(a.begin(), a.end(), out_it);
*/
/*
*10.32
懒得写
*/
/*
*10.33
*接受三个文件从一个人文件读取int序列奇数存入1,偶数存入2
ifstream is("a.txt");
ofstream os1("1.txt"), os2("2.txt");
istream_iterator<int> in_it(is), end_it;
ostream_iterator<int> out_it1(os1, " "),
out_it2(os2, "\n");
while(in_it != end_it)
{
if(*in_it % 2)
*out_it1++ = *in_it; //等价于out_it1 = *in_it
else
*out_it2++ = *in_it;
++in_it;
}
*/
/*
*10.34
*舔狗理想小程序
vector<string> a{"我", "爱", "你"};
for(auto it = a.crbegin(); it != a.crend(); ++it)
cout << *it;
*/
/*
*10.35
*现充的实际生活
vector<string> a{"我", "爱", "你"};
for(auto it = a.cend() - 1; it != a.cbegin() - 1; --it)
cout << *it;
*/
/*
*10.36
*查找list中最后一个为0的元素
list<int> a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i = a.size() + 1;
list<int>::const_reverse_iterator b = find_if(a.crbegin(), a.crend(), [&i](const int num)->bool { --i; return num == 0; });
cout << "位于第" << i<< "位";
*/
/*
*10.37
*将vector中第三至第七个元素逆序拷贝至list中
vector<int> a(10);
for(auto &num : a)
num = &num- &*a.begin() + 1;
list<int> b;
auto begin_it = a.crbegin() + 3;
copy(begin_it, begin_it + 5, back_inserter(b));
for(auto num : b)
cout << num << " ";
*/
/*
*10.38
*五种迭代器支持的操作
输入迭代器
只可输出,不可写入,可递增,不可递减,可解引用(作为右值),可比较两个迭代器是否相等
输出迭代器
只可写入,不可输出,可递增,不可递减,可解引用(作为左值)
前向迭代器
可多次读写元素,只能往一个方向移动,可解引用
双向迭代器
可多次读写元素,可正反两向移动,可解引用
随机访问迭代器
可多次读写元素,可正反两向移动多个距离,可计算两个迭代器之间的距离,可解引用
*/
/*
*10.29
*list和vector分别属于哪种迭代器
list为双向迭代器,vector属于双向迭代器和随机访问迭代器
*/
/*
*10.40
*算法需要的迭代器种类
copy(拷贝算法)第三个参数需要输入迭代器,reverse(翻转算法)需要双向迭代器,uniqe(消除相邻重复元素算法)需要前向迭代器
*/
/*
*10.41
*根据算法和参数的名字,描述执行操作
replace(bed, end, old_val, new_val)
将bed和end范围内的old_val替换为new_val
replace_if(bed, end, pred, new_val)
将bed和end范围内使得pred为真的元素替换为new_val
replace_copy(bed, end, dest, old_val, new_val)
将bed和end范围内的old_val替换为new_val拷贝至dest,原容器不变
replace_copy_if(bed, end, dest, pred, new_val)
将bed和end范围内使得pred为真的元素替换为new_val拷贝至dest,原容器不变
*/
/*
*10.42
*使用list重写10.9程序
list<string> a;
string str;
while(cin >> str)
a.push_back(str);
a.sort();
for(auto name : a)
cout << name << " ";
cout << endl;
a.unique();
for(auto name : a)
cout << name << " ";
*/
}