If I have a structure like
std::map<string, int> myMap; myMap["banana"] = 1; myMap["apple"] = 1; myMap["orange"] = 1;
How can I access myMap[0]?
I know that the map sorts internally and I'm fine with this, I want to get a value in the map by index. I've tried myMap[0] but I get the error:
Error 1 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
I realise I could do something like this:
string getKeyAtIndex (int index){ map<string, int>::const_iterator end = myMap.end(); int counter = 0; for (map<string, int>::const_iterator it = myMap.begin(); it != end; ++it) { counter++; if (counter == index) return it->first; } }
But surely this is hugely inefficient? Is there a better way?
Index inside map() Function The index is used inside map() method to state the position of each element in an array, but it doesn't change the original array. Syntax: array. map(function(currentelement, index, arrayobj) { // Returns the new value instead of the item });
map's don't have an index, but an iterator.
The index identifies quadrangle areas by map name and scale. A map is usually named after the most prominent city, town or natural landmark shown on it. Topographic map indexes are located at the first table in the Map Room (TOPO MAP INDEXES).
Your map
is not supposed to be accessed that way, it's indexed by keys not by positions. A map
iterator is bidirectional, just like a list
, so the function you are using is no more inefficient than accessing a list
by position. If you want random access by position then use a vector
or a deque
.
Your function could be written with help from std::advance(iter, index)
starting from begin()
:
auto it = myMap.begin(); std::advance(it, index); return it->first;
There may be an implementation specific (non-portable) method to achieve your goal, but not one that is portable.
In general, the std::map
is implemented as a type of binary tree, usually sorted by key. The definition of the first element differs depending on the ordering. Also, in your definition, is element[0] the node at the top of the tree or the left-most leaf node?
Many binary trees are implemented as linked lists. Most linked lists cannot be directly accessed like an array, because to find element 5, you have to follow the links. This is by definition.
You can resolve your issue by using both a std::vector
and a std::map
:
std::map
.std::vector
at the position you want it at.The std::map
will allow an efficient method to access the object by key.
The std::vector
will allow an efficient method to access the object by index. Storing pointers allows for only one instance of the object instead of having to maintain multiple copies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With