1

Why is map a bidirectional data structure. We can easily access any element if we know by using its key, just like a array which is a random access data structure? Then should not a map be a random access data structure, because we can access any of its data without having to visit any of its previous elements.

8
  • I don't know what you call a "bidirectional data structure", but a std::map is in the family of associative containers per the standard. It associates keys to values. You can random-access it all you want but only via the keys. It's iteration is bi-directional only.
    – WhozCraig
    Commented Feb 5, 2020 at 9:54
  • 1
    @mudit rustagi If you want a map that has constant complexity for acessing elements, you might want to take a look at std::unordered_map, it is implemented with a hash table, rather than a tree like std::map Commented Feb 5, 2020 at 9:57
  • @muditrustagi It seems that you are confusing std::map with std::unordered_map. Only the latter is implemented as a "hash map".
    – Fareanor
    Commented Feb 5, 2020 at 10:00
  • 1
    std::map usually is implemented as red-black tree, so item is found using binary search (complexity O(log n)).
    – Marek R
    Commented Feb 5, 2020 at 10:17
  • And std::unordered_map's iteration is forward only, despite it's "random access" lookup
    – Caleth
    Commented Feb 5, 2020 at 10:39

2 Answers 2

3

No.

The concept RandomAccessIterator extends BidirectionalIterator by also requiring that you can add a number to the iterator.

This has no relation to the key_type of either AssociativeContainer or UnorderedAssociativeContainer. What would it mean to add 5 to "mudit"?

2

Why is map a bidirectional data structure

Why do you think map is a "bidirectional data structure"?

Shouldn't C++ Map be a random access data structure

Why do you think it is not?

Why do you assume that "bidirectional" and "random access" are mutually exclusive?

Seriously, whatever source gave you these terms is either wrong, or you misunderstood it. If you won't say where your incorrect assumptions came from, we can't help you with that.

We can easily access any element if we know by using its key, just like a array which is a random access data structure?

Yes, std::map models AssociativeContainer just as you expect.

Then should not a map be a random access data structure

Are you trying to ask why map is not called a "random access data structure"? That's just because we call it an AssociativeContainer, but the meaning is similar.

Are you trying to ask why map is not random access in reality, even though it supports lookup by key? Then you have to tell us what you mean by "random access" apart from being able to lookup by key.

The lookup operation is logarithmic rather than constant complexity, if that's the distinction you want to draw, because map is a sorted associative container.

The fact that map's iterator is bidirectional doesn't affect how you find elements by key. It just means that once you found (the iterator pointing to) an element, you can walk forward or backwards over its neighbours in sorted order.

Not the answer you're looking for? Browse other questions tagged or ask your own question.