2012-07-05 31 views
3

我想在向量中找到一個結構,但是我遇到了一些麻煩。我閱讀了幾篇有關這方面的文章,但是這些都搜索結構的一個元素:我希望能夠在搜索時比較結構的多個元素。我的結構和矢量被定義爲:查找向量中的結構

​​

然後我要搜索的矢量訂閱一個結構,但由於會話ID和matchid是獨特的組合,我需要尋找兩者。只搜索一個會導致多個結果。

subscription match; 
    match.tournamentid = 54253876; 
    match.sessionid = 56066789; 
    match.matchid = 1108; 
    subscriptions.push_back(match); 

    it = find(subscriptions.begin(), subscriptions.end(), match); 

查找功能在編譯過程中提供以下錯誤:

main.cpp:245:68: error: no match for ‘operator=’ in ‘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>::begin 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*, ((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)))’

和一大堆更多:)所以操作者沒有正確定義,而是應該如何是做什麼?誰能幫我?如何搜索多個元素而不是結構中的一個元素?

+0

你可以發佈你的實際代碼像ideone.com?這個代碼在VC10上爲我編譯。 – Naveen 2012-07-05 08:54:10

+0

您的代碼不完整。 「it」的定義是什麼? – ybungalobill 2012-07-05 08:55:05

+2

你可以請出示'it'的聲明嗎? – thiton 2012-07-05 08:55:08

回答

6

可能是您沒有指定類型爲it

std::vector<subscription>::iterator it = 
    find(subscriptions.begin(), subscriptions.end(), match); 
+0

謝謝...我真是愚蠢!我將它定義爲'vector :: iterator它;'現在正在編譯! – Rogier 2012-07-05 09:01:07