简单记录下我的学习过程 (代码为主)
所有容器适用--------把数据从一个容器copy到另一个容器
//或者同一个容器中不同区间间的copy
copy()
copy_backward() //从最后一个元素开始拷贝到指定位置往前放
/**------http://blog.csdn.net/u010579068------**/ #include<iostream> #include<algorithm> #include<list> #include<iterator> #include<vector> using namespace std; /***************************************** //所有容器适用--------把数据从一个容器copy到另一个容器 copy() //或者同一个容器中不同区间间的copy copy_backward() //从最后一个元素开始拷贝到指定位置往前放 注意: 1、没有copy_if()算法,可以使用remove_copy_if()算法 2、复制过程中要逆转元素次序,使用reverse_copy()算法 3、把容器内所有元素赋值给另一个容器,要使用赋值操作符或容器的assign()成员函数 4、复制过程中删除某些元素,使得remove_copy()和remove_copy_if()算法 5、复制中改变元素,使用transform()和replace_copy()算法 *****************************************/ /**---------------------------------------------------------------------------------- 修改性算法:(简介) for_each() generate() copy() generate_n() copy_backward() replace() transform() replace_if() merge() replace_copy() swap_ranges() replace_copy_if() fill() fill_n() ----------------------------------------------------------------------------------**/ /************************************************************************************* std::copy 所有排序容器适用 algorithm -------------------------------------------------------------------------------------- template <class InputIterator, class OutputIterator> OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ); //eg: template<class InputIterator, class OutputIterator> OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ) { while (first!=last) *result++ = *first++; return result; } *************************************************************************************/ /************************************************************************************* std::copy_backward 所有排序容器适用 algorithm -------------------------------------------------------------------------------------- template <class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result ); //eg: template<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result ) { while (last!=first) *(--result) = *(--last); return result; }*************************************************************************************/ bool IsOdd (int i) { return ((i%2)==1); } int main() { list<int> ilist; for(int i=0; i<10; i++) ilist.push_back(i); list<int>::iterator iter_list=ilist.begin(); while(iter_list != ilist.end()) cout<<*iter_list++<<" "; cout<<endl; vector<int> ivec(ilist.size()*1.5); vector<int>::iterator iter_vector=ivec.begin(); while(iter_vector != ivec.end()) cout<<*iter_vector++<<" "; cout<<endl; copy(ilist.begin(),ilist.end(),ivec.begin()); iter_vector=ivec.begin(); while(iter_vector != ivec.end()) cout<<*iter_vector++<<" "; cout<<endl; copy_backward(ilist.begin(),ilist.end(),ivec.end()); iter_vector=ivec.begin(); while(iter_vector != ivec.end()) cout<<*iter_vector++<<" "; cout<<endl; cout<<endl; /**------------------------------------------------------------------**/ //基本copy,拷贝之前保证足够的空间 //如果一定要copy,可以用一个插入迭代器back_inserter,往后插入数据44 //eg: list<int> li; copy(ivec.begin(),ivec.end(),back_inserter(li)); iter_list=li.begin(); while(iter_list != li.end()) cout<<*iter_list++<<" "; cout<<endl; //还可以拷贝到输出流里 输出流迭代器<iterator> copy(ivec.begin(),ivec.end(),ostream_iterator<int>(cout," ")); cout<<endl; //逆向拷贝,用逆向迭代器 copy(ivec.rbegin(),ivec.rend(),li.begin()); iter_list=li.begin(); while(iter_list != li.end()) cout<<*iter_list++<<" "; cout<<endl; cout<<endl; /**------------------------------------------------------------------**/ vector<char> source(10,'.'); for(int c='a'; c<='f'; c++) source.push_back(c); source.insert(source.end(),10,'.'); vector<char>::iterator iv=source.begin(); while(iv != source.end()) cout<<*iv++<<" "; cout<<endl; //自身区间拷贝覆盖 vector<char> c1(source); copy(c1.begin()+10,c1.begin()+16,c1.begin()+7); iv=c1.begin(); while(iv!=c1.end()) cout<<*iv++<<" "; cout<<endl; //自身区间拷贝逆覆盖 vector<char> c2(source); copy_backward(c2.begin()+10,c2.begin()+16,c2.begin()+19); iv=c2.begin(); while(iv!=c2.end()) cout<<*iv++<<" "; cout<<endl; cout<<endl; /**------------------------------------------------------------------**/ //符合条件的拷贝 (可修改元素,后续继续学习) //remove_copy_if() int myints[] = {1,2,3,4,5,6,7,8,9}; vector<int> myvector; vector<int>::iterator it; remove_copy_if (myints,myints+9,back_inserter(myvector),IsOdd); cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0; }