Checking Value Exist in a Std::Map - C++

Checking value exist in a std::map - C++

You can use boost::multi_index to create a bidirectional map - you can use either value of the pair as a key to do a quick lookup.

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.

How to determine whether a value exists in map in C++

Q1: How to check if a value exists in hashmap?

You need to iterate through and check if such item exists. You can use `std::find_if() with a lambda or do that through a loop. If you do that quite often you may want to index values as well (see below)

Q2: How to iterate through all the values in a map and find the largest value or the smallest value ?

Again you iterate through container and find it or you can use std::max_element() or std::min_element() with a lambda as well. Though if you need to access values in sorted order you may consider to use boost::multimap which will allow to access data using hashed index by name and provide sorted or hashed index(es) for values, though you should be aware of a price of every index added.

How to check if a value exists within a C++ Map

valueFinder is just an iterator for the type std::map<int,int> that is not associated to any instance of that type.

To associate it to an instance (here fibonacci_storage) you have to assign it to that instance, i.e.

valueFinder = fibonacci_storage.begin();

Finding an element can be done with source

valueFinder = fibonacci_storage.find(value);

where value is the key you are searching for. Now you check if value is in the map:

if( valueFinder != fibonacci_storage.end() )
{
// value found
}

and you're done.

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
}

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 whether an element exists in std::map?

Sure, use an iterator

map<string,Car>::const_iterator it = cars.find(name);
return it!=cars.end();

Check if element exists in C++ map?

if (myMap.find(key) != myMap.end())
{ // there is such an element
}

See the reference for std::map::find

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.

Checking whether an element exist or not in the map within map

You can use std::map::find together with short-circuit evaluation:

bool foundXY = instensityValue.find(x) != intensityValue.end() &&
intensityValue[x].find(y) != intensityValue[x].end();

or std::map::count:

bool foundXY = instensityValue.count(x) && intensityValue[x].count(y)


Related Topics



Leave a reply



Submit