C++第十五天泛型算法和迭代器
2014-07-29 10:25
381 查看
/* 泛型算法: 泛型算法中,对指定数目的元素做写入运算,或者写到目标迭代器的算法,都不检查目标的大小是否足以存储要写入的元素 流迭代器的限制: 1.不可能从ostream_iterator对象读入,也不可能写道istream_iterator对象中。 2.一旦给ostream_iterator对象赋了一个值,写入就提交了。赋值后,没有办法在改变这个值。此外,ostream_iterator对象中每个不同的值都只能只好输出一次。 3.ostream_iterator没有->操作符。 流迭代器不能反向遍历流,因此流迭代器不能创建反向迭代器。 输入迭代器:可用于读取容器中的元素,但是不能报纸能支持容器的写入操作。输入迭代器必须至少提供下列支持: 1.相等和不等操作符(==,!=),比较两个迭代器。 2.前置和后置的自增运算(++),使迭代器向前递进指向下一个元素。 3.用于读取元素的解引用操作符(*),此操作符只能出现在赋值运算的右操作数上。 4.箭头操作符(-->),这是(*it).member的同义语,也就是说,对迭代器进行解引用来获取其所关联的对象的成员。 (注:输入迭代器只能顺序使用,一旦输入迭代器自增了,就无法在用它检查之前的元素。要求在这个层次上提供支持的泛型算法包括find和accumulate。) 输出迭代器:可视为与输入迭代器功能互补的迭代器,输出迭代器可用于向容器写入元素,但是不能保证能支持读取容器的内容。 1.前置和后置的自增运算(++),使迭代器向前递进指向下一个元素。 2.解引用操作符(*),此操作符只能出现在赋值运算的左操作数上。 (注:使用输出迭代器时,对于指定的迭代器应该使用一次*运算,而且只能使用一次。) 前向迭代器:读和写,只支持自增运算。 双向迭代器:读和写,支持自增和自减运算。 随机访问迭代器:提供在常量时间内访问容器任意位置的功能,除了支持双向迭代器的功能外,还支持下面的功能: 1.关系操作符(<、<=、>、>=),比较两个迭代器的相对位置。 2.迭代器与整型树脂n之间的加法和减法操作符+、+=、-、-=,结果是迭代器在容器中向前或向后n个元素。 3.两个迭代器之间的减法操作符(-),得到两个迭代器间的距离。 4.下标操作符(iter ),这是*(iter+n)的同义词。 */ //泛型算法头文件 #include<algorithm> //泛化的算术算法头文件 #include<numeric> #include<iostream> #include<vector> #include<list> using namespace std; int main(){ vector<int> vec; for(int i=0;i!=10;++i){ vec.push_back(i); } //计算元素的和的只读算法 int sum=accumulate(vec.begin(),vec.end(),0); cout<<sum<<endl; //降序排序。 sort(vec.rbegin(),vec.rend()); vector<int>::iterator bsvec=vec.begin(); while(bsvec!=vec.end()){ cout<<*bsvec<<" "; ++bsvec; } cout<<endl; /* vector<string> vec2; for(int j=0;j!=10;++j){ vec2.push_back(string(""+j)); } //下面这句话报错,因为累加和的类型将是const char*,而string的加法擦欧洲哦符所使用的操作数分别是string和const char*类型,加法的结果将产生一个string对象,而不是const char*指针。 string su=accumulate(vec2.begin(),vec2.end(),string("")); cout<<sum<<endl; */ //写入输入序列的元素,把指定范围内的元素都设为给定的值。 fill(vec.begin(),vec.end(),0); //结果算出来的值将是0 int sums=accumulate(vec.begin(),vec.end(),0); cout<<sums<<endl; //前半段指定为1 fill(vec.begin(),vec.begin()+vec.size()/2,1); //结果算出来的值将是5 int sumsa=accumulate(vec.begin(),vec.end(),0); cout<<sumsa<<endl; /* string str[]={"the","quick","red","fox","jumps","over","the","slow","red","turthle"}; vector<string> words; for(string::size_type ij=0;ij!=sizeof(str)/sizeof(string);++ij){ words.push_back(str[ij]); } //排序 sort(words.begin(),words.end()); //unique的使用:该算法删除响亮的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器,表示无重复的值范围的结束。 vector<string>::iterator end_unique=unique(words.begin(),words.end()); words.erase(end_unique,words.end()); */ //顺序表容器 list<int> ilst,ilst2,ilst3; //为第一个顺序表容器赋值 for(list<int>::size_type ins=0;ins!=4;++ins){ ilst.push_front(ins); } //通过迭代器访问容器的元素 list<int>::iterator beg=ilst.begin(); while(beg!=ilst.end()){ cout<<*beg<<" "; ++beg; } cout<<endl; //通过复制,给容器2赋值 copy(ilst.begin(),ilst.end(),front_inserter(ilst2)); //遍历第二个容器 list<int>::iterator beg2=ilst2.begin(); while(beg2!=ilst2.end()){ cout<<*beg2<<" "; ++beg2; } cout <<endl; //通过赋值,给容器3赋值 copy(ilst.begin(),ilst.end(),inserter(ilst3,ilst3.begin())); list<int>::iterator beg3=ilst3.begin(); while(beg3!=ilst3.end()){ cout<<*beg3<<" "; ++beg3; } cout <<endl; /* //输入流迭代器 istream_iterator<int> cin_it(cin); istream_iterator<int> end_of_stream; //下面这句代码有错 vector<int> vecsa(cin_it,end_of_stream); //排序 sort(vecsa.begin(),vecsa.end()); //输出流迭代器 ostream_iterator<int> output(cout," "); //删除重复 unique_copy(vecsa.begin(),vecsa.end(),output); */ /* string sa[10]; //字符串数组为初始化? const vector<string> file_names(sa,sa+6); //报未初始化错误。 vector<string>::iterator istsa=file_names.begin()+2; */ return 0; } /* //泛型算法头文件 #include<algorithm> //泛化的算术算法头文件 #include<numeric> #include<iostream> #include<vector> using namespace std; //比较两个单词的长度大小 bool isShorter(const string &s1,const string &s2){ return s1.size()<s2.size(); } //判定该单词的长度是否大于等于6 bool GT6(const string &s){ return s.size()>=6; } int main(){ vector<string> words; string next_word; while(cin >> next_word){ words.push_back(next_word); } //排序 sort(words.begin(),words.end()); //删除相邻的重复单词,把该重复的单词移动到最后 vector<string>::iterator end_unique=unique(words.begin(),words.end()); //真正的删除容器的元素 words.erase(end_unique,words.end()); //根据单词长度排序 stable_sort(words.begin(),words.end(),isShorter); //判断单词长度是否大于6 vector<string>::size_type wc=count_if(words.begin(),words.end(),GT6); cout<<wc<<" "<<make_plural(wc,"word","s")<<" 6characters or longer"<<endl; return 0; } */
相关文章推荐
- C++语法基础--泛型算法(generic algorithm)--插入迭代器back_inserter(),front_insertor(),inserter()以及next()函数简介
- C++STL总结(附带容器迭代器泛型算法详情链接【未完成】)
- C++学习笔记28——泛型算法之iostream迭代器
- C++学习笔记29——泛型算法之插入迭代器
- C++ 学习笔记(10)泛型算法、lambda表达式、bind函数、迭代器
- C++ 泛型算法及迭代器概述
- C++语法基础--泛型算法(generic algorithm)--反向迭代器,reverse_iterator::base()
- C++语法基础--泛型算法(generic algorithm)--iostream迭代器
- C++初学,泛型算法 一
- C++语法基础--泛型算法(generic algorithm)--transform
- C++语法基础--泛型算法(generic algorithm)--find(),find_if()
- C++ 插入迭代器,算法使用
- c++学习9之泛型算法
- 八皇后问题C++泛型算法实现
- C++泛型算法
- C++泛型算法之向算法传递参数
- 容器、迭代器、泛型算法综合小实例
- c++ 泛型算法 使用方法总结
- c++ 泛型算法 numeric方法实现原型
- C++沉思录读书笔记(20章)- 通过迭代器配接器提高泛型算法的通用性