【发布时间】:2012-07-05 15:34:58
【问题描述】:
我想在向量中找到一个结构,但我遇到了一些麻烦。我阅读了几篇关于此的文章,但这些文章都在搜索结构的一个元素:我希望能够在搜索时比较结构的多个元素。我的结构和向量定义为:
struct subscription {
int tournamentid;
int sessionid;
int matchid;
bool operator==(const subscription& m) const {
return ((m.matchid == matchid)&&(m.sessionid==sessionid)&&(m.tournamentid==tournamentid));
}
};
vector<subscription> subscriptions;
然后我想在向量订阅中搜索一个struct,但是由于sessionid和matchid的组合是唯一的,所以我需要两者都搜索。只搜索一个会导致多个结果。
subscription match;
match.tournamentid = 54253876;
match.sessionid = 56066789;
match.matchid = 1108;
subscriptions.push_back(match);
it = find(subscriptions.begin(), subscriptions.end(), match);
find 函数在编译时出现如下错误:
main.cpp:245:68: 错误: 'it = std::find [with _IIter = __gnu_cxx::__normal_iterator >, _Tp = echo_client_handler::subscription](((echo_client_handler* )this)->echo_client_handler::subscriptions.std::vector<_tp _alloc>:: 以 _Tp = echo_client_handler::subscription 开头,_Alloc = std::allocator, std::vector<_tp _alloc>::iterator = __gnu_cxx::__normal_iterator >, typename std::_Vector_base<_tp _alloc>::_Tp_alloc_type::pointer = echo_client_handler::subscription*, ((echo_client_handler*)this)->echo_client_handler::subscriptions.std::vector<_tp _alloc>::end with _Tp = echo_client_handler::subscription, _Alloc = std::allocator, std::vector<_tp _alloc>::iterator = __gnu_cxx::__normal_iterator >, typename std::_Vector_base<_tp _alloc>: :_Tp_alloc_type::pointer = echo_client_handler::subscription*, (*(const echo_client_handler::subscription*)(& match)))'
还有更多:) 所以操作符定义不正确,但是应该怎么做呢?谁能帮我?如何搜索多个元素而不是只搜索结构的一个元素?
【问题讨论】:
-
你能在 ideone.com 之类的网站上发布你的实际代码吗?这段代码在 VC10 上为我编译。
-
您的代码不完整。
it的定义是什么? -
能否请您出示
it的声明? -
我假设你已经将
it定义为vector<subscription>::iterator..你已经做到了吗? -
愚蠢的错误...我将其定义为
vector<int>::iterator it;...感谢您向我展示错误!