If_life 2020-05-09 12:03 采纳率: 0%
浏览 88

STL c++11 function object 有关判断式

代码是写一个function object


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class myPredicate
{
private:
    int initNum;
    int count;
public:
    myPredicate(int i)
    {
        count = i;
        initNum = 0;
    }

    bool operator()(int ) //一定要加int或者int i,为何???   之前写过返回值是int的function object不需要加
    {
        return ++initNum == count;
    }
};

int main()
{
    vector<int>myVector = {1,2,3,4,5,6,7,8,9};

    for (int i : myVector) cout << i << " ";

    remove_if(myVector.begin(), myVector.end(), myPredicate(3));//调用后不会复原,可能VS2010上会复原,我用的是2017

    cout << endl;

    for (int i : myVector) cout << i << " ";

    system("pause");
}

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-05-09 12:14
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题