urgent:search value in map

Posted on 2011-09-11
Medium Priority
Last Modified: 2012-05-12
Hi there,

I have a map like this:
 map<int,int> months;
I know we can find the key using the find function. months[key] but is it possible find the value of the months ?

Is it possible to search for value in a map ?
Question by:zizi21
  • 2
LVL 53

Expert Comment

ID: 36521019
Sure. If you iterate over the entire map, and check every item in it.

But probably that's not what you want. Most likely, you are using the wrong data structure for your needs.

Author Comment

ID: 36521042
I need to search for key and at times, the value. Is there any data structure that allows  me to do that?

I searched and there was boost bimap but they stated that make sure that this is what you are really after as it is a bit slow due to the redundancy being kept.
LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 36521566
There is indeed a trade-off to be taken into account. If you need to be able to efficiently perform lookups on both key and value, then you'll pay for that elsewhere (eg. in memory footprint, insertion time, ...).

If you only rarely have to look up a value, and the map is relatively small, and you can live with a slower performance when looking up by value, then you can keep using a map, and simply iterate over the entire contents to find the value.
If any of those preconditions are not true though, you'll likely be better off placing the trade-off elsewhere by using a different data structure.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question