hi, I need to do this but don't compile...
I'd like every element has an index, and insert and remove they; furthermore I need take it ordered for index (int): how this?Code:
std::map<int,Light> lightmap; lightmap.insert(1,Light());
thanks.
Printable View
hi, I need to do this but don't compile...
I'd like every element has an index, and insert and remove they; furthermore I need take it ordered for index (int): how this?Code:
std::map<int,Light> lightmap; lightmap.insert(1,Light());
thanks.
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.
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...
Quote:
(29): error C2664: 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::insert(std::_Tree<_Traits>::i terator,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>
]
std::map::insert() wants an iterator as the first parameter.
Try this instead:Code:
std::map<int,Light> lightmap; lightmap[1] = Light();
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
I agree, luckily QMap::insert() requires a key instead of iterator.Quote:
Originally Posted by Michiel
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.
is it better use container Pair? thanks
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?
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? thanksCode:
typedef map<int,Light> MAP; map<int,Light>::iterator ll; MAP _map; MAP::iterator _it; _map.insert(MAP::value_type(1,Light()));
Yes:Quote:
Originally Posted by mickey
Code:
_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;Quote:
Originally Posted by jacek
With this:
what am I doing? Have I inserted 3 element in map with key 1? thanksCode:
_map[1] = Light() _map[1] = Light() _map[1] = Light()
The latter. There are no positions in a map.Quote:
Originally Posted by mickey
There is no "end" in a map.Quote:
Originally Posted by mickey
No, you have inserted element with key 1 and then changed it twice.Quote:
Originally Posted by mickey
sorry,
I've 3 Light; now I need to delete the element with key '1';Code:
_map[0] = Light() _map[1] = Light() // == _map.insert(make_pair(1,Light())); //is it th same?? _map[2] = Light()
This??Code:
_map.erase(_map.find(1));
thanks
A question: here I tried the use of end for a map? is this correct? isn't there 'end'? I don't understandQuote:
Originally Posted by jacek
http://www.cppreference.com/cppmap/end.html
You can treat std::map as both a map (i.e. set of pairs) and a sorted list of pairs .Quote:
Originally Posted by mickey