学习笔记所用
本文部分内容参考了:C++Primer第五版——习题答案+详解(完整版)
10.1
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
vector<int> a = { 1,3,23,41,3,23,3,23 };
auto x = count(a.begin(), a.end(), 23);
cout << x;
return 0;
}
10.2
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;
int main(int argc, char** argv)
{
list<string> ls = { "asd","asd", "asd", "asd", "adsa","asd" };
auto x = count(ls.begin(), ls.end(), "asd");
cout << x;
return 0;
}
10.3
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vi = { 1,2,3,4,5,6,7,8,9 };
int s = accumulate(vi.begin(), vi.end(), 0);
cout << s;
return 0;
}
10.4
没有错误
10.5
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
char r1[] = "123456", r2[] = "123456";
cout << equal(begin(r1), end(r1), begin(r2));
return 0;
}
//一切正常。
10.6
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
vector<int> a = { 1,2,3,4,5,6,7,8,9 };
fill_n(a.begin(), a.size(), 0);
for (auto i : a) cout << i << " ";
return 0;
}
10.7
a:有错误,vec是空的,copy算法要求目的序列至少包含与输入序列一样多的元素.
b:有错误,应该将reserve改为resize,因为reserve并不改变容器中元素的数量,它仅影响vector预先分配多大的内存空间,只改变容器容量,但不改变容器大小。
10.8
back_inserter算法是返回一个与容器绑定的插入迭代器,该算法并没有改变容器的大小,只是通过它得到的插入迭代器可以改变容器大小。
10.9
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
void elimDups(vector<string>& words) {
cout << "排序前:";
for (auto i : words) cout << i << " ";
sort(words.begin(), words.end());
cout << endl << "排序后:";
for (auto i : words) cout << i << " ";
auto end_unique = unique(words.begin(), words.end());
cout << endl << "unique后:";
for (auto i : words) cout << i << " ";
words.erase(end_unique, words.end());
cout << endl << "erase后:";
for (auto i : words) cout << i << " ";
}
int main(int argc, char** argv)
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
elimDups(vs);
return 0;
}
10.10
标准库算法是对迭代器而非容器进行操作。
10.11
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
bool isShorter(const string& s1, const string& s2) {
return s1.size() < s2.size();
}
void elimDups(vector<string>& words) {
cout << "排序前:";
for (auto i : words) cout << i << " ";
sort(words.begin(), words.end());
cout << endl << "排序后:";
for (auto i : words) cout << i << " ";
auto end_unique = unique(words.begin(), words.end());
cout << endl << "unique后:";
for (auto i : words) cout << i << " ";
words.erase(end_unique, words.end());
cout << endl << "erase后:";
for (auto i : words) cout << i << " ";
stable_sort(words.begin(), words.end(), isShorter);
cout << endl << "stable_sort和isShorter后:";
for (auto i : words) cout << i << " ";
}
int main(int argc, char** argv)
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
elimDups(vs);
return 0;
}
10.12
答案来源于C++Primer第五版 第十章习题答案(11~20)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
class Sales_data
{
public:
Sales_data();
Sales_data(string s):_isbn(s)//列表初始化格式(:类内变量名(初始化值),)
{
}
string isbn()
{
return _isbn;
}
string _isbn;
};
bool compareIsbn(Sales_data s1, Sales_data s2)//好像这里不能加const,报错:对象包含与成员函数不兼容的类型限定符
{
return s1.isbn().size() < s2.isbn().size();
}
int main(int argc, char**argv)
{
Sales_data a("because");//初始化对象
Sales_data b("I");
Sales_data c("Like");
Sales_data d("your");
Sales_data e("beautiful");
Sales_data f("eyes");
vector<Sales_data> vec1;//VS2010不支持列表初始化
vec1[0] = a;
vec1[1] = b;
vec1[2] = c;
vec1[3] = d;
vec1[4] = e;
vec1[5] = f;
stable_sort(vec1.begin(),vec1.end(),compareIsbn);//排序
cout<<"排序后的vector:";
for(int i = 0; i < vec1.size(); ++i)
{
cout<<vec1[i].isbn()<<" ";
}
return 0;
}
————————————————
版权声明:本文为CSDN博主「MISAYAONE」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/misayaaaaa/article/details/57396640
10.13
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
//这样长度小于5的会放在前边
bool ld5(const string s) {
return s.size() < 5;
}
int main(int argc, char** argv)
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
auto is = partition(vs.begin(), vs.end(), ld5);
for (; is != vs.end(); is++) {
cout << *is << " ";
}
return 0;
}
10.14
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
auto s = [](const int i1, const int i2) {return i1 + i2; };
cout<<s(1, 2);
return 0;
}
10.15
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
int a = 3;
auto s = [a](const int i1) {return i1 + a; };
cout<<s(2);
return 0;
}
10.16
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
//这样长度小于5的会放在前边
bool ld5(const string s) {
return s.size() < 5;
}
bool isShorter(const string& s1, const string& s2) {
return s1.size() < s2.size();
}
void elimDups(vector<string>& words) {
cout << "排序前:";
for (auto i : words) cout << i << " ";
sort(words.begin(), words.end());
cout << endl << "排序后:";
for (auto i : words) cout << i << " ";
auto end_unique = unique(words.begin(), words.end());
cout << endl << "unique后:";
for (auto i : words) cout << i << " ";
words.erase(end_unique, words.end());
cout << endl << "erase后:";
for (auto i : words) cout << i << " ";
stable_sort(words.begin(), words.end(), isShorter);
cout << endl << "stable_sort和isShorter后:";
for (auto i : words) cout << i << " ";
}
void biggies(vector<string>& words, vector<string>::size_type sz) {
auto bs = find_if(words.begin(), words.end(), [sz](const string s) {return s.size() >= sz; });
cout << endl << "长度大于" << sz << "的数有:";
for (auto i = bs; i != words.end(); i++) cout << *i << " ";
cout << "共有" << words.end() - bs << "个";
}
int main(int argc, char** argv)
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
elimDups(vs);
biggies(vs, 5);
return 0;
}
10.17
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
class Sales_data
{
public:
Sales_data();
Sales_data(string s):_isbn(s)//列表初始化格式(:类内变量名(初始化值),)
{
}
string isbn()
{
return _isbn;
}
string _isbn;
};
bool compareIsbn(Sales_data s1, Sales_data s2)//好像这里不能加const,报错:对象包含与成员函数不兼容的类型限定符
{
return s1.isbn().size() < s2.isbn().size();
}
int main(int argc, char**argv)
{
Sales_data a("because");//初始化对象
Sales_data b("I");
Sales_data c("Like");
Sales_data d("your");
Sales_data e("beautiful");
Sales_data f("eyes");
vector<Sales_data> vec1;//VS2010不支持列表初始化
vec1[0] = a;
vec1[1] = b;
vec1[2] = c;
vec1[3] = d;
vec1[4] = e;
vec1[5] = f;
stable_sort(vec1.begin(),vec1.end(),[](Sales_data s1, Sales_data s2){return s1.isbn().size() < s2.isbn().size();});//排序
cout<<"排序后的vector:";
for(int i = 0; i < vec1.size(); ++i)
{
cout<<vec1[i].isbn()<<" ";
}
return 0;
}
10.18
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
//这样长度小于5的会放在前边
bool ld5(const string s) {
return s.size() < 5;
}
bool isShorter(const string& s1, const string& s2) {
return s1.size() < s2.size();
}
void elimDups(vector<string>& words) {
cout << "排序前:";
for (auto i : words) cout << i << " ";
sort(words.begin(), words.end());
cout << endl << "排序后:";
for (auto i : words) cout << i << " ";
auto end_unique = unique(words.begin(), words.end());
cout << endl << "unique后:";
for (auto i : words) cout << i << " ";
words.erase(end_unique, words.end());
cout << endl << "erase后:";
for (auto i : words) cout << i << " ";
stable_sort(words.begin(), words.end(), isShorter);
cout << endl << "stable_sort和isShorter后:";
for (auto i : words) cout << i << " ";
}
void biggies(vector<string>& words, vector<string>::size_type sz) {
auto bs = partition(words.begin(), words.end(), [sz](const string s) {return s.size() < sz; });
cout << endl << "长度大于" << sz << "的数有:";
for (auto i = bs; i != words.end(); i++) cout << *i << " ";
cout << "共有" << words.end() - bs << "个";
}
int main(int argc, char** argv)
{
vector<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
elimDups(vs);
biggies(vs, 5);
return 0;
}
10.19
将partition改为stable_partition就可以了
10.20
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
vector<string> vs = { "thdadae","qadaduick","reda","fox","jumps","over","the","slow","red","turtlse" };
int le = 6, sc=0;
int cl = count_if(vs.begin(), vs.end(),
[&sc, le](const string& s) {(s.size() > le) ? (++sc) : sc; return sc; }
);
cout<<sc;
return 0;
}
10.21
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
int si = 10;
auto f = [&si]() {if (si < 0) return false; else { --si; return true; } };
int i = 1;
while(f()) {
cout << i << " ";
i++;
}
return 0;
}
10.22
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<functional>
using namespace std;
using namespace placeholders;
bool tj(const string& s, int le, int& sc) {
if (s.size() <= le)
return true;
else return false;
}
int main(int argc, char** argv)
{
vector<string> vs = { "thdadae","qadaduick","reda","fox","jumps","over","the","slow","red","turtlse" };
int le = 5, sc = 0;
auto cj=count_if(vs.begin(), vs.end(),bind(tj,_1,le,sc));
cout << cj;
return 0;
}
10.23
bind接受一个参数。
10.24
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<functional>
using namespace std;
using namespace placeholders;
bool check_size(vector<int>::value_type i, const string& s) {
return (i > s.size()) ? true : false;
}
void fz(vector<int>& vi, const string& s) {
auto b1=bind(check_size, _1, s);
for (int i = 0; i < vi.size(); i++) {
if (b1(vi[i])) { cout << vi[i]; break; }
}
//cout << *ri;
}
int main(int argc, char** argv)
{
string s1 = "adadada";
vector<int> vi = { 1,2,3,5,9,4,7,6 };
fz(vi, s1);
return 0;
}
10.25
主要修改有两点:
一:定义check_size
bool check_size(vector<string>::size_type sz, const string& s) {
return s.size() < sz;
}
二:将原本的auto bs = partition(words.begin(), words.end(), [sz](const string s) {return s.size() < sz; });
auto bs = partition(words.begin(), words.end(),bind(check_size,sz,_1));
10.26
区别:
插入位置不同:
back_inserter 插入到所指容器尾部
front_inserter 插入到容器头部
inserter可以插入到任意位置
使用范围不同,有些容器支持。
调用底层不同,这也是上个不同的原因。
10.27
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
int main(int argc, char** argv)
{
vector<string> vs = { "the","the","quick","red","red","fox","jumps","over","slow","turtle" };
vector<string> ls;
unique_copy(vs.cbegin(), vs.cend(), back_inserter(ls));
for(auto x:ls) cout<<x<<" ";
return 0;
}
//注意,unique_copy只消除相邻的重复元素,不相邻的重复元素不删除
10.28
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vi = { 1,2,3,4,5,6,7,8,9 };
deque<int> ki1, ki2, ki3;
copy(vi.cbegin(), vi.cend(), inserter(ki1, ki1.begin()));
copy(vi.cbegin(), vi.cend(), front_inserter(ki2));
copy(vi.cbegin(), vi.cend(), back_inserter(ki3));
cout << "inserter:";
for (auto i : ki1) cout << i << " ";
cout << endl << "front_inserter:";
for (auto i : ki2) cout << i << " ";
cout << endl << "back_inserter:";
for (auto i : ki3) cout << i << " ";
return 0;
}
10.29
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
vector<string> vs;
ifstream in1("1.txt");
istream_iterator<string> str(in1);
istream_iterator<string> end;
while (str != end) {
vs.push_back(*str);
str++;
}
for (auto i : vs) cout << i << " ";
return 0;
}
10.30
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
istream_iterator<int> is(cin),end;
ostream_iterator<int> oi(cout," ");
vector<int> vi(is, end);
sort(vi.begin(), vi.end());
for (auto i : vi) oi = i;
return 0;
}
10.31
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
istream_iterator<int> is(cin),end;
ostream_iterator<int> oi(cout," ");
vector<int> vi;
unique_copy(is, end, back_inserter(vi));
sort(vi.begin(), vi.end());
//oi = vi;
for (auto i : vi) oi = i;
return 0;
}
10.33
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
ifstream is1("1.txt");
ofstream os1("2.txt"), os2("3.txt");
istream_iterator<int> ii(is1),end;
ostream_iterator<int> oi1(os1," "), oi2(os2," ");
while (ii != end) {
if (*ii % 2 == 1) {
oi1 = *ii;
}
else {
oi2 = *ii;
}
ii++;
}
return 0;
}
10.34
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vi = { 1,2,3,4,5,6,7,8,9 };
for (auto i = vi.rbegin(); i != vi.rend(); i++) {
cout << *i << " ";
}
return 0;
}
10.35
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
vector<int> vi = { 1,2,3,4,5,6,7,8,9 };
for (auto i = vi.size()-1; i >= 0; i--) {
cout << vi[i] << " ";
}
return 0;
}
10.36
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
list<int> vi = { 1,2,0,4,5,6,7,0,8,9 };
auto i = find(vi.rbegin(), vi.rend(), 0);
cout << *(--i);//输出后一个元素,看是那个0
return 0;
}
10.37
#include<iostream>
#include<fstream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<deque>
#include<functional>
using namespace std;
int main(int argc, char** argv)
{
list<int> vi = { 0,1,2,3,4,5,6,7,8,9 };
auto i = find(vi.rbegin(), vi.rend(), 7);
auto j = find(vi.rbegin(), vi.rend(), 2);
while (i != j) {
cout << *i << " ";
i++;
}
return 0;
}
10.38
输入迭代器:只读,不写,单遍扫描,只能递增
输出迭代器:输入迭代器的补集,只写,不读,单遍扫描,只能递增
前向迭代器:可读写,多遍扫描,只能递增
双向迭代器:在前向基础上,还可以递减
随机访问迭代器:支持迭代器的所有操作
10.39
list:双向迭代器
vector:随机访问迭代器
10.40
copy:前两个是输入迭代器,最后一个是输出迭代器
reverse:双向迭代器
unique:前向迭代器
10.41
replace(beg,end,old_val,new_val):将veg-end中值为old_val的元素替换为new_val。
replace_if(beg,end,pred,new_val):将veg-end中使pred成立的元素替换为new_val。
replace_copy(beg,end,dest,old_val,new_val):将veg-end中值为old_val的元素替换为new_val后的结果存储到dest迭代器指向的位置。
replace_copy_if(beg,end,dest,pred,new_val):将veg-end中使pred成立的元素替换为new_val后的结果存储到dest迭代器指向的位置。
10.42
#include<iostream>
#include<list>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iterator>
using namespace std;
void elimDups(list<string>& words) {
cout << "排序前:";
for (auto i : words) cout << i << " ";
words.sort();
cout << endl << "排序后:";
for (auto i : words) cout << i << " ";
words.unique();
cout << endl << "unique后:";
for (auto i : words) cout << i << " ";
}
int main(int argc, char** argv)
{
list<string> vs = { "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
elimDups(vs);
return 0;
}
本章常用标准库算法总结
只读算法:
find()寻找,count()计数,accumulate()求和,equal()判断相等,for_each遍历容器元素
写容器算法
fill()赋值,fill_n()将n个元素赋值给给定容器,copy()拷贝,replace()替换,unique()消除相邻相同的元素
排序算法
sort()排序,stable_sort()稳定排序,partition()满足条件在前不满足在后返回指向最后一个为true的迭代器,