PDA

View Full Version : map



mickey
28th May 2006, 18:02
hi, I need to do this but don't compile...


std::map<int,Light> lightmap;
lightmap.insert(1,Light());

I'd like every element has an index, and insert and remove they; furthermore I need take it ordered for index (int): how this?
thanks.

Michiel
28th May 2006, 18:33
If it's a sequence of indices from 0 (or 1, I suppose), you might want to use a vector instead. It'll have faster inserts and lookups.

However, if you want to keep using the map, please post the entire code and the compiler error. I can't see anything wrong with the code you posted.

mickey
28th May 2006, 19:35
Yes I need a map because I need erase eg element 4 and 6 and compact map (is this automatically?). Then I need re-insert elemt 4 and 6 in their position...

(29): error C2664: 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::insert(std::_Tree<_Traits>::iterator,const std::_Tree<_Traits>::value_type &)' : cannot convert parameter 1 from 'int' to 'std::_Tree<_Traits>::iterator'
with
[
_Traits=std::_Tmap_traits<int,Light,std::less<int>,std::allocator<std::pair<const int,Light>>,false>
]
and
[
_Traits=std::_Tmap_traits<int,Light,std::less<int>,std::allocator<std::pair<const int,Light>>,false>
]

jacek
28th May 2006, 19:45
std::map::insert() wants an iterator as the first parameter.

Try this instead:
std::map<int,Light> lightmap;
lightmap[1] = Light();

Michiel
28th May 2006, 19:57
Of course. I always forget about that. :p (To be honest, I think it's a bit illogical.)

Here's a link that might come in handy: Click (http://www.cppreference.com/)

jacek
28th May 2006, 20:02
To be honest, I think it's a bit illogical.
I agree, luckily QMap::insert() requires a key instead of iterator.

Michiel
28th May 2006, 20:18
Of course, if you already happen to have an iterator, the insert will be faster. But they could have overloaded the function to accept a key as well.

mickey
30th May 2006, 01:11
is it better use container Pair? thanks

mickey
2nd June 2006, 17:26
Hi, I'm trying to use map, but I've got some problems; I'd like to insert in _map one element at begin. Is there a way more simple to do this below?


typedef map<int,Light> MAP;
map<int,Light>::iterator ll;
MAP _map;
MAP::iterator _it;
_map.insert(MAP::value_type(1,Light()));

Furhermore, After i inerted some elements in map I need add one other at the end and one at position eg 4; how to use insert to do this? thanks

jacek
2nd June 2006, 18:29
Is there a way more simple to do this below?
Yes:
_map[1] = Light();

mickey
2nd June 2006, 18:54
Yes:
_map[1] = Light();
sorry bu i don't understand: am I inserting an object Light at position 1 or an obj Light with key 1? I need insert element at the end;
With this:


_map[1] = Light()
_map[1] = Light()
_map[1] = Light()

what am I doing? Have I inserted 3 element in map with key 1? thanks

jacek
2nd June 2006, 19:01
am I inserting an object Light at position 1 or an obj Light with key 1?
The latter. There are no positions in a map.


I need insert element at the end;
There is no "end" in a map.


Have I inserted 3 element in map with key 1? thanks
No, you have inserted element with key 1 and then changed it twice.

mickey
2nd June 2006, 19:08
sorry,


_map[0] = Light()
_map[1] = Light() // == _map.insert(make_pair(1,Light())); //is it th same??
_map[2] = Light()

I've 3 Light; now I need to delete the element with key '1';

_map.erase(_map.find(1)); This??
thanks

jacek
2nd June 2006, 19:13
http://www.cppreference.com/cppmap/erase.html

mickey
3rd June 2006, 19:40
There are no positions in a map.
There is no "end" in a map.

A question: here I tried the use of end for a map? is this correct? isn't there 'end'? I don't understand
http://www.cppreference.com/cppmap/end.html

jacek
3rd June 2006, 20:45
here I tried the use of end for a map? is this correct? isn't there 'end'? I don't understand
You can treat std::map as both a map (i.e. set of pairs) and a sorted list of pairs .