Lev
Lev

Reputation: 663

How to search a map in C++

With this example being the closest I have come to understand how I can search a map in C++, I still need help.

I created a map for a phone book.

map<string,int> PhBook;

And now I am letting the user add names and phones to it.

getline(cin >> ws,name);
cin >> phone;
PhBook[name] = phone;

What I wanna do is to also let the user search for possible names in the map and print "the phone = ...." or the "not found" message accordingly.

So how do I do this? I want the simplest solution since I am a newbie. My difficulty lies to the fact that I don't know how to search the map using the elements(people names).

Upvotes: 1

Views: 11323

Answers (3)

AmirSalar
AmirSalar

Reputation: 347

The simple way is:

if (PhBook[name] != 0)
    cout<<"The phone is: "<<PhBook[name]<<endl;
else
    cout<<"Not found”<<endl;

Upvotes: 0

Masum
Masum

Reputation: 393

Here map returns an iterator to it if found, otherwise it returns an iterator to map::end

map<string,int> ::iterator it;
it = PhBook.find(name);

if (it != PhBook.end()){  
  cout<<"The phone = "<<it->second<<endl;
}else{
  cout<<"Not found"<<endl;
}

Upvotes: 6

nmd_07
nmd_07

Reputation: 706

There are multiple ways you can accomplish finding a key in a STL(Standard Template Library) Map. You can either use methods already provided by the STL library or you can use your own methods to get done the same task. I will try to provide few tips that will help you ease your solution.

1-)As already mentioned by the previous post, you can define an iterator of std::Map and assign result of the find function to an already defined iterator. However, sometimes declaring iterators can become messy. For example, if you ever happen to define a nested map object

std::map<std::string, std::map<std::string, int>> dict;
dict["name"]["phone"] = 1234;

In such case when you want to define an iterator of above type, you will have to declare,

std::map<std::string, std::map<std::string, int>>::iterator it = dict.begin();

Having to spend such much effort just to be able to declare an iterator is insane and time consuming. Fortunately, C++11 came with the auto keyword which automatically deduces types at compile-time. This is the only place where I have found auto keyword to be most beneficial. With auto keyword,

auto it = dict.begin(); /*or*/ auto it = dict.find(name)

is all you need.

2-)Iterators gives you the illusion of pointers when pointers are used on one dimensional consecutive data types such as arrays. However, an STL container may not distribute data consecutively on the memory (linkedlist, binary trees are typical examples), rather they can be all over the heap and stack. To give the illusion of pointer arithmetic and use iterators are useful abstractions. Range-based loop from C++11 can be a good substitute for iterators.

std::map<std::string, int> dict;
for(auto & kv : dict){ /* Program logic goes here */ }

In this case kv will contain key-value pairs and be type of std::pair.

3-)You can use count method of std::Map the check whether a key exists within the map. Difference between count and find is count returns a numerical value whereas find returns an iterator.

Upvotes: 0

Related Questions