Solved

urgent:search value in map

Posted on 2011-09-11
3
310 Views
Last Modified: 2012-05-12
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
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Infinity08
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.
0
 

Author Comment

by:zizi21
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.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 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.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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