tags:

views:

57

answers:

6

I want to insert a vector into a set like this:

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

I have a compilation error like this:

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

What should I do? Could somebody help me??

Thank you very much

+5  A: 

It doesn't know how to compare vectors. You should supply operator< for vector<prmEdge> (or for prmEdge to automatically use std::lexicographical_compare for vectors) or use unordered_set if you don't actually need sorted set of vectors.

Kirill V. Lyadvinsky
But there is a non-member operator<() defined for vector: http://stdcxx.apache.org/doc/stdlibref/vector.html#idx1350. Shouldn't that work?
Fred Larson
how to declare a set with unordered_set?
livio8495
@Fred Larson, it uses `std::lexicographical_compare` for comparing, so should be defined `operator<` for `prmEdge`.
Kirill V. Lyadvinsky
@Kirill: Ah, I understand. It's not vector that needs operator<() defined, it's prmEdge. Thank you.
Fred Larson
@livio8495, `unordered_set` is not part of current standard, but it supported in most popular compilers. You could use `boost::unordered_set` as alternative.
Kirill V. Lyadvinsky
ok, thank you very much
livio8495
A: 

The object contained in the set must have operator < defined.

Didier Trosset
A: 

std::set needs its entries to be sortable. The std::vector is however not sortable.

Vlad
A: 

std::set needs a predicate to sort elements. By default that's <, so you would need to define operator < for vector<prmEdge>. You can supply a custom predicate to std::set as well, see here.

Alexander Gessler
A: 

agree with previous answers, just want to add that you can create global bool operator<(vector<T> v)

Andrey
+1  A: 

Since the elements in your set are vectors, which don't define operator<, you'll need to do one of two things: write a wrapper around vector that defines operator<, or else write a comparison functor, and supply it as a parameter when you create your set.

Jerry Coffin