Topic: Obtaining a non-const iterators from a const_iterator


Author: Xavi Gratal <gratal@gmail.com>
Date: Wed, 1 Jan 2014 20:19:56 -0800 (PST)
Raw View
------=_Part_5461_14137664.1388636396581
Content-Type: text/plain; charset=ISO-8859-1

Given a const_iterator ci to an element of a container c, it is already
possible to obtain a non-const iterator with:

auto i=std::advance(c.begin(),std::distance(c.cbegin(),ci);

However, this is extremely inefficient for non-random-access containers. I
would like to add some mechanism to the standard containers so that given a
non-const reference to the container and a const_iterator, it is possible
to obtain a non-const iterator in constant time.
This would be useful in a scenario where an algorithm takes a const
reference to a container and finds some interesting element inside,
returning a const_iterator to it. Then, as long as you have a non-const
reference to the container, you should be able to modify the element you
found. It is already possible to erase it using a const_iterator.

Is there any reason why such a facility doesn't exist for the standard
containers?

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_5461_14137664.1388636396581
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Given a const_iterator ci to an element of a container c, =
it is already possible to obtain a non-const iterator with:<div><br></div><=
div>auto i=3Dstd::advance(c.begin(),std::distance(c.cbegin(),ci);</div><div=
><br></div><div>However, this is extremely inefficient for non-random-acces=
s containers. I would like to add some mechanism to the standard containers=
 so that given a non-const reference to the container and a const_iterator,=
 it is possible to obtain a non-const iterator in constant time.</div><div>=
This would be useful in a scenario where an algorithm takes a const referen=
ce to a container and finds some interesting element inside, returning a co=
nst_iterator to it. Then, as long as you have a non-const reference to the =
container, you should be able to modify the element you found. It is alread=
y possible to erase it using a const_iterator.</div><div><br></div><div>Is =
there any reason why such a facility doesn't exist for the standard contain=
ers?</div><div><br></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_5461_14137664.1388636396581--

.