remove_if的用法,浅谈

1、需要的头文件:

#include <algorithm>

2、参数说明

remove_if的前两个参数不难理解。

第三个参数可以是返回bool的函数指针,也可以是重载=(bool operator() ())的类对象;从remove_if的函数定义往里面跟,可以看到这样一句:if (_Pred(*_First))。

3、示例代码

#include "stdafx.h"

#include <algorithm>

#include <iostream>

#include <vector>

using namespace std;

 

class CTest

{

public:

    CTest(int nData)

    {

        m_nData = nData;

    }

 

    bool operator() (const CTest & test)

    {

        if (test.m_nData > 5)

        {

            return true;

        }

 

        return false;

    }

 

    ~CTest(){};

 

//private:

    int m_nData;

};

 

bool Com(CTest test)

{

    if (test.m_nData > 5)

    {

        return true;

    }

 

    return false;

}

 

bool intCom(int nData)

{

    return (nData > 5);

}

 

int _tmain()

{

 

    vector<CTest> tests;

    vector<int>    inttest;

    for (int i=10; i>0; i--)

    {

        CTest test(i);

        tests.push_back(test);

        inttest.push_back(i);

    }

  

    for (vector<CTest>::iterator it = tests.begin(); it != tests.end(); it++)

    {

        vector<CTest>::iterator itEnd = remove_if(tests.begin(), tests.end(), CTest((*it).m_nData));

        tests.erase(itEnd, tests.end());

    }

 

    for (vector<int>::iterator it = inttest.begin(); it != inttest.end(); it++)

    {

        vector<int>::iterator itEnd = remove_if(inttest.begin(),inttest.end(), intCom);

        inttest.erase(itEnd, inttest.end());

    }

   

    system("pause");

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值