Logo

Programming-Idioms

History of Idiom 52 > diff from v27 to v28

Edit summary for version 28 by ne9xt:
New Cpp implementation by user [ne9xt]

Version 27

2019-09-26, 16:34:32

Version 28

2019-09-26, 16:52:21

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