When I access a value in std::map at a given key, does the map search for this value linearly by checking each key-value pair if the key match or does it access the requested value directly?
1 Answer
Neither. It will have to search through an index to find the value, but it will do that in a way, that is much more efficient than a linear search.
Typically, this is implemented using a Red-black tree and search time will be logarithmic to the number of elements in the map.
std::mapwould be implemented as a tree. It would search the tree using the comparator to know where to look, using the key.