gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:42:26 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com