c++ - STL vector erase via pointer -


i understand (at least think do) pointers can used random stl iterators.

why following code not compile unless cast pointer iterator?

vector<int> v{1, 2, 3}; v.erase(&v[0]); 

you can pass pointers algorithms std::sort, std::find, or std::copy. these templates configurable works appropriate iterator.

this not mean different iterators convert each other.

the method erase of std::vector<int> container can work iterators elements of same vector. has been pointed out may implemented pointer, not, reasons stated here: c++ std::vector<>::iterator not pointer, why?

consider std::find:

template< class inputit, class t > inputit find( inputit first, inputit last, const t& value ); 

here inputit template parameter. template std::find work iterator type fulfills requirements of input iterator, , operator* returns can compared type t. pointers work here. fire lancer pointed out correctly in comment, both first , last must of type inputit.

now compare std::vector::erase:

iterator erase( const_iterator pos ); 

this takes const_iterator, 1 of typedefs of std::vector class. 1 particular type of iterator, not template parameter.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -