count_if使用我自己的类实现第三个参数
count_if third argument implementation using my own class
我在编写函数以正确将参数传递给算法库count_if时遇到问题。
这是我的代码。
int range = count_if(empVect.begin(), empVect.end(), empVect.InRange(200,399));
范围函数实现为:
bool Employee::inRange(double min, double max)
{
if (nIncome > min && nIncome < max)
return true;
return false;
}
编译器错误:类"std::vector>"没有成员"InRange"
我有一个包含 2 个字符串和 3 个双成员变量的 Employee 类。我已经在我的主中创建对矢量创建进行了硬编码,并且需要显示一定收入范围内的员工数量。根据分配要求,仅需要使用 count_if 或 lambda 函数。谢谢。
empVect
是一个std::vector<Employee>
,它没有接受任何东西的InRange
成员函数。该函数是 Employee
类的成员。您需要定义一个函子,该函子将使用您传递的参数在向量的每个实例上调用该成员。一个简单的lambda就可以了:
int range = count_if(empVect.begin(), empVect.end(), [](Employee const& e){
return e.InRange(200,399);
});
您还需要将inRange
修复为 const 成员函数,因为e
是对 const Employee
的引用。渴望编写常量正确的代码。如果成员未修改任何成员数据,则默认将其标记为 const。
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 重载运算符 new 和运算符 delete 带有三个参数
- 三个参数函数模板令人困惑的示例
- 五个中三个最大参数的平均值,不使用排序算法或向量/数组
- C 理解`emove_if()`的第三个参数
- 如何使用三个参数化构造函数的rand()函数初始化对象的数组
- SetPixelFormat() 中的第三个参数对于 OpenGL 上下文创建有什么意义?
- find_if中的模板比较器作为第三个参数
- count_if使用我自己的类实现第三个参数
- 排序函数的第三个参数如何工作
- 在C++标准中,当我同时使用 list.begin() 作为 list.splice() 的第一个和第三个参数时,为什么会发生无限循环
- 接受子引用作为参数的父函数(或三个相互依赖类的冒险)
- 具有三个参数的C 递归二进制搜索
- 函数,它接受三个参数:指向双数组的指针、指向最小值的指针和指向最大值元素的指针
- C++ count_if对象中的第三个参数
- 调用接受三个参数的函数出现问题,其中一个是 int const
- 第三个“SetWindowSubclass”参数中的“uIdSubclass”是什么?
- 我们如何确保传递的 2 个参数被视为第一个和第三个,而第二个 AD 第四个被视为默认值
- 空白或不执行任何操作的三元运算符的第二个或第三个参数
- fcntl() 中的第三个参数是什么