Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

urgent:search value in map

Hi there,

I have a map like this:
 map<int,int> months;
months[key]=value.
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 ?
0
zizi21
Asked:
zizi21
  • 2
1 Solution
 
Infinity08Commented:
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.
0
 
zizi21Author Commented:
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.
0
 
Infinity08Commented:
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.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now