Ajay Kumar
Ajay Kumar

Reputation: 29

What is the role of Keys in containers and what exactly is a key?

I know it may sound awkward but i don't really get the concept of Keys in map and any role of keys in associative containers, that's why i am unable to understand the concept of containers.

is there anyway that there might be a simple explanation to what is a key and how does it plays its role in Containers?

Upvotes: 0

Views: 66

Answers (1)

ruohola
ruohola

Reputation: 24048

Think of a map key like a dictionary key. You need to look up some information and you know the key so you simply scroll to the page that has the key and look up the information under it.

In real dictionaries you can easily find the page with the correct key since the keys are in a nice alphabetical order. In ordered maps it's a quite similar concept since the keys are for example in a specific sorted tree like structure that makes finding the right one quite fast. In unordered hashmaps the contents are in an array and you pass the key through a special function to get the index of the array where the value corresponding to the key is.

In all map like structures, like C++ std::map, Python dict and Java HashMap the key is the thing you know and the value is the content behind the key.

Upvotes: 3

Related Questions