C++ Primer(第五版) 第十章练习答案
目录
- C++ Primer(第五版) 第十章练习答案
-
-
- 10.1
- 10.2
- 10.3
- 10.4
- 10.5
- 10.6
- 10.7
- 10.8
- 10.9
- 10.10
- 10.11
- 10.12
- 10.13
- 10.14
- 10.15
- 10.16
- 10.17
- 10.18
- 10.19
- 10.20
- 10.21
- 10.22
- 10.23
- 10.24
- 10.25
- 10.26
- 10.27
- 10.28
- 10.29
- 10.30
- 10.31
- 10.32
- 10.33
- 10.34
- 10.35
- 10.36
- 10.37
- 10.38
- 10.39
- 10.40
- 10.41
- 10.42
10.1
#include
#include
#include
using namespace std;
int main()
{
vector<int> vec;
int i;
while (cin >> i)
vec.emplace_back(i);
cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);
while (getchar() != '\n');
cin >> i;
cout << count(vec.begin(), vec.end(), i) << endl;
return 0;
}
10.2
#include
#include
#include
#include
using namespace std;
int main()
{
list<string> lst;
string s;
while (cin >> s)
lst.emplace_back(s);
cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);
while (getchar() != '\n');
cin >> s;
cout << count(lst.begin(), lst.end(), s) << endl;
return 0;
}
10.3
#include
#include
#include
#include
using namespace std;
int main()
{
vector<int> vec(3, 10);
auto sum = accumulate(vec.cbegin(), vec.cend(), 0);
cout << sum << endl;
return 0;
}
10.4
10.5
10.6
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector<int> vec;
fill_n(back_inserter(vec), 10, 0);
for (auto &i : vec)
cout << i << ends;
cout << endl;
return 0;
}
10.7
10.8
10.9
#include
#include
#include
#include
#include
using namespace std;
void elimDups(vector<string> &vec);
int main()
{
vector<string> vec;
string str;
while (cin >> str)
vec.emplace(vec.end(), str);
elimDups(vec);
for (auto &i : vec)
cout << i << ends;
cout << endl;
return 0;
}
void elimDups(vector<string> &vec)
{
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
}
10.10
10.11
#include
#include
#include
#include
#include
using namespace std;
typedef bool(*is)(const string&, const string&);
typedef void(*st_sort)(vector<string>::iterator, vector<string>::iterator, is);
bool isShorter(const string &s1, const string &s2);
void elimDups(vector<string> &vec, st_sort, is);
int main()
{
vector<string> vec;
string str;
while (cin >> str)
vec.emplace(vec.end(), str);
elimDups(vec, stable_sort, isShorter);
for (auto &i : vec)
cout << i << ends;
cout << endl;
return 0;
}
void elimDups(vector<string> &vec, st_sort u, is i)
{
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
u(vec.begin(), vec.end(), i);
}
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
10.12
#include
#include
#include
#include
#include
#include "../Sales_data/Sales_data.h"
using namespace std;
bool compareIsbn(const Sales_data &s1, const Sales_data &s2);
int main()
{
vector<Sales_data> vec;
Sales_data sa;
while (read(cin, sa))
{
vec.push_back(sa);
}
sort(vec.begin(), vec.end(), compareIsbn);
for (auto &i : vec)
print(cout, i) << endl;
return 0;
}
bool compareIsbn(const Sales_data &s1, const Sales_data &s2)
{
return s1.isbn() < s2.isbn();
}
10.13
#include
#include
#include
#include
#include
using namespace std;
bool is(const string &s);
int main()
{
vector<string> vec;
string str;
while (cin >> str)
vec.emplace_back(str);
auto par_end = partition(vec.begin(), vec.end(), is);
while (par_end-- != vec.begin())
cout << *par_end << endl;
return 0;
}
bool is(const string &s)
{
return s.size() >= 5;
}
10.14
10.15
#include
#include
#include
#include
#include
using namespace std;
auto main() -> int
{
int a = 10;
auto f = [a](int b) -> int { return a + b; };
cout << f(10) << endl;
return 0;
}
10.16
#include
#include
#include
#include
#include
using namespace std;
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
auto main() -> int
{
vector<string> vec;
string str;
while (cin >> str)
vec.push_back(str);
biggies(vec, 3);
return 0;
}
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
auto wc = find_if(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
auto count = words.end() - wc;
cout << count << endl;
for_each(wc, words.end(), [](const string &s) { cout << s << ends; });
cout << endl;
}
10.17
#include
#include
#include
#include
#include
#include "../Sales_data/Sales_data.h"
using namespace std;
int main()
{
vector<Sales_data> vec;
Sales_data sa;
while (read(cin, sa))
{
vec.push_back(sa);
}
sort(vec.begin(), vec.end(), [](const Sales_data &s, const Sales_data &b){ return s.isbn() < b.isbn(); });
for (auto &i : vec)
print(cout, i) << endl;
return 0;
}
10.18
#include
#include
#include
#include
#include
#include
using namespace std;
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
auto main() -> int
{
vector<string> vec;
string str;
while (cin >> str)
vec.push_back(str);
biggies(vec, 3);
return 0;
}
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
vector<string> tr;
vector<string> fa;
partition_copy(words.begin(), words.end(), back_inserter(tr), back_inserter(fa), [sz](const string &s) { return s.size() >= sz; });
auto count = tr.size();
cout << count << endl;
for_each(tr.begin(), tr.end(), [](const string &s) { cout << s << ends; });
cout << endl;
}
10.19
#include
#include
#include
#include
#include
using namespace std;
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
auto main() -> int
{
vector<string> vec;
string str;
while (cin >> str)
vec.push_back(str);
biggies(vec, 3);
return 0;
}
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
auto count = wc - words.begin();
cout << count << endl;
for_each(words.begin(), wc, [](const string &s) { cout << s << ends; });
cout << endl;
}
10.20
#include
#include
#include
#include
#include
using namespace std;
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
auto main() -> int
{
vector<string> vec;
string str;
while (cin >> str)
vec.push_back(str);
biggies(vec, 3);
return 0;
}
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
auto count = wc - words.begin();
auto count6 = count_if(words.cbegin(), words.cend(), [](const string &s) { return s.size() > 6; });
cout << count << endl;
cout << count6 << endl;
for_each(words.begin(), wc, [](const string &s) { cout << s << ends; });
cout << endl;
}
10.21
#include
#include
#include
#include
#include
using namespace std;
auto main() -> int
{
int a = 10;
auto func = [&]() ->bool { if (a == 0) return true; while (a-- != 1); return true; };
cout << a << endl;
cout << boolalpha << func() << endl;
cout << a << endl;
cout << func() << noboolalpha << endl;
return 0;
}
10.22
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace std::placeholders;
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
bool check_size(const string &s, vector<string>::size_type sz);
auto main() -> int
{
vector<string> vec;
string str;
while (cin >> str)
vec.push_back(str);
biggies(vec, 3);
return 0;
}
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
auto wc = stable_partition(words.begin(), words.end(), [sz](const string &s) -> bool { return s.size() >= sz; });
auto count = wc - words.begin();
auto count6 = count_if(words.cbegin(), words.cend(), bind(check_size, _1, 6));
cout << count << endl;
cout << count6 << endl;
for_each(words.begin(), wc, [](const string &s) { cout << s << ends; });
cout << endl;
}
bool check_size(const string &s, vector<string>::size_type sz)
{
return s.size() >= sz;
}
10.23
10.24
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace std::placeholders;
bool check_size(int a, size_t sz);
auto main() -> int
{
string s(4, 'f');
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto it = find_if(vec.begin(), vec.end(), bind(check_size, _1, s.size()));
while (it != vec.end())
cout << *it++ << endl;
return 0;
}
bool check_size(int a, size_t sz)
{
return a > sz;
}
10.25
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace std::placeholders;
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void;
bool check_size(const string &s, vector<string>::size_type sz);
auto main() -> int
{
vector<string> vec;
string str;
while (cin >> str)
vec.push_back(str);
biggies(vec, 3);
return 0;
}
auto biggies(vector<string> &words, vector<string>::size_type sz) -> void
{
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());
stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) -> bool { return s1.size() < s2.size(); });
vector<string> tr;
vector<string> fa;
partition_copy(words.begin(), words.end(), back_inserter(tr), back_inserter(fa), bind(check_size, _1, sz));
auto count = tr.size();
cout << count << endl;
for_each(tr.begin(), tr.end(), [](const string &s) { cout << s << ends; });
cout << endl;
}
bool check_size(const string &s, vector<string>::size_type sz)
{
return s.size() >= sz;
}
10.26
10.27
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 3, 4, 5, 5, 1, 11, 22, 4, 3};
list<int> lst;
sort(vec.begin(), vec.end());
unique_copy(vec.begin(), vec.end(), back_inserter(lst));
for_each(lst.begin(), lst.end(), [](int &a) { cout << a << endl; });
return 0;
}
10.28
#include
#include
#include
#include
#include
#include
#include
#define foreach(a, b) { \
for_each(a, b, [](int x){ cout << x << ends; }); \
cout << endl; \
}
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
forward_list<int> flst;
list<int> lst;
vector<int> vec2;
copy(vec.begin(), vec.end(), back_inserter(lst));
copy(vec.begin(), vec.end(), front_inserter(flst));
copy(vec.begin(), vec.end(), inserter(vec2, vec2.end()));
foreach(flst.begin(), flst.end());
foreach(vec2.begin(), vec2.end());
foreach(lst.begin(), lst.end());
return 0;
}
10.29
#include
#include
#include
#include
#include
#include
#include
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, " "))
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2)
puts("??"), exit(1);
ifstream in_file(argv[1]);
istream_iterator<string> in_iter(in_file), eof;
vector<string> vec;
copy(in_iter, eof, back_inserter(vec));
foreach(vec.begin(), vec.end(), string);
return 0;
}
10.30
#include
#include
#include
#include
#include
#include
#include
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, " "))
using namespace std;
int main()
{
istream_iterator<int> in_iter(cin), eof;
vector<int> vec(in_iter, eof);
sort(vec.begin(), vec.end());
foreach(vec.begin(), vec.end(), int);
return 0;
}
10.31
#include
#include
#include
#include
#include
#include
#include
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, " "))
using namespace std;
int main()
{
istream_iterator<int> in_iter(cin), eof;
vector<int> vec(in_iter, eof);
sort(vec.begin(), vec.end());
unique_copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));
return 0;
}
10.32
#include "../Sales_item/Sales_item.h"
#include
#include
#include
#include
#include
using namespace std;
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))
int main()
{
istream_iterator<Sales_item> item_iter(cin), eof;
ostream_iterator<Sales_item> out_iter(std::cout, "\n");
vector<Sales_item> vec(item_iter, eof);
sort(vec.begin(), vec.end(), [](const Sales_item &i, const Sales_item &b) { return i.isbn() < b.isbn(); });
auto beg = vec.begin();
Sales_item sum = *beg++;
while (beg != vec.end())
{
if (find_if(beg, vec.end(), [sum](const Sales_item &i) { return i.isbn() == sum.isbn(); }) != vec.end())
{
sum = accumulate(beg, beg + 1, sum);
++beg;
}
else
{
out_iter = sum;
sum = *beg++;
}
}
out_iter = sum;
return 0;
}
10.33
#include
#include
#include
#include
#include
#include
using namespace std;
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))
int main(int argc, char **argv)
{
if (argc != 4)
puts("??"), exit(1);
ifstream in_file(argv[1]);
ofstream out_file_ou(argv[2]);
ofstream out_file_ji(argv[3]);
istream_iterator<int> in_iter(in_file), eof;
ostream_iterator<int> out_iter_ou(out_file_ou, "\n");
ostream_iterator<int> out_iter_ji(out_file_ji, " ");
while (in_iter != eof)
{
if (*in_iter % 2 == 0)
*out_iter_ou++ = *in_iter++;
else
*out_iter_ji++ = *in_iter++;
}
return 0;
}
10.34
#include
#include
#include
#include
using namespace std;
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))
int main()
{
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
foreach(vec.rbegin(), vec.rend(), int);
return 0;
}
10.35
#include
#include
#include
#include
using namespace std;
int main()
{
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto end = vec.end() - 1; end != vec.begin() - 1; --end)
cout << *end << endl;
return 0;
}
10.36
#include
#include
#include
#include
#include
using namespace std;
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))
int main()
{
list<int> lst{10, 2, 3, 0, 4, 5, 0, 1, 2, 3};
auto f = find(lst.rbegin(), lst.rend(), 10);
int index = 0;
auto beg = lst.begin();
while (beg != f.base())
{
++index;
++beg;
}
cout << index << endl;
return 0;
}
10.37
#include
#include
#include
#include
#include
using namespace std;
#define foreach(a, b, c) copy(a, b, ostream_iterator(cout, "\n"))
int main()
{
vector<int> vec{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
list<int> lst;
copy(vec.rbegin() + 3, vec.rbegin() + 7, back_inserter(lst));
foreach(lst.begin(), lst.end(), int);
return 0;
}
10.38
10.39
10.40
10.41
10.42
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
istream_iterator<string> in_iter(cin), eof;
ostream_iterator<string> out_iter(cout, "\12");
list<string> lst(in_iter, eof);
lst.sort();
lst.sort([](const string &s1, const string &s2) { return s1.size() < s2.size(); });
lst.unique();
copy(lst.begin(), lst.end(), out_iter);
return 0;
}