Determine If Map Contains a Value for a Key

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 check if a map contains a key in Go?

One line answer:

if val, ok := dict["foo"]; ok {
//do something here
}

Explanation:

if statements in Go can include both a condition and an initialization statement. The example above uses both:

  • initializes two variables - val will receive either the value of "foo" from the map or a "zero value" (in this case the empty string) and ok will receive a bool that will be set to true if "foo" was actually present in the map

  • evaluates ok, which will be true if "foo" was in the map

If "foo" is indeed present in the map, the body of the if statement will be executed and val will be local to that scope.

How can I find out if a map contains a given value?

std::map only indexes its elements by key; it does not index them by value. Therefore, there is no way to look up an element by its value without iterating over the map.

Take a look at Boost.Bimap:

Boost.Bimap is a bidirectional maps library for C++. With Boost.Bimap you can create associative containers in which both types can be used as key. A bimap<X,Y> can be thought of as a combination of a std::map<X,Y> and a std::map<Y,X>.

Using it is pretty straightforward, although you will of course need to consider the question of whether duplicate values are allowed.

Also, see Is there a Boost.Bimap alternative in c++11?

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.

Is it possible to check if a JS Map contains a value and have it return its key

I'm doing it this way to search for a certain value that could have many terms it might not be the best way its what I cam up with basically many values could be entered into an input but all relate to only one output

Based on a comment, I'm going to recommend a different way to build your lookup structure -

const terms =
[ ["KEY1", ["dummy1","dummy2","dummy3"]]
, ["KEY2", ["dummy4","dummy5","dummy6","dummy7"]]
, ["KEY3", ["dummy8","dummy9"]]
]

const dict =
new Map(terms.flatMap(([ k, vs ]) => vs.map(v => [ v, k ])))

console.log(dict.get("dummy2"))
console.log(dict.get("dummy5"))
console.log(dict.get("dummy7"))
console.log(dict.get("dummy9"))
console.log(dict.get("dummy0"))

check if the map has a specific string in it's keys, then give me it's value, that belong this key, Dart

Map<String, String> ListFinalAllInfos = {'stackoverflow': 'one', 'google': 'two'};
String key = ListFinalAllInfos.containsKey("stackoverflow"); // search for the key. for example stackoverflow
String value = ListFinalAllInfos[key]; // get the value for the key, value will be 'one'
if(ListFinalAllInfos.containsKey(value)){ //check if there is a key which is the value you grabbed
return true;
}

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.

Check in Map of list if exists value for specific key Java 8

You can simply use m.get("Name2"), place the (nullable) result into an Optional and then use a mapping:

boolean result = Optional.ofNullable(m.get("Name2"))
.map(l -> l.stream().anyMatch(s -> s.contains("@")))
.orElse(false);

This is preferable to looping over the entry set, as HashMap.get is O(1) and iterating over the entry set is O(n).

How can I find out if a List of Maps contain a specific value for key?

Direct way to check

if (list[0].containsKey("id")) {
if (list[0]["id"] == 3) {
// your list of map contains key "id" which has value 3
}
}

And for indirect way you need to iterate through the loop like this:

for (var map in list) {
if (map?.containsKey("id") ?? false) {
if (map!["id"] == 3) {
// your list of map contains key "id" which has value 3
}
}
}


Related Topics



Leave a reply



Submit