读书笔记(C++)——【泛型算法】

    xiaoxiao2025-06-14  23

    #include<iostream> #include<fstream> #include<sstream> #include<string> #include<cctype> #include<vector> #include<map> #include<set> #include <stdexcept> #include<stdlib.h> #include<algorithm> #include<numeric> //11.1 概述 //容器内查询某个特定值,vector为例 int search_value = 42; vector<int>::iterator resualt = find(vec.begin(),vec.end(),search_value); cout<<"the resualt"<<search_value<<(resualt == vec.end() ? "is not present" : "is present"); <<endl; //数组例 int a[6] = {27,210,12,47,109,83}; int search_value = 83; int *result = find(a,a+6,search_value); cout<<"the resualt"<<search_value<<(resualt == a+6 ? "is not present" : "is present"); <<endl; //11.2.1 只读算法 //sum the element in vec starting the summation with the value 42 int sum = accumulate(vec.begin().vec.end(),42); //concatenate element from v and store in sum string sum = accumulate(v.begin(),v.end()," "); //第三个参数不能使显式字符串字面值 // find_first_of的使用 //统计有多少名字同时出现在两个列表中 size_t cnt = 0; vector<string>::iterator it = roster1.begin(); while((it = find_first_of(it,roster1.end(),roster2.begin(), roster2.end())) != roster1.end()) { ++cnt; ++it; } cout << "Found " << cnt << " name on both rosters" << endl; //11.2.2写容器元素的算法 //1.写入输入序列的元素 fill(vec.begin(),vec.end(),10);//reset each element to 10; //2.不检查写入操作的算法 vector<int> vec;//empty vector //disaster:attempts to write to 10(nonexistent) element in vec; fill_n(vec.begin(),10,0); //3.引入back_inserter vector<int> vec; fill_n(back_inserter(vec),10,0);//ok //4.写入到目标迭代器的算法 vector<int> ivec; copy(ilist.begin(),ilist.end(),back_inserter(ivec)); //better way to copy element from list vector<int> ivec(list.begin(),list.end()); //5.算法的_copy版本 //replace empty vector with value of 0 by 42 replace(ilst.begin(),ilst.end(),0,42); //creat empty vector to hold the replacement vector<int> ivec. replace_copy(ilst.begin(),ilst.end(),back_inserter(ivec),0,42); //11.2.3 对容器元素重新排序的算法 //输入:the quick red fox jumps over the slow red turtle //输出:1 word 6 characters or longer //去掉重复 vector<string> words; sort(words.begin(),words.end()); vector<string>::iterator end_unique = unique(words.begin,words.end()); words.erase(end_unique,words.end()) //定义函数 - 谓词 bool isShorter(string &s1,string &s2) { return si.size() < s2.size(); } bool GT6(string &s) { return s1 >= 6; } //排序 //sort word by size,but maintain alphabetic order for word of the same size stable_sort(words.begin(),words.end(),isShorter); //统计长度不小于6的单词 vector<string>::size_type wc = count_if(words.begin(),words.end(),GT6); //完整程序 #include<iostream> #include<string> #include<vector> #include<stdexcept> #include<stdlib.h> #include<algorithm> #include<numeric> bool isShorter(std::string &s1,std::string &s2) { return (s1.size() < s2.size()); } bool GT6(std::string &s) { return (s.size() >= 6); } std::string make_plural(size_t ctr,const std::string &word,const std::string &ending) { return (ctr==1)?word:word + ending; } int main() { std::vector<std::string> words; std::string next_word; while(std::cin>>next_word) { words.push_back(next_word); } sort(words.begin(),words.end()); std::vector<std::string>::iterator end_unique = unique(words.begin(),words.end()); words.erase(end_unique,words.end()); stable_sort(words.begin(),words.end(),isShorter); std::vector<std::string>::size_type wc = count_if(words.begin(),words.end(),GT6); std::cout << wc << " " << make_plural(wc,"word","s") << " 6 characters or longer" << std::endl; return 0; } //11.3.1插入迭代器 1).back_inserter //push_back()实现 2).front_inserter//push_front()实现 3).inserter //insert实现 list<int>::iterator it = find(ilst.begin(),ilst.end(),42); replace_copy(ivec.begin(),ivec.end(),inserter(ilst,it),100,0); //11.3.2 iostream 迭代器 //表11 - 1 iostream 迭代器的构造函数 istream_iterator<T> in(strm); istream_iterator<T> in;//istream_iterator超出末端的迭代器 ostream_iterator<T> in(strm); ostream_iterator<T> in(strm,delim);//写入过程中使用dilim作为元素分隔符,delim是以空字符结束的字符数组 //流迭代器定义 istream_iterator<int> cin_it(cin); istream_iterator<int> end_of_stream; ofstream outfile; ostream_iterator<Sales_item> output(outfile," "); //istream_iterator 迭代器上的操作 istream_iterator<int> in_iter(cin); istream_iterator<int> eof; while(in_iter != eof) vec.push_back(*in_iter++); //等价 istream_iterator<int> in_iter(cin); istream_iterator<int> eof; vector<int> vec(in_iter,eof); //ostream_iterator对象和istream_iterator对象的使用 ostream_iterator<string> out_iter(cout," "); istream_iterator<string> in_iter(cin),eof; while(in_iter != eof) *out_iter++ = *in_file++; //在类类型上使用istream_iterator istream_iterator<Sale_item> item_iter(cin),eof; Sale_item sum; sum = *item_iter++ while(item != eof) { if(item_iter->same_isbn(sum)) sum = sum + *item_iter; else { cout<<sum<<endl; sum = *item_iter; } ++item_iter; } cout<<sum<<endl; //流迭代器三个限制 //................ //与算法一起使用流迭代器 istream_iterator cin_it(cin); istream_iterator eof; vector<int> vec(cin_it,eof); sort(vec,begin(),vec.end()); ostream_iterator output(cout," "); unique_copy(vec.begin(),vec.end(),output); //11.3.3 反向迭代器 vector<int> vec; for(int i = 0;i != 10;++i) { vec.push_back(i); } vector<int>::reverse_iterator r_iter; for(r_iter = vec.rbegin(); r_iter != vec.rend();++r_iter) { cout<<*r_iter<<endl; } //降序排列vector sort(vec.rbegin(),vec.rend()); //反向迭代器与其它迭代器 //find first element in a comma-separated list string::iterator comma = find(line.begin(),line.end(),','); cout<<string(line.begin(),comma)<<endl; //find last element in a comma-separated list string::reverse_iterator rcomma = find(line.rbegin(),line.rend(),','); cout<<string(line.rbegin(),rcomma)<<endl;//MIDDLE,LAST -> TSAL cout<<string(rcomma.base(),line.end()); //ok //11.3.4 const 迭代器 //五种迭代器 //表 11 - 3 迭代器种类 /*输入迭代器 *输出迭代器 *前向迭代器 *双向迭代器 *随机访问迭代器 */ //泛型算法结构 //容器特有算法 //对于list对象,应该优先使用list容器特有的成员版本,而不是泛型算法 //表 11 - 4 list 容器特有的操作 /* *lst.merge(lst2) *lst.merge(lst2,comp) *lst.remove(val) *lst.remove_if(unaryPred) *lst.reverse() *lst.sort() *lst.splice(iter,lst2) *lst.splice(iter,lst2,iter2) *lst.splice(iter,beg,end) *lst.unique() *lst.unique(binaryPred) */
    转载请注明原文地址: https://ju.6miu.com/read-1299928.html
    最新回复(0)