Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use of STL map.at()

28 views
Skip to first unread message

MikeCopeland

unread,
Feb 21, 2015, 12:13:28 AM2/21/15
to
Is there a way to obtain an index value for an STL map? That is, if
I use myMap.find(some_key) and obtain the container object, is there a
way I can obtain a value from that iterator that allows me to (later)
use the myMap.at() function to re-obtain the data object?
Or, is there a way to calculate and store (in the object) the index
value I can use with myMap.at()?
My purpose here is that there are events where, after I've stored a
map object via the key, I want to reacquire the object where I don't
have access to the key: I've written another data object that doesn't
contain the original map key.
Thoughts? TIA


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

Barry Schwarz

unread,
Feb 21, 2015, 3:03:48 AM2/21/15
to
On Fri, 20 Feb 2015 22:13:05 -0700, MikeCopeland <mrc...@cox.net>
wrote:

> Is there a way to obtain an index value for an STL map? That is, if
>I use myMap.find(some_key) and obtain the container object, is there a

.find() returns an iterator, not the mapped value or the container
object. If you preserve the iterator by either not reusing it or
assigning its value to another iterator, you can access the map
element any time you want.

>way I can obtain a value from that iterator that allows me to (later)
>use the myMap.at() function to re-obtain the data object?

Once you have the iterator, you can obtain the map key with it->first.
If you save the key, you can use it later in a call to .at(). Unless
you have inserted additional elements into the map, using the iterator
seems to be more efficient.

> Or, is there a way to calculate and store (in the object) the index
>value I can use with myMap.at()?

What object are you talking about.

> My purpose here is that there are events where, after I've stored a
>map object via the key, I want to reacquire the object where I don't
>have access to the key: I've written another data object that doesn't
>contain the original map key.

Keys are unique in a map. If you stored a value in a map using the
key, then save the value of the key for later use in retrieving the
value. Storing another value in the map with a different key will not
change the way you can access the original value. Storing a different
value with the same key destroys the first value. There is no way to
get it back.
> Thoughts? TIA
>
>
>---
>This email has been checked for viruses by Avast antivirus software.
>http://www.avast.com

--
Remove del for email

Öö Tiib

unread,
Feb 21, 2015, 12:06:12 PM2/21/15
to
On Saturday, 21 February 2015 07:13:28 UTC+2, MikeCopeland wrote:
> Is there a way to obtain an index value for an STL map? That is, if
> I use myMap.find(some_key) and obtain the container object, is there a
> way I can obtain a value from that iterator that allows me to (later)
> use the myMap.at() function to re-obtain the data object?

The iterators, pointers or references to elements of 'std::map' are quite
fine for storing for later usage. Neither inserts nor erases will
invalidate any iterators or references to 'std::map' elements unless
that element itself was not erased.
Note that it is not the case with 'boost::flat_map' that usually
outperforms 'std::map' but most inserts and erases invalidate its
iterators and references.

> Or, is there a way to calculate and store (in the object) the index
> value I can use with myMap.at()?
> My purpose here is that there are events where, after I've stored a
> map object via the key, I want to reacquire the object where I don't
> have access to the key: I've written another data object that doesn't
> contain the original map key.

Map *is* indexed by keys. If you do not have neither keys nor iterators
then you have insufficient data for to navigate in map.

Norman J. Goldstein

unread,
Feb 21, 2015, 12:56:28 PM2/21/15
to
I haven't tested this, but the prototype is

mapped_type& at (const key_type& k);

so I would expect that the address of the return value never changes
(unless the mapped type is deleted and re-inserted). So, rather than
storing an "index", just store the address of the return value of at,
and no need to use "at" again.


Öö Tiib

unread,
Feb 21, 2015, 1:17:06 PM2/21/15
to
Yes but in that case storing 'K*' that points at key of element and
using it in 'std::map<K,V>::at' (that is O(log N) method) is clearly
wasteful compared to storing 'std::map<K,V>::iterator' or 'std::map<K,V>::pointer' to that element (that take commonly as
little memory) and to reach value with 'it->second' or 'p->second'.
Trying to access erased and inserted element that way is undefined
behavior in all described cases.
0 new messages