How to Check If Std::Map Contains a Key Without Doing Insert

How to check if std::map contains a key without doing insert?

Use my_map.count( key ); it can only return 0 or 1, which is essentially the Boolean result you want.

Alternately my_map.find( key ) != my_map.end() works too.

Determine if map contains a value for a key?

Does something along these lines exist?

No. With the stl map class, you use ::find() to search the map, and compare the returned iterator to std::map::end()

so

map<int,Bar>::iterator it = m.find('2');
Bar b3;
if(it != m.end())
{
//element found;
b3 = it->second;
}

Obviously you can write your own getValue() routine if you want (also in C++, there is no reason to use out), but I would suspect that once you get the hang of using std::map::find() you won't want to waste your time.

Also your code is slightly wrong:

m.find('2'); will search the map for a keyvalue that is '2'. IIRC the C++ compiler will implicitly convert '2' to an int, which results in the numeric value for the ASCII code for '2' which is not what you want.

Since your keytype in this example is int you want to search like this: m.find(2);

How to find if a given key exists in a C++ std::map

Use map::find and map::end:

if (m.find("f") == m.end()) {
// not found
} else {
// found
}

check if map contains a certain value

You can use std::find_if to find if a value exists in a std::map or not shown below:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>

int main()
{
// Create a map of three strings (that map to integers)
std::map<int, int> m { {1, 10}, {2, 15}, {3, 300}, };

int value = 300;
auto result = std::find_if(std::begin(m), std::end(m), [value](const auto& mo) {return mo.second == value; });

if(result != std::end(m))
{
std::cout<<"found"<<std::endl;
}
else
{
std::cout<<"not found"<<std::endl;
}
}

The output of the above program can be seen here.

Why use `std::map::find` for checking if maps have a key?

Is there something about the latter that makes it safer, or is the
first only applicable to pointers keys and values?

Yes, std::map::operator[] performs an insertion if no key exists. And the std::map::find does not.

From cppreference.com the std::map::operator[]

Returns a reference to the value that is mapped to a key equivalent to
key, performing an insertion if such key does not already exist.

How to see if a key is present in a map?

Use map::find as:

for(i=a.begin(); i!=a.end(); i++)
{
if( b.find(i->first) != b.end() )
std::cout << (*i).first << " also present in b" << std::endl;
}

Note i->first and (*i).first are same.

C++ - how to tell whether a key exists in map of maps

You could improve it a little via:

std::map<int, std::map<int, int>>::iterator it = my_map.find(3);
if (it != my_map.end()){
if (it->second.find(5) != internal_map.end()) {
}
}

In your current solution, you are finding the key twice and it could be slower than finding it once and store it in iterator.

Check if map contains all the keys from another map

If you want to iterate the 2 maps simultaneously, you can do this:

if (map1.size() != map2.size())
; // problem
else
{
for (map<X,Y>::const_iterator it1 = map1.begin(),
it2 = map2.begin();
it1 != map1.end() && it2 != map2.end();
++it1 , ++it2)
{
// ...
}
}

Now if you want to iterate through the 2 maps at different "speeds", then a while loop to condition the increments of it1 and it2 independently would then be more appropriate. See Golgauth's answer for an example.

Way to find if map contains any keys NOT from a list

Instead of using ! on the list, why don't you try ..

boolean allKeysInList = map.entrySet()
.stream()
.allMatch(entry -> list.contains(entry.getKey()));

allKeysInList will be TRUE if all of the keys in the map are in the list and FALSE if not.



Related Topics



Leave a reply



Submit