Topic: iterator container::access(const_iterator); member


Author: Maurice Bos <m-ou.se@m-ou.se>
Date: Sat, 24 Aug 2013 13:39:57 +0200
Raw View
--047d7b3a8976c1c56504e4affc29
Content-Type: text/plain; charset=ISO-8859-1

Hello,

Since C++11, a lot of member functions of containers that used to take
(non-const) iterators, now take const_iterators (such as insert, erase,
etc.), since those member functions can only be called on non-const
containers anyway and can thus modify it.

However, what seems to be missing is to get a (non-const) iterator, given a
non-const (reference to) a constainer and a const_iterator in it. For
example:

vector<int> a;
vector<int>::const_iterator ci = a.cbegin();
vector<int>::iterator i = a.access(ci); // turn it into a non-const
iterator.

Although this is already possible, there is currently no cross-container
way do this in O(1):

For a vector: auto i = a.begin() + (ci - a.begin());
For a list: auto i = a.erase(ci, ci); // This is only guaranteed to be O(n)
for a std::vector, although in most implementations this will be O(1)

I needed this when i was making a class template called 'vectorset', which
has the interface of a std set, but keeps its contents in a vector (or
list, or deque, or anything else with a vector-like interface, depending on
a template parameter). (It has less efficient inserting and erasing
obviously, but it's more memory efficient and probably a better choice for,
for example, a (mostly) constant set of just a few integers.) To make the
insert member function take a const_iterator as hint, i needed to convert
it to a non-const iterator, which can currently not be done in one way
that's O(1) for all containers.

Has anybody else encountered such a situation? Any arguments against adding
this funcitonality?

Kind regards,
Maurice Bos

--

---
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/.

--047d7b3a8976c1c56504e4affc29
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Hello,<br><br>Since C++11, a lot of member functions =
of containers that used to take (non-const) iterators, now take const_itera=
tors (such as insert, erase, etc.), since those member functions can only b=
e called on non-const containers anyway and can thus modify it.<br>

<br>However, what seems to be missing is to get a (non-const) iterator, giv=
en a non-const (reference to) a constainer and a const_iterator in it. For =
example:<br><br>vector&lt;int&gt; a;<br></div>vector&lt;int&gt;::const_iter=
ator ci =3D a.cbegin();<br>

vector&lt;int&gt;::iterator i =3D a.access(ci); // turn it into a non-const=
 iterator.<br><br>Although this is already possible, there is currently no =
cross-container way do this in O(1):<br><br>For a vector: auto i =3D a.begi=
n() + (ci - a.begin());<br>

For a list: auto i =3D a.erase(ci, ci); // This is only guaranteed to be O(=
n) for a std::vector, although in most implementations this will be O(1)<br=
><br>I needed this when i was making a class template called &#39;vectorset=
&#39;, which has the interface of a std set, but keeps its contents in a ve=
ctor (or list, or deque, or anything else with a vector-like interface, dep=
ending on a template parameter). (It has less efficient inserting and erasi=
ng obviously, but it&#39;s more memory efficient and probably a better choi=
ce for, for example, a (mostly) constant set of just a few integers.) To ma=
ke the insert member function take a const_iterator as hint, i needed to co=
nvert it to a non-const iterator, which can currently not be done in one wa=
y that&#39;s O(1) for all containers.<br>

<br>Has anybody else encountered such a situation? Any arguments against ad=
ding this funcitonality?<br><br>Kind regards,<br>Maurice Bos<br></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 />

--047d7b3a8976c1c56504e4affc29--

.