我想在向量中找到一个结构,但是我遇到了一些麻烦。我读了几篇关于这个的帖子,但是这些都搜索了struct的一个元素:我希望能够在搜索时比较struct的多个元素。我的结构和向量定义为:
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;
然后我想在向量订阅中搜索结构,但由于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&gt;,_Tp = echo_client_handler :: subscription]((echo_client_handler) *)this) - &gt; echo_client_handler :: subscriptions.std :: vector&lt; _Tp,_Alloc&gt; ::以_Tp = echo_client_handler :: subscription开头,_Alloc = std :: allocator,std :: vector&lt; _Tp,_Alloc&gt; :: iterator = __gnu_cxx :: __ normal_iterator&gt;,typename std :: _ Vector_base&lt; _Tp,_Alloc&gt; :: _ Tp_alloc_type :: pointer = echo_client_handler :: subscription *,((echo_client_handler *)this) - &gt; echo_client_handler :: subscriptions.std :: vector&lt; _Tp,_Alloc&gt; :: end with _Tp = echo_client_handler :: subscription,_Alloc = std :: allocator,std :: vector&lt; _Tp,_Alloc&gt; :: iterator = __gnu_cxx :: __ normal_iterator&gt ;,typename std :: _ Vector_base&lt; _Tp,_Alloc&gt; :: _ Tp_alloc_type :: pointer = echo_client_handler :: subscription *,(*(const echo_client_handler :: subscription *)(&amp; match)))'
还有更多:)所以操作员没有正确定义,但应该怎么做?谁能帮我?如何搜索多个元素而不是只搜索结构的一个元素?
答案 0 :(得分:6)
您可能没有为it
指定类型吗?
std::vector<subscription>::iterator it =
find(subscriptions.begin(), subscriptions.end(), match);