练习10.1、10.2:考察函数count的用法
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<list> 5 #include<algorithm> 6 using namespace std; 7 8 int main() 9 { 10 vector<int>ivec={1,2,3,4,5,6,6}; 11 cout<<count(ivec.cbegin(),ivec.cend(),6)<<endl; 12 list<string>lst={"aa","bb","cc"}; 13 cout<<count(lst.cbegin(),lst.cend(),"aa")<<endl; 14 }
练习10.3:考察函数accumulate的用法
1 #include<iostream> 2 #include<numeric> 3 #include<vector> 4 using namespace std; 5 6 int main() 7 { 8 vector<int>ivec={1,2,3,4,5,6,7,8,9}; 9 cout<<accumulate(ivec.cbegin(),ivec.cend(),0); 10 }
练习10.4:考察同上
调用accumulate时,因为函数accummulate的第三个参数是0(为一个int型),所以程序会将vector<double>中的所有元素类型强制转换为int型进行计算,会有精度损失
练习10.5:考察c风格字符串在函数equal中的应用
当两个c风格字符串一样时会返回true,但我们还是应该注意比较两个c风格字符串最好应该用strcmp
练习10.6:考察函数fill_n的用法
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 using namespace std; 5 6 int main() 7 { 8 vector<int>ivec={1,2,3,4,5,6}; 9 fill_n(ivec.begin(),ivec.size(),0); 10 for ( auto i:ivec ) cout<<i<<endl; 11 }
练习10.7:考察copy和fill_n函数的使用前提
1 #include<iostream> 2 #include<vector> 3 #include<list> 4 #include<iterator> 5 #include<algorithm> 6 using namespace std; 7 8 int main() 9 { 10 vector<int>vec; 11 list<int>lst; 12 int i; 13 while ( cin>>i ) lst.push_back(i); 14 vec.resize(lst.size()); 15 copy(lst.cbegin(),lst.cend(),vec.begin()); 16 for ( auto i:vec ) cout<<i<<endl; 17 18 vector<int>ivec; 19 ivec.reserve(10); 20 //ivec.resize(10); 21 //fill_n(back_inserter(ivec),10,0); 22 for ( auto i:ivec ) cout<<i<<endl; 23 }
(a)使vec.resize(lst.size()),即保证vec中所含的元素数量至少和lst中的元素数量一样多才能使用copy函数
(b)该程序不算错误,但是没有意义,当vec.reserve(10),容器改变的只是容量,而容器内所含的元素数量并没有发生改变。
有两种修改的办法,一种是通过back_inserter创建一个插入迭代器,另外一种就是通过resize改变容器内的元素数量
练习10.8:考察对back_inserter的理解
因为back_inserter是一个插入迭代器,是通过迭代器适配器的容器操作产生的,目的是将元素添加到所要求的容器当中
标准库算法是不会改变它们所操作的容器的大小,但是迭代器可以通过使用容器操作改变它的
练习10.9:考察消除重复单词的实现
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 7 void elimDups(vector<string> &words) 8 { 9 sort(words.begin(),words.end()); 10 auto end_unique=unique(words.begin(),words.end()); 11 words.erase(end_unique,words.end()); 12 } 13 14 int main() 15 { 16 string str; 17 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 18 elimDups(vec); 19 for ( auto i:vec ) cout<<i<<endl; 20 }
练习10.10:考察算法不改变容器大小的原因
因为算法是通过迭代器对容器中的元素进行操作的,并不是直接操作容器中元素
练习10.11:考察排序算法的相关变形
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 7 bool isShorter(const string &s1,const string &s2) 8 { 9 return s1.size()<s2.size(); 10 } 11 12 void elimDups(vector<string> &words) 13 { 14 sort(words.begin(),words.end()); 15 auto end_unique=unique(words.begin(),words.end()); 16 words.erase(end_unique,words.end()); 17 } 18 19 int main() 20 { 21 string str; 22 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 23 elimDups(vec); 24 stable_sort(vec.begin(),vec.end(),isShorter); 25 for ( const auto &i:vec ) cout<<i<<endl; 26 }
练习10.13:考察函数partition及其参数的应用
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<vector> 5 using namespace std; 6 7 bool predicate(const string& s) 8 { 9 return s.size()>=5; 10 } 11 12 int main() 13 { 14 string str; 15 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 16 auto pos=partition(vec.begin(),vec.end(),predicate); 17 for ( auto i=vec.cbegin();i!=pos;++i ) cout<<*i<<endl; 18 }
练习10.14:考察lambda的使用
auto add=[] (int a,int b) {return a+b;};
练习10.15:考察同上
int i; auto add[i] (int num) {return i+num;};
练习10.16:考察lambda的综合应用
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 7 void elimDups(vector<string> &words) 8 { 9 sort(words.begin(),words.end()); 10 auto end_unique=unique(words.begin(),words.end()); 11 words.erase(end_unique,words.end()); 12 } 13 14 void biggies(vector<string> &words,vector<string>::size_type sz) 15 { 16 //将words按字典序排序,删除重复单词 17 elimDups(words); 18 //按长度排序,长度相同的单词维持字典序 19 stable_sort(words.begin(),words.end(), 20 [](const string &s1,const string &s2) 21 {return s1.size()<s2.size();}); 22 //获取第一个迭代器,指向第一个满足size()>=sz的元素 23 auto wc=find_if(words.begin(),words.end(), 24 [sz](const string &s) 25 {return s.size()>=sz;}); 26 //计算满足size>=sz的元素的数目 27 auto count=words.end()-wc; 28 for_each(wc,words.end(), 29 [](const string &s) 30 {cout<<s<<" ";}); 31 //打印长度大于等于给定值的单词,每个单词后面接一个空格 32 } 33 34 int main() 35 { 36 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 37 biggies(vec,5); 38 }
练习10.18:考察同上
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 7 void elimDups(vector<string> &words) 8 { 9 sort(words.begin(),words.end()); 10 auto end_unique=unique(words.begin(),words.end()); 11 words.erase(end_unique,words.end()); 12 } 13 14 void biggies(vector<string> &words,vector<string>::size_type sz) 15 { 16 //将words按字典序排序,删除重复单词 17 elimDups(words); 18 //按长度排序,长度相同的单词维持字典序 19 stable_sort(words.begin(),words.end(), 20 [](const string &s1,const string &s2) 21 {return s1.size()<s2.size();}); 22 auto wc=partition(words.begin(),words.end(), 23 [sz](const string &s) 24 {return s.size()>=sz;}); 25 //计算满足size>=sz的元素的数目 26 auto count=wc-words.begin(); 27 for_each(words.begin(),wc, 28 [](const string &s) 29 {cout<<s<<" ";}); 30 //打印长度大于等于给定值的单词,每个单词后面接一个空格 31 } 32 33 int main() 34 { 35 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 36 biggies(vec,5); 37 }
练习10.19:考察同上
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include<algorithm> 5 using namespace std; 6 7 void elimDups(vector<string> &words) 8 { 9 sort(words.begin(),words.end()); 10 auto end_unique=unique(words.begin(),words.end()); 11 words.erase(end_unique,words.end()); 12 } 13 14 void biggies(vector<string> &words,vector<string>::size_type sz) 15 { 16 elimDups(words); 17 auto wc=stable_partition(words.begin(),words.end(), 18 [sz](const string &s) 19 {return s.size()>=sz;}); 20 auto count=wc-words.begin(); 21 for_each(words.begin(),wc, 22 [](const string &s) 23 {cout<<s<<" ";}); 24 } 25 26 int main() 27 { 28 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 29 biggies(vec,5); 30 }
练习9.20:考察同上
1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 #include<vector> 5 using namespace std; 6 7 int main() 8 { 9 string str; 10 vector<string>vec={"the","quick","red","fox","jumps","over","the","slow","red","turtle"}; 11 auto num=count_if(vec.cbegin(),vec.cend(), 12 [] (const string &s) 13 {return s.size()>6;}); 14 cout<<num<<endl; 15 }
练习9.21:考察lambda的具体使用细节
1 #include<iostream> 2 using namespace std; 3 4 int main() 5 { 6 int val=5; 7 auto f=[&val]() { 8 if ( val>0 ) --val; 9 return val==0? true:false; 10 }; 11 while ( !f() ) cout<<val<<endl; 12 }