【发布时间】:2018-03-28 10:06:14
【问题描述】:
我有一个带有自定义数据类型对象的向量。此数据类型的字段之一是枚举。我想确保对于所有枚举值,至少将一个条目添加到向量中。
我想使用std::find_if() 检查向量是否有某个枚举值的条目。但我不确定如何正确编写和使用 lambda。这是我得到的:
struct CustomType
{
EnumType type {EnumType::DEFAULT};
int x;
};
std::vector<CustomType> tmp;
// fetch values from database and add them to the vector
// for some EnumType values there might not be a record added to the vector
auto hasEnumType = [](const CustomType& customType, const EnumType& enumType) -> bool
{
return (customType.type == enumType);
};
// What do I replace '?' with to get the current position
if (std::find_if(tmp.begin(), tmp.end(), hasEnumType(?, EnumType::VALUE1)) != tmp.end())
{
//add CustomType with VALUE1 to vector
}
【问题讨论】:
标签: c++ algorithm c++11 lambda find