c++ primer 第十章习题
程序员文章站
2024-03-21 14:18:58
...
练习10.1
int main() {
vector<int> v = {1,1,2,3,3,4,4,4,4,5,5,5};
cout << count(v.begin(),v.end(),6)<<std::endl;
cout << count(v.begin(), v.end(),1)<<std::endl;
return 0;
}
练习10.2
int main() {
list<string> v = {"hello", "hello", "how", "are", "are", "you"};
cout << count(v.begin(),v.end(),"hello")<<std::endl;
cout << count(v.begin(), v.end(),"ok")<<std::endl;
return 0;
}
练习10.3
int main() {
vector<int> v = {1,2,3,4,5,6};
int sum = 0;
sum = accumulate(v.begin(), v.end(), sum);
cout << sum <<endl;
return 0;
}
练习10.4 使用的加法是整数类型的,返回整数。
练习10.5 正确执行。
练习10.6
int main() {
vector<int> a = {1,2,3,4,5};
fill_n(a.begin(), a.size(), 0);
for(auto x : a)
cout << x<<endl;
return 0;
}
练习10.6 fill_n(v.begin, v.size(),0)
练习10.7 (a) 使用back_inserter(vec)做copy的参数 (b)同a
练习10.8 算法不会直接调用容器操作,因此不会直接增删。而调用迭代器进行增删是迭代器的操作而不是算法的。
练习10.9
int main() {
vector<int> a = {1,2,3,4,6,2,2,2,3,4,5,6,7};
sort(a.begin(), a.end());
auto end_unique = unique(a.begin(), a.end());
for(auto x : a)
cout << x << ' ';
cout << endl;
a.erase(end_unique, a.end());
for(auto x : a)
cout << x << ' ';
cout<<endl;
}
练习10.9
void elimDups(vector<string>& vs) {
sort(vs.begin(), vs.end());
auto end_unique = unique(vs.begin(), vs.end());
for(auto x : vs)
cout << x << ' ';
cout << endl;
vs.erase(end_unique, vs.end());
for(auto x : vs)
cout << x << ' ';
cout<<endl;
}
练习10.10 不会调用容器的增删操作。
练习10.11
void elimDups(vector<string>& vs) {
sort(vs.begin(), vs.end());
auto end_unique = unique(vs.begin(), vs.end());
for(auto x : vs)
cout << x << ' ';
cout << endl;
vs.erase(end_unique, vs.end());
for(auto x : vs)
cout << x << ' ';
cout<<endl;
stable_sort(vs.begin(), vs.end(), isShorter);
for(auto x : vs)
cout << x << ' ';
cout<<endl;
}
练习10.12
bool compareIsbn(const Sales_data&a, const Sales_data&b) {
return a.isbn() < b.isbn();
}
练习10.13
bool isShorter(const string& a, const string& b) {
return a.size() < b.size();
}
void elimDups(vector<string>& vs) {
sort(vs.begin(), vs.end());
auto end_unique = unique(vs.begin(), vs.end());
for(auto x : vs)
cout << x << ' ';
cout << endl;
vs.erase(end_unique, vs.end());
for(auto x : vs)
cout << x << ' ';
cout<<endl;
}
bool longer(string& a) {
return a.size() >= 5;
}
int main() {
vector<string> vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
elimDups(vs);
stable_sort(vs.begin(), vs.end(), isShorter);
for(auto x : vs)
cout << x << ' ';
cout << endl;
auto it = partition(vs.begin(), vs.end(),longer);
for(auto begin = vs.begin(); begin != it; begin++)
cout << *begin << ' ';
cout << endl;
return 0;
}
练习10.14 10.15
int main() {
int i1 = 2, i2 = 3;
auto f = [](int i1, int i2){return i1+i2;};
cout << f(i1, i2) <<endl;
auto g = [i2](int i1){return i1+i2;};
cout << g(i1)<<endl;
return 0;
}
练习10.16
void elimDups(vector<string>& vs) {
sort(vs.begin(), vs.end());
auto end_unique = unique(vs.begin(), vs.end());
for(auto x : vs)
cout << x << ' ';
cout << endl;
vs.erase(end_unique, vs.end());
for(auto x : vs)
cout << x << ' ';
cout<<endl;
}
void bigges(vector<string> & words, vector<string>::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()<b.size();});
for_each(words.begin(), words.end(), [](const string& s) { cout << s << ' ';});
cout << endl;
auto wc = find_if(words.begin(), words.end(), [sz](string &s){return s.size()>=sz;});
for_each(wc, words.end(), [](string& s) {cout << s << ' ';});
cout << endl;
}
练习10.17
sort(v.begin(),v.end(), [](Sales_data&a, Sales_data&b) {return a.isbn()<b.isbn();});
练习10.18
void bigges(vector<string> & words, vector<string>::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()<b.size();});
for_each(words.begin(), words.end(), [](const string& s) { cout << s << ' ';});
cout << endl;
auto wc = partition(words.begin(), words.end(), [sz](string &s){return s.size()>=sz;});
for_each(words.begin(), wc, [](string& s) {cout << s << ' ';});
cout << endl;
}
练习10.19 略
练习10.20
int main() {
vector<string> vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
int c = count_if(vs.begin(), vs.end(), [](string& s){return s.size()>=6;});
cout << c <<endl;
return 0;
}
练习10.21
int main() {
int i1 = 3;
auto f = [i1]()mutable->bool{if (i1 > 0) return (i1--) == 0;
else return true;};
cout << f()<<endl;
cout << f()<<endl;
cout << f()<<endl;
cout << f()<<endl;
return 0;
}
练习10.22
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <functional>
using namespace std::placeholders;
using namespace std;
bool check_size(const string& s, string::size_type sz) {
return s.size()>=sz;
}
int main() {
auto f = bind(check_size,_1, 6);
vector<string> vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
int c = count_if(vs.begin(), vs.end(), f);
cout << c <<endl;
return 0;
}
练习10.23 参数是第一个参数本身和第一个参数的形参列表。
练习10.24
int main() {
string s = "nihaoma";
vector<int> v = {1,2,3,4,5,6,9,11,2,113,443};
auto f = bind(check_size,s,_1);
auto it = find_if_not(v.begin(), v.end(), f);
cout << *it <<endl;
return 0;
}
练习10.25
bool check_size(const string&, string::size_type);
void bigges(vector<string> & words, vector<string>::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()<b.size();});
for_each(words.begin(), words.end(), [](const string& s) { cout << s << ' ';});
cout << endl;
auto f = bind(check_size, _1, sz);
auto wc = stable_partition(words.begin(), words.end(), f);
for_each(words.begin(), wc, [](string& s) {cout << s << ' ';});
cout << endl;
}
练习10.27
void printStrings(vector<string>& vs) {
for_each(vs.begin(), vs.end(), [](string& s) {cout << s <<' ';});
cout << endl;
}
int main() {
vector<string> vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
vector<string> em;
sort(vs.begin(), vs.end());
unique_copy(vs.begin(),vs.end(),back_inserter(em));
printStrings(vs);
printStrings(em);
return 0;
}
练习10.28
void printInts(deque<int>& v) {
for_each(v.begin(), v.end(), [](int& x){cout << x << ' ';});
cout << endl;
}
int main() {
// auto f = bind(check_size,_1, 6);
vector<int> v = {1,2,3,4,5,6,7,8,9};
deque<int> v1,v2,v3;
copy(v.begin(), v.end(), inserter(v1,v1.begin()));
copy(v.begin(), v.end(), back_inserter(v2));
copy(v.begin(), v.end(), front_inserter(v3));
printInts(v1);
printInts(v2);
printInts(v3);
return 0;
}
练习10.29
int main() {
ifstream in("e_9_20.cpp");
if(in) {
istream_iterator<string> in_iter(in), eof;
vector<string> vs(in_iter,eof);
cout << vs.back()<<endl;
}else {
cout << "could not open file!"<<endl;
}
return 0;
}
练习10.30
int main() {
istream_iterator<int> in_iter(cin), eof;
ostream_iterator<int> out_iter(cout, " ");
vector<int> v(in_iter,eof);
sort(v.begin(),v.end());
copy(v.begin(),v.end(),out_iter);
return 0;
}
练习10.31 sort和copy中间加个unique。
练习10.32
bool compareIsb(Sales_item& a, Sales_item& b) {
return a.isbn() < b.isbn();
}
int main()
{
ifstream in_f("../1/data/book_sales");
istream_iterator<Sales_item> in_iter(in_f), eof;
ostream_iterator<Sales_item> out_iter(cout, "\n");
if(in_f) {
vector<Sales_item> v(in_iter, eof);
sort(v.begin(), v.end(),compareIsb);
copy(v.begin(), v.end(),out_iter);
cout << endl;
for(auto begin = v.begin(); begin != v.end();) {
auto end = find_if(begin, v.end(), [begin](Sales_item& s) {return s.isbn() != begin->isbn();});
Sales_item total(begin->isbn());
out_iter = accumulate(begin, end, total);
begin = end;
}
}else
cout << "could not open data."<<endl;
return 0;
}
练习10.33
int main()
{
ifstream in_f("data/10_33");
ofstream out_f1("data/10_33_1"), out_f2("data/10_33_2");
istream_iterator<int> in_iter(in_f), eof;
ostream_iterator<int> out_iter1(out_f1, " "), out_iter2(out_f2, "\n");
int tmp;
while(in_iter != eof) {
tmp = *in_iter++;
if (tmp%2)
out_iter1 = tmp;
else
out_iter2 = tmp;
}
return 0;
}
练习10.34
int main()
{
vector<int> v = {1,2,4,5};
vector<int>::reverse_iterator iter = v.rbegin(), rend = v.rend();
while(iter != rend)
cout << *iter++ << ' ';
return 0;
}
练习10.35
int main()
{
vector<int> v = {1,2,4,5};
vector<int>::iterator iter = v.end(), end = v.begin();
while(iter != end)
cout << *--iter << ' ';
return 0;
}
练习10.36
int main()
{
list<int> v = {1,2,4,5,0,1,2,0,3};
auto iter = find(v.rbegin(), v.rend(), 0);
cout << *++iter <<endl;
return 0;
}
练习10.37
int main()
{
vector<int> v = {1,2,4,5,0,1,2,0,3,10};
list<int> l(v.rbegin()+3,v.rbegin()+7);
ostream_iterator<int> out_iter(cout, " ");
copy(l.begin(), l.end(), out_iter);
return 0;
}
练习10.39 双向迭代器 随机迭代器
练习10.40 copy要求输入和输出两种对两个容器。 reverse和unique要求双向读写。
练习10.41 略
练习10.42
void elimDups(list<string>& ls) {
ls.sort();
for_each(ls.begin(), ls.end(),[](string&s){cout << s<<' ';});
cout << endl;
ls.unique();
for_each(ls.begin(), ls.end(),[](string&s){cout << s<<' ';});
cout<<endl;
}
上一篇: 字符串的操作