Question on Map 
Author Message
 Question on Map

If you insert a value into std::map and the key is already present, no
insertion takes place and the value is not replaced. The value that was
inserted first stays in the map.

You can do the insertion this way:

myMap[1] = x;
myMap[1] = y;

The first statement inserts a new, default constucted, element at key 1,
and immediately replaces it with x. The second finds the existing
element and replaces the value with y. Thus, your map stores the latest
inserted value for the key.

If you want to hold multiple values for the same key, use std::multimap
instead.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> hi,

> I have a map and if I keep inserting it with same key but different
values,
> would it erase the previous record?

> If not, then I would have >1 copies of record with the same key. And
how can
> I retreive the latest copy of the record.

> That is, if I do,

> insert(1, x);
> insert(2,y);
> insert(1,y);
> insert(1,k);

> if I do find(1) what would I get? I think I will get x. But is there
any way
> for me to get k instead, ie. the latest copy? Also, is there a way for
me to
> get y as well?

> Thanks in advance.



Tue, 05 Apr 2005 00:26:30 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Some questions regarding map, list, iterators, the universe, and everything

2. Just learning STL, question about map

3. a question about map

4. Chicago Map's Precision Mapping OCX

5. Memory-mapped files - pros and cons of mapping view on whole file

6. stl::map - can't map int to char *

7. Accessing map elements in vector of maps

8. map::mapped_type vs. map::referent_type

9. map::erase( map::end() ) is it harmless?

10. Problem with using map .erase inside a map loop

11. inserting into map of maps

12. Memery mapped map

 

 
Powered by phpBB® Forum Software