zoukankan      html  css  js  c++  java
  • std::remove_if

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    #include<vector>
    using namespace std;
    
    int main(){
        int a[11]={1,2,3,4,99,5,6,99,7,8,99};
        vector<int,allocator<int>>arr(a,a+11);
    //    c
        cout<<&arr<<endl;
        for(int i=0;i<arr.size();i++){
            cout<<arr[i]<<" ";
        }
        cout<<endl;
        //remove_if( arr.begin(),  arr.end(),not1(std::bind2nd( std::less< int>(), 99)));
        arr.erase( std::remove_if( arr.begin(),  arr.end(),std::not1(std::bind2nd( std::less< int>(), 99))), arr.end());
        //remove_if(arr.begin(),arr.end(),bind2nd(less<int>(),99));
        cout<<&arr<<endl;
        cout<<arr.size()<<endl;
        for(int i=0;i<arr.size();i++){
            cout<<arr[i]<<" ";
        }
    
    }

    当将arr。ereas注释保留

    remove_if( arr.begin(),  arr.end(),not1(std::bind2nd( std::less< int>(), 99)));的时候

    因此可以看出remove_if并不是删除指定的元素,只不过是将后面的元素不断向前面进行覆盖,结束后返回符合条件的最后一个元素的下一个位置
    ,此时在配合erase一起使用,删除后面不符合的元素

    remove_if()类似于partition(), 但有两点不同: 1) 它们使用的谓词条件刚好相反. 2) remove_if只强调前面部分(第二部分不再需要了)
    remove_if()以线性时间(linear time)运行.
    remove_if()不能用于关联容器如set<>或map<>.

  • 相关阅读:
    git学习笔记
    ubuntu常用命令
    hdfs[命令] fsck
    hdfs[命令] dfsadmin
    hdfs[命令] dfs
    Hadoop2.0新特性-持续追加【干货】
    Cloudera 建议使用 NTP 使 Hadoop 群集实现时间同步
    Cloudera CDH5 部署实战指南(离线安装)
    没有用户画像,别谈精准营销
    用户画像数据建模方法
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/6388121.html
Copyright © 2011-2022 走看看