defining < operator for map of list iterators
Posted
by Adrian
on Stack Overflow
See other posts from Stack Overflow
or by Adrian
Published on 2010-03-29T04:48:05Z
Indexed on
2010/03/29
4:53 UTC
Read the original article
Hit count: 403
I'd like to use iterators from an STL list as keys in a map. For example:
using namespace std;
list<int> l
;
map<list<int>::const_iterator, int> t;
int main(int argv, char * argc) {
l.push_back(1);
t[l.begin()] = 5;
}
However, list iterators do not have a comparison operator defined (in contrast to random access iterators), so compiling the above code results in an error:
/usr/include/c++/4.2.1/bits/stl_function.h:227: error: no match for ‘operator<’ in ‘__x < __y’
If the list is changed to a vector, a map of vector const_iterators compiles fine.
What is the appropriate way to define the operator < for list::const_iterator?
© Stack Overflow or respective owner