2010-03-03 87 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周围编写一个封装器,或者编写一个比较函子,并在创建集合时将其作为参数提供。