I have the following code, in which I hold a std::set of iterators to a STL container of ints, and erase an element by key (where the key is an iterator to the STL container). The code compiles and runs as expected (compiler is VC 2008) if the container is std::vector, but fails to compile if the container is a std::list
using namespace std;
typedef vector< int > Container; //--> fails to compile if 'vector' is changed to 'list'
typedef Container::iterator IntIt;
set< IntIt > itSet;
Container c;
c.push_back (1);
c.push_back (2);
c.push_back (3);
IntIt it = c.begin ();
itSet.insert (it++);
itSet.insert (it++);
itSet.erase (c.begin ()); //--> The problematic line
The compile error is:
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const IntIt'
So it seems to me the error is because the compiler interprets one of the template's < as smaller than operator - but I can't really understand why, or how to fix it.
list::iteratorimplementation. - Naveenstd::setof iterator (of vector), because once the vector resizes itself, all the iterators in the set would be invalidated, and you never know when the vector resizes itself. I'm not saying that its impossible to know when it happens, but it wouldn't be elegant to have such design that requires this knowledge. If you tell us the goal as to what exactly you're trying to do, maybe we could suggest some better solutions. - Nawazc.begin()toc.end(). - Bo Persson