0

Possible Duplicate:
How to remove constness of const_iterator?

I have a find-like member function that returns an iterator. Since it should not modify the container it must return a const_iterator. How can I convert this one into a iterator e.g. to use it for

myNonConstVector.erase(nonConstIterator) 

I know this issue is solved in C++11 but I unfortunately cannot use the new standard yet.

Thanks for the help, Martin

Community
  • 1
  • 1
Martin
  • 4,526
  • 4
  • 27
  • 55
  • 2
    Write two member functions, one const returning a const-iterator, and one non-const returning a regular iterator. – Viktor Sehr Mar 13 '12 at 17:09
  • Just saw this question http://stackoverflow.com/q/765148/484230. If there is not better way, please regard as duplicate – Martin Mar 13 '12 at 17:09
  • @Martin: You should close this question, as it is a exact duplicate. – David Rodríguez - dribeas Mar 13 '12 at 17:10
  • @Viktor I want no function to take a non const reference to the vector, so this is not an option. The vector is not a member. – Martin Mar 13 '12 at 17:11
  • 1
    @Martin: There is something wrong if you only want const references to vectors **but** you intend on modifying it by removing elements. Why would you want to *lie* on your interfaces? – David Rodríguez - dribeas Mar 13 '12 at 17:12
  • @David: I cannot close the question (more votes needed), please go ahead. Concerning your reply: No there is nothing wrong. Consider a member function of class Foo that browses a non-member container std::vector searching for an element matching a Foo object. It returns an iterator. This iterator can be used by the calling code (which has a non const access to the container) to delete the element from the container – Martin Mar 13 '12 at 17:15

0 Answers0