STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)
来源:互联网 发布:php防止sql注入代码 编辑:程序博客网 时间:2024/04/25 05:06
C++ Primer 学习中。。。
简单记录下我的学习过程 (代码为主)
所有容器适用
remove(b,e,v) //[b,e) 删value
remove_if(b,e,p) //[b,e) 删p条件
remove_copy(b,e,r,v) //[b,e) 删v,结果存入r
remove_copy_if(b,e,r,p) //[b,e) 删p条件,结果存入r
注意:
1、并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素
2、返回新的逻辑终点
/**------http://blog.csdn.net/u010579068------**/#include<iostream>#include<cstdio>#include<string>#include<vector>#include<list>#include<deque>#include<algorithm>using namespace std;/*****************************************//所有容器适用remove(b,e,v) //[b,e) 删valueremove_if(b,e,p) //[b,e) 删p条件remove_copy(b,e,r,v) //[b,e) 删v,结果存入rremove_copy_if(b,e,r,p) //[b,e) 删p条件,结果存入r注意: 1、并不是真正的删除,而是把后面的元素向前移动,覆盖被删除元素 2、返回新的逻辑终点*****************************************//**--------------------------------------------------------------------------------------------------------------------------------------------------------------------**//*************************************************************************************std::remove 所有排序容器适用 algorithm--------------------------------------------------------------------------------------template < class ForwardIterator, class T > ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value );//eg:template < class ForwardIterator, class T > ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value ){ ForwardIterator result = first; for ( ; first != last; ++first) if (!(*first == value)) *result++ = *first; return result;}*************************************************************************************//*************************************************************************************std::remove_if 所有排序容器适用 algorithm--------------------------------------------------------------------------------------template < class ForwardIterator, class Predicate > ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last, Predicate pred );//eg:template < class ForwardIterator, class Predicate > ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last, Predicate pred ){ ForwardIterator result = first; for ( ; first != last; ++first) if (!pred(*first)) *result++ = *first; return result;}*************************************************************************************//*************************************************************************************std::remove_copy 所有排序容器适用 algorithm--------------------------------------------------------------------------------------template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy ( InputIterator first, InputIterator last, OutputIterator result, const T& value );//eg:template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy ( InputIterator first, InputIterator last, OutputIterator result, const T& value ){ for ( ; first != last; ++first) if (!(*first == value)) *result++ = *first; return result;}*************************************************************************************//*************************************************************************************std::remove_copy_if 所有排序容器适用 algorithm--------------------------------------------------------------------------------------template <class InputIterator, class OutputIterator, class Predicate> OutputIterator remove_copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate pred );//eg:template <class InputIterator, class OutputIterator, class Predicate> OutputIterator remove_copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate pred ){ for ( ; first != last; ++first) if (!pred(*first)) *result++ = *first; return result;}*************************************************************************************/template<typename T>void Print(T& V){ typename T::iterator iter=V.begin(); while(iter != V.end()) { cout<<*iter++<<" "; } cout<<endl;}int main(){ int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 cout<<"myints : 10,20,30,30,20,10,10,20"<<endl; // bounds of range: vector<int> vec(myints,myints+8); vector<int>::iterator pend,iv;// int* pbegin = myints; // ^// int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = remove (vec.begin(), vec.end(), 20); // 10 30 30 10 10 ? ? ? // ^ ^ //pend:结尾位置 cout << "range contains:"; for(iv=vec.begin(); iv!=pend; ++iv) cout<<*iv<<" ";// for (int* p=pbegin; p!=pend; ++p)// cout << " " << *p; cout << endl; /***---实际数据---***/ cout<<"---实际数据---"<<endl; Print(vec); cout<<"表示并没有删除数据而是向前覆盖!"<<endl; cout<<endl; /**--------------------------------------------------------------------------------**/ int myints2[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9 cout<<"myints2 : 1,2,3,4,5,6,7,8,9 "<<endl; // bounds of range:// int* pbegin = myints; // ^// int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^// pend = remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ? // ^ ^ list<int> li(myints2,myints2+9); list<int>::iterator plend,il; cout << "range contains:";// for (int* p=pbegin; p!=pend; ++p)// cout << " " << *p; plend = remove_if (li.begin(),li.end(),not1(bind2nd(modulus<int>(),2)));//删除奇数 for(il=li.begin(); il!=plend; ++il) cout<<*il<<" "; cout << endl; /***---实际数据---***/ cout<<"---实际数据---"<<endl; Print(li); cout<<"表示并没有删除数据而是向前覆盖!"<<endl; cout<<endl; /**--------------------------------------------------------------------------------**/ int myints3[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 cout<<"myints3 : 10,20,30,30,20,10,10,20"<<endl; vector<int> myvector (8); vector<int>::iterator it,pvend; pvend=remove_copy (myints3,myints3+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0 cout << "myvector contains:"; for (it=myvector.begin(); it!=pvend; ++it) cout << " " << *it; cout << endl; /***---实际数据---***/ cout<<"---实际数据---"<<endl; Print(myvector); cout<<"表示并没有删除数据而是向前覆盖!"<<endl; cout<<endl; /**--------------------------------------------------------------------------------**/ int myints4[] = {1,2,3,4,5,6,7,8,9}; cout<<"myints4 : 1,2,3,4,5,6,7,8,9"<<endl; deque<int> mydeque (9); deque<int>::iterator itd,id; id=remove_copy_if (myints4,myints4+9,mydeque.begin(),bind2nd(modulus<int>(),2)); cout << "mydeque contains:"; for (itd=mydeque.begin(); itd!=id; ++itd) cout << " " << *itd; cout << endl; /***---实际数据---***/ cout<<"---实际数据---"<<endl; Print(mydeque); cout<<"表示并没有删除数据而是向前覆盖!"<<endl; cout<<endl; /**--------------------------------------------------------------------------------**/ return 0;}
0 0
- STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)
- STL_Algorithm3: remove, remove_if, remove_copy, remove_copy_if
- STL 之remove,remove_if,remove_copy,remove_copy_if
- STL 之remove,remove_if,remove_copy,remove_copy_if
- STL之remove、remove_copy、remove_if、remove_copy_if
- 变异算法之删除(remove, remove_if, remove_copy, remove_copy)
- STL常用算法: remove,remove_if,remove_copy,remove_copy_if,remove系列算法和partition的区别...
- remov,remove_if,remove_copy,remove_copy_if
- C++ STL 算法:删除算法(2)remove_copy、remove_copy_if
- [C/C++]_[初级]_[使用remove,remove_if,remove_copy_if过滤元素或字符]
- STL algorithm算法remove_copy,remove_copy_if(48)
- STL算法之—————remove_if, remove, remove_copy_if
- 泛型算法系列19:remove_if()&&remove_copy_if()
- STL_算法(23)_删除_(1)
- STL_算法_删除(unique、unique_copy)
- remove_copy/remove_copy_if详解
- 删除算法 2-remove_copy()
- C++ STL 算法:删除算法(1)remove、remove_if
- PAT 1009
- 【手册】之Openstack常用端口
- 数据结构与算法——二叉树的前序遍历,中序遍历,后序遍历
- Git---具体实例讲解Git用法以及提交PR
- Android之进程通信机制(下)(AIDL,Messenger,Socket)
- STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)
- listener-thread: err = -25572
- websocket实现android A 通过服务器实时控制android B
- 几种主流贴图压缩算法的实现原理
- JAVA学习笔记5--加载第三方jar包
- Bootstrap的图片轮播示例代码
- Structure and inference in annotated networks - note
- SSH中主键生成策略
- 协同过滤算法python实现简单入门详细注释