You were redirected to cplusplus.com/map::count || See search results for: "map::count"
public member function
<map>

std::map::count

size_type count (const key_type& k) const;
Count elements with a specific key
Searches the container for elements with a key equivalent to k and returns the number of matches.

Because all elements in a map container are unique, the function can only return 1 (if the element is found) or zero (otherwise).

Two keys are considered equivalent if the container's comparison object returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).

Parameters

k
Key to search for.
Member type key_type is the type of the element keys in the container, defined in map as an alias of its first template parameter (Key).

Return value

1 if the container contains an element whose key is equivalent to k, or zero otherwise.

Member type size_type is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// map::count
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> mymap;
  char c;

  mymap ['a']=101;
  mymap ['c']=202;
  mymap ['f']=303;

  for (c='a'; c<'h'; c++)
  {
    std::cout << c;
    if (mymap.count(c)>0)
      std::cout << " is an element of mymap.\n";
    else 
      std::cout << " is not an element of mymap.\n";
  }

  return 0;
}


Output:
a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is not an element of mymap.
e is not an element of mymap.
f is an element of mymap.
g is not an element of mymap.

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data races

The container is accessed.
No mapped values are accessed: concurrently accessing or modifying elements is safe.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.

See also