2010-03-03 84 views
1

我想插入一個載體爲一組這樣的:插入對象爲一組

set<vector<prmEdge> > cammini; 
vector<prmEdge> vecEdge; 
cammini.insert(vecEdge); 

我有這樣的編譯錯誤:

prmPlanner.cpp:1285: instantiated from here 
/usr/include/c++/4.2/bits/stl_algobase.h:853: error: no match for ‘operator<’ in ‘__first1.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = const prmEdge*, _Container = std::vector<prmEdge, std::allocator<prmEdge> >]() < __first2.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = const prmEdge*, _Container = std::vector<prmEdge, std::allocator<prmEdge> >]()’ 
/usr/include/c++/4.2/bits/stl_algobase.h:855: error: no match for ‘operator<’ in ‘__first2.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = const prmEdge*, _Container = std::vector<prmEdge, std::allocator<prmEdge> >]() < __first1.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = const prmEdge*, _Container = std::vector<prmEdge, std::allocator<prmEdge> >]()’ 
make[1]: *** [prmPlanner.o] Errore 1 

我應該怎麼辦? 有人能幫助我嗎?

非常感謝你

回答

5

它不知道如何比較矢量。應提供operator<vector<prmEdge>(或prmEdge自動使用std::lexicographical_compare爲載體),或使用unordered_set如果你實際上並不需要有序集合向量。

+1

但有一個非成員操作符<()定義爲vector:http://stdcxx.apache.org/doc/stdlibref/vector.html#idx1350。不應該那樣工作? –

+0

如何用unordered_set聲明一個集合? – livio8495

+2

@Fred Larson,它使用'std :: lexicographical_compare'進行比較,所以應該爲'prmEdge'定義'operator <'。 –

0

包含在組中的對象必須具有operator <定義。

0

std::set需要它項可排序。然而,std::vector不可排序。

0

std::set需要一個謂詞元素進行排序。默認情況下,這是<,因此您需要爲vector<prmEdge>定義operator <。您也可以提供自定義謂詞到std::set,請參閱here

0

同意前面的答案,只是想補充一點,你可以創建全球bool operator<(vector<T> v)

1

由於您setvector S,即沒有限定operator<的元素,你需要做兩件事情之一:在定義operator<vector周圍編寫一個封裝器,或者編寫一個比較函子,並在創建集合時將其作爲參數提供。