c++primer第十章練習問題
11406 ワード
練習10.1
練習10.2
練習10.3
練習10.4で使用する加算は整数タイプで、整数を返します.
練習10.5は正しく実行されます.
練習10.6
練習10.6 file_n(v.begin, v.size(),0)
練習10.7(a)back_を使うInserter(vec)はcopyのパラメータ(b)をaとする
練習10.8アルゴリズムはコンテナ操作を直接呼び出すことはないので、直接削除しません.反復器を呼び出して削除するのは、アルゴリズムではなく反復器の操作です.
練習10.9
練習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.27
練習10.28
練習10.29
練習10.30
練習10.31 sortとcopyの間にuniqueを追加します.
練習10.32
練習10.33
練習10.34
練習10.35
練習10.36
練習10.37
練習10.39双方向反復ランダム反復器
練習10.40 copyは2つのコンテナの入力と出力を要求します.reverseとuniqueは双方向の読み書きを要求します.
練習10.41略
練習10.42
int main() {
vector v = {1,1,2,3,3,4,4,4,4,5,5,5};
cout << count(v.begin(),v.end(),6)<<:endl cout="" count="" v.end="" return=""/>
練習10.2
int main() {
list v = {"hello", "hello", "how", "are", "are", "you"};
cout << count(v.begin(),v.end(),"hello")<<:endl cout="" count="" v.end="" return=""/>
練習10.3
int main() {
vector v = {1,2,3,4,5,6};
int sum = 0;
sum = accumulate(v.begin(), v.end(), sum);
cout << sum <
練習10.4で使用する加算は整数タイプで、整数を返します.
練習10.5は正しく実行されます.
練習10.6
int main() {
vector a = {1,2,3,4,5};
fill_n(a.begin(), a.size(), 0);
for(auto x : a)
cout << x<
練習10.6 file_n(v.begin, v.size(),0)
練習10.7(a)back_を使うInserter(vec)はcopyのパラメータ(b)をaとする
練習10.8アルゴリズムはコンテナ操作を直接呼び出すことはないので、直接削除しません.反復器を呼び出して削除するのは、アルゴリズムではなく反復器の操作です.
練習10.9
int main() {
vector 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<
練習10.9
void elimDups(vector& 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<
練習10.10コンテナの削除操作は呼び出されません.
練習10.11
void elimDups(vector& 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<
練習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& 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<= 5;
}
int main() {
vector 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) <
練習10.16
void elimDups(vector& 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< & words, vector::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.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()
練習10.18
void bigges(vector & words, vector::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()=sz;});
for_each(words.begin(), wc, [](string& s) {cout << s << ' ';});
cout << endl;
}
練習10.19略
練習10.20
int main() {
vector 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 <
練習10.21
int main() {
int i1 = 3;
auto f = [i1]()mutable->bool{if (i1 > 0) return (i1--) == 0;
else return true;};
cout << f()<
練習10.22
#include
#include
#include
#include
#include
#include
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 vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
int c = count_if(vs.begin(), vs.end(), f);
cout << c <
練習10.23パラメータは、最初のパラメータ自体と最初のパラメータのパラメータリストです.
練習10.24
int main() {
string s = "nihaoma";
vector 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 <
練習10.25
bool check_size(const string&, string::size_type);
void bigges(vector & words, vector::size_type sz) {
elimDups(words);
stable_sort(words.begin(), words.end(), [](const string&a, const string&b){return a.size()
練習10.27
void printStrings(vector& vs) {
for_each(vs.begin(), vs.end(), [](string& s) {cout << s < vs = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
vector 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& v) {
for_each(v.begin(), v.end(), [](int& x){cout << x << ' ';});
cout << endl;
}
int main() {
// auto f = bind(check_size,_1, 6);
vector v = {1,2,3,4,5,6,7,8,9};
deque 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 in_iter(in), eof;
vector vs(in_iter,eof);
cout << vs.back()<
練習10.30
int main() {
istream_iterator in_iter(cin), eof;
ostream_iterator out_iter(cout, " ");
vector 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 in_iter(in_f), eof;
ostream_iterator out_iter(cout, "
");
if(in_f) {
vector 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."<
練習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 in_iter(in_f), eof;
ostream_iterator out_iter1(out_f1, " "), out_iter2(out_f2, "
");
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 v = {1,2,4,5};
vector::reverse_iterator iter = v.rbegin(), rend = v.rend();
while(iter != rend)
cout << *iter++ << ' ';
return 0;
}
練習10.35
int main()
{
vector v = {1,2,4,5};
vector::iterator iter = v.end(), end = v.begin();
while(iter != end)
cout << *--iter << ' ';
return 0;
}
練習10.36
int main()
{
list v = {1,2,4,5,0,1,2,0,3};
auto iter = find(v.rbegin(), v.rend(), 0);
cout << *++iter <
練習10.37
int main()
{
vector v = {1,2,4,5,0,1,2,0,3,10};
list l(v.rbegin()+3,v.rbegin()+7);
ostream_iterator out_iter(cout, " ");
copy(l.begin(), l.end(), out_iter);
return 0;
}
練習10.39双方向反復ランダム反復器
練習10.40 copyは2つのコンテナの入力と出力を要求します.reverseとuniqueは双方向の読み書きを要求します.
練習10.41略
練習10.42
void elimDups(list& ls) {
ls.sort();
for_each(ls.begin(), ls.end(),[](string&s){cout << s<