C++ Primer第5版 第十章课后练习答案
合集《C++ Primer第5版》 课后练习答案 - 丸子球球 - 博客园 (cnblogs.com)
练习10.1
int main(int argc, char* argv[])
{
vector<int> vec;
int i = 0;
cout << "请输入一个int序列" << endl;
while (cin >> i) {
*back_inserter(vec) = i;
}
cout << "给定的查找值10:";
cout << endl << "出现次数为" << count(vec.begin(), vec.end(), 10);
return 0;
}
练习10.2
int main(int argc, char* argv[])
{
list<string> vec;
string i;
cout << "请输入一个string序列" << endl;
while (cin >> i) {
*back_inserter(vec) = i;
}
cout << "给定的查找值abc:";
cout << endl << "出现次数为" << count(vec.begin(), vec.end(), "abc");
return 0;
}
练习10.3
int main(int argc, char* argv[])
{
vector<int> vec;
int i = 0;
cout << "请输入一个int序列" << endl;
while (cin >> i) {
*back_inserter(vec) = i;
}
cout << "容器元素之和为" << accumulate(vec.begin(), vec.end(), 0);
return 0;
}
练习10.4
int main(int argc, char* argv[])
{
vector<double> vec;
double i = 0;
cout << "请输入一个double序列" << endl;
while (cin >> i) {
*back_inserter(vec) = i;
}
cout << "容器元素之和为" << accumulate(vec.begin(), vec.end(), 0);//函数返回值是int类型,丢失精度
return 0;
}
练习10.5
equal间元素的比较使用的是符号==
,而字符串之间的==
一般(注意是一般,这个有时候特殊)都是比较字符串地址,因此哪怕比较的两个元素相等但是字符串地址不同也会出现失败
练习10.6
int main(int argc, char* argv[])
{
vector<int> vec(10, 6);
fill_n(vec.begin(), vec.size(), 0);
for (const auto& i : vec) {
cout << i << " ";
}
return 0;
}
练习10.7
(a)vec不一定包含和lst同样多的元素
int main(int argc, char* argv[])
{
vector<int> vec; list<int>lst; int i;
while (cin>>i)
lst.push_back(i);
vec.resize(lst.size());
copy(lst.begin(), lst.end(), vec.begin());
return 0;
}
(b)reserve分配的是内存空间大小而不是元素数量
int main(int argc, char* argv[])
{
vector<int> vec;
vec.resize(10);
fill_n(vec.begin(), 10, 0);
return 0;
}
练习10.8
back_inserter使用的是插入迭代器而不是执行容器的操作,因此算法本身永远不会改变底层容器的大小
练习10.9
void elimDups(vector<string>& words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
练习10.10
算法本身永远不会改变底层容器的大小
练习10.11
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(int argc, char* argv[])
{
vector<string> words{ "12","123","34","12" };
elimDups(words);
stable_sort(words.begin(), words.end(), isShorter);
for (const auto& s : words)
cout << s << " ";
cout << endl;
return 0;
}
练习10.12
bool compareIsbn(const Sales_data& sd1, const Sales_data& sd2) {
return sd1.isbn().compare(sd2.isbn()) > 0;
}
int main(int argc, char* argv[])
{
vector<Sales_data> sale_vec;
string bookNo = "";
for (int i = 0; i < 5; i++) {
bookNo += "s";
cout << bookNo << endl;
sale_vec.push_back(Sales_data(bookNo));
}
for (auto i : sale_vec) {
print(cout, i);
}
sort(sale_vec.begin(), sale_vec.end(), compareIsbn);
cout << "排序之后结果" << endl;
for (auto i : sale_vec) {
print(cout, i);
}
return 0;
}
练习10.13
int main(int argc, char* argv[])
{
string a = "a";
vector<string> str_vec(10);
for (auto& i : str_vec) {
i = a;
cout << i<<" ";
a += "a";
}
cout << endl << endl;
partition(str_vec.begin(), str_vec.end(), strThan5);
for (auto& i : str_vec) {
cout << i << " ";
}
return 0;
}
练习10.14
auto sum = [](
练习10.15
int main(int argc, char* argv[])
{
int i, j;
cin >> i;
auto sum1 = [i](const int& i1) {return i + i1; };
cout << sum1(j);
return 0;
}
练习10.16
void biggies(vector<string>& words, size_t sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string& str1, const string& str2) { return (str1.size() >= str2.size()); });
auto wc = partition(words.begin(), words.end(), [sz](const string& str) { return str.size() > sz; });
for_each(words.begin(), wc, [](const string& s) {cout << s << " "; });
}
练习10.17
int main(int argc, char* argv[])
{
vector<Sales_data> sale_vec;
string bookNo = "";
for (int i = 0; i < 5; i++) {
bookNo += "s";
cout << bookNo << endl;
sale_vec.push_back(Sales_data(bookNo));
}
for (auto i : sale_vec) {
print(cout, i);
}
sort(sale_vec.begin(), sale_vec.end(), [](const Sales_data& sd1, const Sales_data& sd2) {return sd1.isbn().compare(sd2.isbn()) > 0; });
cout << "排序之后结果" << endl;
for (auto i : sale_vec) {
print(cout, i);
}
return 0;
}
练习10.18
void biggies(vector<string>& words, size_t sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string& str1, const string& str2) { return (str1.size() >= str2.size()); });
auto wc = partition(words.begin(), words.end(), [sz](const string& str) { return str.size() > sz; });
for_each(words.begin(), wc, [](const string& s) {cout << s << " "; });
}
练习10.19
void biggies(vector<string>& words, size_t sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string& str1, const string& str2) { return (str1.size() >= str2.size()); });
auto wc = stable_partition(words.begin(), words.end(), [sz](const string& str) { return str.size() > sz; });
for_each(words.begin(), wc, [](const string& s) {cout << s << " "; });
}
练习10.20
int main(int argc, char* argv[])
{
string a = "a";
vector<string> str_vec(10);
for (auto& i : str_vec) {
i = a;
cout << i << " ";
a += "a";
}
cout << endl << count_if(str_vec.begin(), str_vec.end(), [](string& s) {return s.size() > 6; }) << "个单词长度超过6位";
return 0;
}
练习10.21
int main(int argc, char* argv[])
{
int num = 5;
auto sub = [&num]() ->bool {num == 0 ? num : --num;return !num;};
for (auto i = 0; i < 6; i++) {
cout << sub() << endl;
}
return 0;
}
练习10.22
bool strThan6(const string& s) {
return s.size() > 6;
}
int main(int argc, char* argv[])
{
string a = "a";
vector<string> str_vec(10);
for (auto& i : str_vec) {
i = a;
cout << i << " ";
a += "a";
}
cout << endl << count_if(str_vec.begin(), str_vec.end(), bind(strThan6,_1)) << "个单词长度超过6位";
return 0;
}
练习10.23
上文中调用两个,第一个是可调用函数,第二个是传递的参数
练习10.24
bool check_size(const string& s, string::size_type sz) {
return s.size() >= sz;
}
int main(int argc, char* argv[])
{
string a = "a";
string com = "abcdef";
vector<string> str_vec(10);
for (auto& i : str_vec) {
i = a;
cout << i << " ";
a += "a";
}
cout << endl << "第一个长度大于" << com << "的单词是" << *find_if(str_vec.begin(), str_vec.end(), bind(check_size, _1, com.size()));
return 0;
}
练习10.25
void biggies(vector<string>& words, size_t sz)
{
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string& str1, const string& str2) { return (str1.size() >= str2.size()); });
auto wc = stable_partition(words.begin(), words.end(), bind(check_size, _1, sz));
for_each(words.begin(), wc, [](const string& s) {cout << s << " "; });
}
练习10.26
back_inserter:创建一个使用push_back的迭代器
front_inserter:创建一个使用push_front的迭代器
inserter:创建一个使用insert的迭代器,因此支持插入到给定迭代器所表示的元素之前
练习10.27
int main(int argc, char* argv[])
{
vector<string> str_vec(10,"a");
list<string> lst(10);
unique_copy(str_vec.begin(), str_vec.end(), lst.begin());
return 0;
}
练习10.28
int main(int argc, char* argv[])
{
vector<int> ivec{ 1,2,3,4,5,6,7,8,9 }, ivec_i, ivec_b;
list<int> ivec_f;
copy(ivec.begin(), ivec.end(), inserter(ivec_i, ivec_i.begin())); //{1,2,3,4,5,6,7,8,9}
copy(ivec.begin(), ivec.end(), back_inserter(ivec_b));//{1,2,3,4,5,6,7,8,9}
copy(ivec.begin(), ivec.end(), front_inserter(ivec_f));//{9,8,7,6,5,4,3,2,1}
return 0;
}
练习10.29
int main(int argc, char* argv[])
{
ifstream ifs(argv[1]);
std::istream_iterator<string> item_iter(ifs), eof;
vector<string> str_vec;
while (item_iter != eof) {
back_inserter(str_vec) = *item_iter++;
}
}
练习10.30
int main(int argc, char* argv[])
{
std::istream_iterator<int> item_iter(cin), eof;
vector<int> ivec;
copy(item_iter, eof, back_inserter(ivec));
sort(ivec.begin(), ivec.end());
for (const auto& i : ivec) {
cout << i << " ";
}
}
练习10.31
int main(int argc, char* argv[])
{
std::istream_iterator<int> item_iter(cin), eof;
vector<int> ivec;
unique_copy(item_iter, eof, back_inserter(ivec));
sort(ivec.begin(), ivec.end());
for (const auto& i : ivec) {
cout << i << " ";
}
}
练习10.32
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{
int main(int argc, char* argv[])
{
std::istream_iterator<Sales_item> item_iter(cin), eof;
vector<Sales_item> si_vec(item_iter, eof);
sort(si_vec.begin(), si_vec.end(), compareIsbn);
auto beg = si_vec.begin(), end = si_vec.end();
while (beg != end) {
auto temp = beg;
beg = find_if(beg, end,
[temp](const Sales_item& s) {return s.isbn() != (*temp).isbn(); }); //找到第一个和前一个书籍编号不一样的书籍迭代器
auto iter = beg - 1; //temp到iter 即为相同书籍编号的区间
auto sum = accumulate(temp, iter, *temp);
cout << sum << endl; //打印该区间书籍销售记录
}
return 0;
}
练习10.33
int main(int argc, char* argv[])
{
ifstream ifs(argv[1]);
ofstream ofs1(argv[2]), ofs2(argv[3]);
istream_iterator<int> item_iter(ifs), eof;
ostream_iterator<int> out_iter1(ofs1, " "), out_iter2(ofs2, "\n");
while (item_iter != eof) {
if (*item_iter % 2) {
*out_iter1++ = *item_iter;
}
else {
*out_iter2++ = *item_iter;
}
++item_iter;
}
return 0;
}
练习10.34
int main(int argc, char* argv[])
{
vector<int> ivec{ 1,2,3,4,5,6,7,8,9 };
for_each(ivec.crbegin(), ivec.crend(), [](const int& i) {cout << i << " "; });
}
练习10.35
int main(int argc, char* argv[])
{
vector<int> ivec{ 1,2,3,4,5,6,7,8,9 };
auto it = ivec.cend();
while (--it > ivec.cbegin()) {
cout << *it << " ";
}
cout << *ivec.cbegin();
}
练习10.36
int main(int argc, char* argv[])
{
vector<int> ivec{ 0,1,2,3,4,5,6,0,7,8,9 };
auto it=find(ivec.crbegin(), ivec.crend(), 0);
cout << "第" << ivec.crend() - it << "位";
}
练习10.37
int main(int argc, char* argv[])
{
vector<int> ivec{ 0,1,2,3,4,5,6,7,8,9 };
list<int> ilst(5);
copy(ivec.cbegin()+2, ivec.cbegin() + 7, ilst.rbegin());
for_each(ilst.cbegin(), ilst.cend(), [](const int& i) {cout << i << " "; });
}
练习10.38
输入迭代器:只读,一次传递
输出迭代器:对输入迭代器的补充,但是是只写而不是只读,一次传递
前向迭代器:包含了输入和输出迭代器两者的功能,多次读/写
双向迭代器:具有前向迭代器的全部功能。另外它还可以利用自减操作符operator--向后一次移动一个位置
随机访问迭代器:具有双向迭代器的所有功能,再加上一个指针所有的功能(除了没有和空指针对应的空迭代器)
练习10.39
list上的迭代器属于双向迭代器,vector上的迭代器属于随机访问迭代器
练习10.40
copy要求一个输入迭代器和一个输出迭代器,reverse要求一个双向迭代器,unique要求一个前向迭代器
练习10.41
replace(beg, end, old_val, new_val);//替换[beg, end)范围内所有的old_val为new_val;
replace_if(beg, end, pred, new_val);
replace_copy(beg, end, dest, old_val, new_val);//替换[beg, end)范围内所有的old_val值为new_val并copy到dest中,原来的不变;
replace_copy_if(beg, end, dest, pred, new_val);
练习10.42
void elimDups(list<string>& words)
{
words.sort();
words.unique();
}