As an extension to this question Are const_iterators faster?, I have another question on const_iterators
. How to remove constness of a const_iterator
You can subtract the begin() iterator from the const_iterator to obtain the position the const_iterator is pointing to and then add begin() back to that to obtain a non-const iterator. I don't think this will be very efficient for non-linear containers, but for linear ones such as vector this will take constant time.
vector v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
vector::const_iterator ci = v.begin() + 2;
cout << *ci << endl;
vector::iterator it = v.begin() + (ci - v.begin());
cout << *it << endl;
*it = 20;
cout << *ci << endl;
EDIT: This appears to only work for linear (random access) containers.