c++ - 在 C++ 中的 vector 中的结构中搜索枚举

标签 c++ vector enums find

我有一个 enum,一个以这个 enum 作为成员的结构和这些结构的 vector :

enum TickerType { tt1, tt2, tt3 };
struct Ticker {
  std::string name;
  TickerType type;
};
std::vector<Ticker> vect;

我想在 vect 中搜索类型为 tt1 的元素。为此我声明

struct find_ticker
{
  const TickerType type;
  find_ticker(TickerType type) : type(type) {};
  bool operator () ( const Ticker& ticker ) const
  {
    return ticker.type == type;
  }
};

并查看:

 if ( std::find ( vect.begin(), vect.end(), find_ticker ( tt1 ) ) != tickers.end() ) 

但是我明白了

error: no match for ‘operator==’ (operand types are ‘Dfp::Ticker’ and
 ‘const {anonymous}::find_ticker’) ||   { return *__it == _M_value; }

*Dfp是应用的全局命名空间,find_ticker定义在我需要的实现文件中的匿名命名空间

最佳答案

std::find如果您想使用谓词进行查找,函数是错误的调用函数。 std::find 用于根据值查找,因此您要尝试在 vector 中查找等于 find_ticker 对象的值。

你应该使用 std::find_if相反。

关于c++ - 在 C++ 中的 vector 中的结构中搜索枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614118/

相关文章:

c++ - 创建 std::vector 减去一个元素的拷贝的最快方法

C++ 标准库容器相对于所包含对象的线程安全性

MATLAB:函数返回单个值而不是向量

c++ - 在没有 for 循环的 C++98/03 中以相反的顺序将 C 数组附加到 vector

c++ - 为什么我不能使用 lambda 按值对 std::map 进行排序

c++ - 在 C++ 中实现 matlab hist()

c++ - 在 vector 中查找字符串的索引

TypeScript 使用枚举类型而不是字符串类型迭代字符串枚举

c++ - 按值或引用传递枚举?

javascript - 如何使用枚举值作为数组的索引