部分习题解答:
10.1:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vec;
int word, val = 42;
cout << "input a sequence: " << endl;
while (cin >> word)
{
vec.push_back(word);
}
int cnt = count(vec.begin(), vec.end(), val);
cout << "The value comes out " << cnt << " times." << endl;
system("pause");
return 0;
}
10.3:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<int> vec;
int word;
cout << "input a sequence: " << endl;
while (cin >> word)
{
vec.push_back(word);
}
int sum = accumulate(vec.begin(), vec.end(), 0);
cout << "所有元素的和为:" << sum << endl;
system("pause");
return 0;
}
10.4:
将初值设为0,表明返回类型是int,使用之后,将double转化为int,损失精度
10.5:
equal使用==运算符比较两个序列中的元素。string类重载了==,可比较两个字符串是否长度相等且其中元素对位相等。而C风格字符串本质是char *类型,用==比较两个char *对象,只是检查两个指针值是否相等,即地址是否相等,而不会比较其中字符是否相同。所以,只有当两个序列中的指针都指向相同的地址时,equal才会返回true,否则,即使字符串内容完全相同,也会返回false。
10.6:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
vector<int> vec;
int word;
cout << "input a sequence: " << endl;
while (cin >> word)
{
vec.push_back(word);
}
cout << "修改前的元素值为:" << endl;
for (auto c : vec)
{
cout << c << " ";
}
fill_n(vec.begin(),vec.size(), 0); //注意fill_n算法只传入一个迭代器
cout<<endl << "修改后的元素值为:" << endl;
for (auto c : vec)
{
cout << c << " ";
}
system("pause");
return 0;
}
10.7:
(a)有错误,copy算法要求目标序列至少要包含与源序列一样多的元素,而此程序中,vec进行缺省初始化,它是空的,copy无法进行。可以将第三个参数改为back_inserter(vec),通过它,copy算法即可将lst中的元素拷贝插入到lst的末尾。
(b)也有错误。此时,vec仍然为空,没有任何元素。而算法又不具备向容器添加元素的能力,因此fill_n仍然失败。这里还是将第一个参数改成back_inserter(vec)来让fill_n有能力向vec添加元素。
10.8:
算法只是产生了一个插入迭代器,然后使用该迭代器进行插入操作
10.9:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
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> vec;
string word;
while (cin >> word)
{
vec.push_back(word);
}
elimDups(vec);
for (auto c : vec)
{
cout << c << " ";
}
system("pause");
return 0;
}
10.11:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
void elimDups(vector<string> &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<string> vec{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
elimDups(vec);
stable_sort(vec.begin(), vec.end(), isShorter); //使相同长度字符串按字典序排列
for (auto &c : vec)
{
cout << c << " ";
}
system("pause");
return 0;
}
10.12:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#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)
{
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{ a,b,c,d,e,f };
stable_sort(vec1.begin(), vec1.end(), compareIsbn);//排序
cout << "排序后的vector:" << endl;
for (int i = 0; i < vec1.size(); ++i)
{
cout << vec1[i].isbn() << " ";
}
system("pause");
return 0;
}
10.13:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
bool divide(string &s)
{
return s.size() < 5;
}
int main(int argc, char**argv)
{
vector<string> words;
string word;
cout << "输入划分的序列:" << endl;
while (cin >> word)
{
words.push_back(word);
}
auto it = partition(words.begin(),words.end(),divide);
cout << "输出长度大于等于5的元素:" << endl;
for (; it != words.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
system("pause");
return 0;
}
10.14:
[] (int &a,int &b) {return a+b;}
10.15:
[a] (int &b) {return a+b;}
10.16:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
void elimDups(vector<string> &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<string> &words,
vector<string>::size_type sz)
{
elimDups(words); //将words按字典序排序,删除重复单词
//按长度排序,长度相同的单词维持字典序
stable_sort(words.begin(), words.end(),
[](const string&a, const string &b)
{return a.size() < b.size(); });
//获取一个迭代器,指向第一个满足size()>=sz的元素
auto wc = find_if(words.begin(), words.end(),
[sz](const string &a)
{return a.size() >= sz; });
//计算满足size>=sz的元素的数目
auto count = words.end() - wc;
cout << count << " " << make_plural(count, "word", "s")
<< " of length " << sz << " or longer " << endl;
//打印长度大于等于给定值的单词,每个单词后面接一个空格
for_each(wc, words.end(),
[](const string &s) {cout << s << " "; });
}
int main(int argc, char**argv)
{
vector<string> vec{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
string::size_type i = 4;
biggies(vec, i);
system("pause");
return 0;
}
10.18、10.19、10.20:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
void elimDups(vector<string> &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<string> &words,
vector<string>::size_type sz)
{
elimDups(words); //将words按字典序排序,删除重复单词
//按长度排序,长度相同的单词维持字典序
stable_sort(words.begin(), words.end(),
[](const string&a, const string &b)
{return a.size() < b.size(); });
//获取一个迭代器,指向第一个满足size()>=sz的元素
auto wc = partition(words.begin(), words.end(), //10.19将partition改为stable_partition
[sz](const string &a)
{return a.size() >= sz; });
//计算满足size>=sz的元素的数目
auto count = wc - words.begin();
/*10.20改为auto count = count_if(words.begin(), words.end(), //返回满足条件的单词的个数
[sz](const string &a)
{return a.size() >= sz; });*/
cout << count << " " << make_plural(count, "word", "s")
<< " of length " << sz << " or longer " << endl;
//打印长度大于等于给定值的单词,每个单词后面接一个空格
for_each(words.begin(), wc,
[](const string &s) {cout << s << " "; });
}
int main(int argc, char**argv)
{
vector<string> vec{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
string::size_type i = 4;
biggies(vec, i);
system("pause");
return 0;
}
10.21:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
void fcn()
{
int val = 42;
auto f = [&val]()->bool {
if (val != 0)
{
--val;
return 0;
}
else
return 1;};
auto j = f();
cout << j << endl;
}
int main(int argc, char**argv)
{
fcn();
system("pause");
return 0;
}
10.22:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
bool fcn(string &s)
{
return s.size() <= 6;
}
int main(int argc, char**argv)
{
vector<string> vec;
string word;
while (cin >> word)
{
vec.push_back(word);
}
auto cnt = count_if(vec.begin(), vec.end(), fcn);
cout << "长度小于等于6的单词数量有" << cnt << "个" << endl;
system("pause");
return 0;
}
10.23:
第一个参数为可调用对象A,若A接受x个参数,则bind的参数个数应为x+1
10.24:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <functional>
#include <algorithm>
#include <numeric>
using namespace std;
using namespace placeholders;
bool check_size(const string &s, int sz)
{
return s.size() < sz;
}
int main(int argc, char**argv)
{
vector<int> vec;
int word;
cout << "输入目标序列:" << endl;
while (cin >> word)
{
vec.push_back(word);
}
cin.clear();
string s;
cout << "输入比较的字符串:" << endl;
cin >> s;
auto wc = find_if(vec.begin(), vec.end(), bind(check_size,s,_1));
cout << *wc << endl;
system("pause");
return 0;
}
10.25:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
using placeholders::_1;
void elimDups(vector<string> &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;
}
bool check_size(const string &s, int sz)
{
return s.size() >= sz;
}
void biggies(vector<string> &words,
vector<string>::size_type sz)
{
elimDups(words); //将words按字典序排序,删除重复单词
//按长度排序,长度相同的单词维持字典序
stable_sort(words.begin(), words.end(),
[](const string&a, const string &b)
{return a.size() < b.size(); });
//获取一个迭代器,指向第一个满足size()>=sz的元素
auto wc = partition(words.begin(), words.end(), bind(check_size, _1, sz));
//计算满足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 << " "; });
}
int main(int argc, char**argv)
{
vector<string> vec{ "the","quick","red","fox","jumps","over","the","slow","red","turtle" };
string::size_type i = 4;
biggies(vec, i);
system("pause");
return 0;
}
10.27:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
int main(int argc, char**argv)
{
vector<int> vec = { 1,1,2,3,4,4,5,6,7,7,8,9 };
list<int> lst;
unique_copy(vec.begin(),vec.end(),back_inserter(lst));
for (auto &c : lst)
{
cout << c << " ";
}
system("pause");
return 0;
}
10.28:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
vector<int> vec = { 1,2,3,4,5,6,7,8,9 };
list<int> vec1, vec2, vec3;
copy(vec.begin(), vec.end(), back_inserter(vec1));
copy(vec.begin(), vec.end(), front_inserter(vec2));
copy(vec.begin(), vec.end(),inserter(vec3,vec3.begin()));
for (auto &c : vec1) { cout << c << " "; }
cout << endl;
for (auto &c : vec2) { cout << c << " "; }
cout << endl;
for (auto &c : vec3) { cout << c << " "; }
cout << endl;
system("pause");
return 0;
}
10.29:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
ifstream in("10.txt");
istream_iterator<string> str_it(in), eof;
vector<string> vec;
while (str_it != eof)
{
vec.push_back(*str_it++);
}
for (auto &c : vec)
{
cout << c << " ";
}
system("pause");
return 0;
}
10.30、10.31:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
istream_iterator<int> str_it(cin), eof;
vector<int> vec;
while (str_it != eof)
{
vec.push_back(*str_it++);
}
ostream_iterator<int> out(cout, " ");
copy(vec.begin(), vec.end(), out); //将copy改为unique_copy
system("pause");
return 0;
}
10.33:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
ifstream in("10.txt");
istream_iterator<int> str_it(in), eof;
vector<int> vec; //存储读取的数据
while (str_it != eof)
{
vec.push_back(*str_it++);
}
ofstream out1("10.1.txt");
ofstream out2("10.2.txt");
ostream_iterator<int> it1(out1, " ");
ostream_iterator<int> it2(out2, "\n");
for (auto it = vec.begin(); it != vec.end(); ++it)
{
if (*it % 2 != 0)
{
it1 = *it;
}
else
{
it2 = *it;
}
}
system("pause");
return 0;
}
10.34:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
vector<string> vec;
string word;
while (cin >> word)
{
vec.push_back(word);
}
for (auto it = vec.crbegin(); it != vec.crend(); ++it)
{
cout << *it << " ";
}
system("pause");
return 0;
}
10.35:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
vector<string> vec;
string word;
while (cin >> word)
{
vec.push_back(word);
}
auto it = vec.begin();
for ( it = vec.end()-1; it != vec.begin(); --it)
{
cout << *it << " ";
}
cout << *it << endl;
system("pause");
return 0;
}
10.36:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
list<int> lst;
int n;
while (cin >> n)
{
lst.push_back(n);
}
auto it = find(lst.rbegin(), lst.rend(), 0);
if (it != lst.rend())
{
cout << *it << endl;
}
else
{
cout << " 0 is not present" << endl;
}
system("pause");
return 0;
}
10.37:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
vector<int> vec{ 0,1,2,3,4,5,6,7,8,9 };
list<int> lst;
/*for (auto it = vec.rbegin() + 2; it != vec.rend() - 3; ++it)
{
lst.push_back(*it);
}*/
copy(vec.rbegin() + 2, vec.rend() - 3, back_inserter(lst));
for (auto &c : lst)
{
cout << c << " ";
}
system("pause");
return 0;
}
10.42:
#include <iostream>
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int main(int argc, char**argv)
{
list<string> lst;
string word;
while (cin >> word)
{
lst.push_back(word);
}
lst.sort();
lst.unique();
for (auto &c : lst)
{
cout << c << " ";
}
system("pause");
return 0;
}