STL 之remove,remove_if,remove_copy,remove_copy_if

作用:从序列中删除指定元素。

声明:

  1. #include   
  2. template <class forwardItr,class Type>  
  3. forwardItr remove(forwardItr first, forwardItr last, const Type& value);  
  4.   
  5. template <class forwardItr, class unaryPredicate>  
  6. forwardItr remove_if(forwardItr first, forwardItr last, unaryPredicate op);  
  7.   
  8. template <class inputItr,class outputItr,class Type>  
  9. outputItr remove_copy(inputItr first1, inputItr last1, outputItr destFirst, const Type& value);  
  10.   
  11. template <class inputItr,class outputItr, class unaryPredicate>  
  12. outputItr remove_copy_if(inputItr first1, inputItr last1, outputItr destFirst, unaryPredicate op);  

示例代码:

  1. #include   
  2. #include   
  3.   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9.   
  10. #include   
  11.   
  12. using namespace std;  
  13.   
  14. bool lessThanEqualTo50(int num) {  
  15.     return (num <= 50);  
  16. }  
  17.   
  18. int main() {  
  19.     char cList[10] = {'A','a','A','B','A','c','D','e','F','A'};  
  20.   
  21.     vector<char> charList(cList, cList + 10);  
  22.     vector<char>::iterator lastElem,newLastElem;  
  23.     ostream_iterator<char> screen(cout, " ");  
  24.   
  25.     cout << "charList:" << endl;  
  26.     copy(charList.begin(),charList.end(),screen);  
  27.     cout << endl;  
  28.     // remove:去除容器中的所有的A  
  29.     // 函数返回新区间最后一个元素的下一个位置  
  30.     lastElem = remove(charList.begin(),charList.end(),'A');  
  31.     cout << "charList remove A:" << endl;  
  32.     copy(charList.begin(),lastElem,screen);  
  33.     cout << endl;  
  34.   
  35.     // remove_if:去掉所有的大写字母  
  36.     // 返回新区间的最后一个元素的下一个位置。  
  37.     newLastElem = remove_if(charList.begin(),charList.end(),isupper);  
  38.     cout << "charList remove if Upper:" << endl;  
  39.     // 显示新区间的所有元素  
  40.     copy(charList.begin(),newLastElem,screen);  
  41.     cout << endl;  
  42.   
  43.     int list[10] = {12,34,56,21,34,78,34,55,12,25};  
  44.     vector<int> intList(list,list+10);  
  45.     vector<int>::iterator endElement,newEndElement;  
  46.     ostream_iterator<int> screenInt(cout, " ");  
  47.   
  48.     cout << "intList:" << endl;  
  49.     copy(intList.begin(),intList.end(),screenInt);  
  50.     cout << endl;  
  51.   
  52.     vector<int> temp1(10);  
  53.     // 将 intList 中除值为34以外的元素,输出到temp1中,不改变 intList。  
  54.     endElement = remove_copy(intList.begin(),intList.end(),temp1.begin(),34);  
  55.     cout << "temp1:" << endl;  
  56.     copy(temp1.begin(),endElement,screenInt);  
  57.     cout << endl;  
  58.     cout << "intList:" << endl;  
  59.     copy(intList.begin(),intList.end(),screenInt);  
  60.     cout << endl;  
  61.   
  62.     vector<int> temp2(10,0);  
  63.     // remove_copy_if  
  64.     // 将intList中大于50的元素输送到 temp2中。  
  65.     newEndElement = remove_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqualTo50);  
  66.     cout << "temp2:" << endl;  
  67.     copy(temp2.begin(),temp2.end(),screenInt);  
  68.     cout << endl;  
  69.   
  70.     cout << "temp2:" << endl;  
  71.     copy(temp2.begin(),newEndElement,screenInt);  
  72.     cout << endl;  
  73.   
  74.     return 0;  
  75. }  

运行结果:

charList:
A a A B A c D e F A
charList remove A:
a B c D e F
charList remove if Upper:
a c e e
intList:
12 34 56 21 34 78 34 55 12 25
temp1:
12 56 21 78 55 12 25
intList:
12 34 56 21 34 78 34 55 12 25
temp2:
56 78 55 0 0 0 0 0 0 0
temp2:
56 78 55


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部