2012-04-28 17 views
0

我想從std :: remove和std :: erase中刪除一個元素。 我不知道索引,只有價值。然而,它不起作用,它可能與我的自定義結構的運算符有關,但我不知道如何解決它。使用std :: erase和std :: remove從vector刪除元素不使用定製struct作爲值

問題是這樣的行代碼:

stillAvailable.erase(remove(stillAvailable.begin(), stillAvailable.end(), previousRound[j]), stillAvailable.end()); 

哪裏stillAvailable是類型的std ::向量和previousRound [j]的的是類型的組中。團隊是一個結構,看起來像這樣:

struct Team 
{ 
    int country; 
    std::string name; 
    int positionGP; 
    int groupID; 
}; 

我收到以下錯誤代碼:

League.cc: In member function `bool League::generateSchema(int, std::vector<Team, std::allocator<Team> >)': 
League.cc:131: error: no match for 'operator*' in '*temp' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h: In function `_OutputIterator std::remove_copy(_InputIterator, _InputIterator, _OutputIterator, const _Tp&) [with _InputIterator = __gnu_cxx::__normal_iterator<Team*, std::vector<Team, std::allocator<Team> > >, _OutputIterator = __gnu_cxx::__normal_iterator<Team*, std::vector<Team, std::allocator<Team> > >, _Tp = Team]': 
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:1114: instantiated from `_ForwardIterator std::remove(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<Team*, std::vector<Team, std::allocator<Team> > >, _Tp = Team]' 
League.cc:145: instantiated from here 
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:1037: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __value' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h: In function `_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Team*, std::vector<Team, std::allocator<Team> > >, _Tp = Team]': 
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:314: instantiated from `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = __gnu_cxx::__normal_iterator<Team*, std::vector<Team, std::allocator<Team> > >, _Tp = Team]' 
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:1112: instantiated from `_ForwardIterator std::remove(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = __gnu_cxx::__normal_iterator<Team*, std::vector<Team, std::allocator<Team> > >, _Tp = Team]' 

League.cc:145: instantiated from here 
C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:207: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:211: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:215: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:219: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:227: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:231: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

C:/Dev-Cpp/include/c++/3.4.2/bits/stl_algo.h:235: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Team*, _Container = std::vector<Team, std::allocator<Team> >]() == __val' 

任何幫助將不勝感激!提前致謝!

+0

什麼是第133行?一些帶'* temp'的指針...請發佈一些(縮寫)代碼。 – 2012-04-28 15:19:15

回答

0

std::remove使用operator==,它不會自動爲您的類和結構重載。你將不得不自己寫:

bool operator==(const Team & lhs, const Team & rhs) 
{ 
    // compare lhs and rhs in such a way that you return true if they 
    // are equal, and false if they are not equal. You can define 
    // equal to mean whatever you want here. 
} 
+0

太好了,謝謝!我不確定如何定義這樣的功能,但你的答案非常幫助我!再次感謝! – rbnvrw 2012-04-29 10:59:16

相關問題