Logo

Programming-Idioms

History of Idiom 52 > diff from v35 to v36

Edit summary for version 36 by programming-idioms.org:
Admin deletes impl 2902: We want to search in values, but this code searches in keys

Version 35

2020-04-26, 17:31:43

Version 36

2020-04-26, 17:37:20

Idiom #52 Check if map contains value

Determine whether map m contains an entry with value v, for some key.

Illustration

Idiom #52 Check if map contains value

Determine whether map m contains an entry with value v, for some key.

Illustration
Extra Keywords
table dictionary hash
Extra Keywords
table dictionary hash
Imports
#include <iostream>
#include <unordered_map>
Code
int main()
{  
    // simple comparison demo
    std::unordered_map<int,char> example = {{1,'a'},{2,'b'}};

    size_t nElements = example.count(2);
    if (nElements) {
        std::cout << "Found " << nElements << " values." << '\n';
    } else {
        std::cout << "Not found\n";
    }
}
Doc URL
https://en.cppreference.com/w/cpp/container/unordered_map/count