check if an element exists in a map c++ code example

Example 1: check if map key has alue cpp

#include

int main(){

  	map m;
  	
  	char ch = '!';
  
	if (m.find(ch) != m.end()) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}
  
	return 0;
}

Example 2: check if an element exists in a map c++

#include 
#include 
#include 
 
int main()
{
    std::unordered_map m;
 
    std::string s("abcba");
    std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });
 
    char ch = 's';
 
    if (m.find(ch) != m.end()) {
        std::cout << "Key found";
    } else {
        std::cout << "Key not found";
    }
 
    return 0;
}

Tags:

Misc Example