public member function
<map>

std::map::clear

void clear();
void clear() noexcept;
Clear content
Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.

Parameters

none

Return value

none

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
25
26
// map::clear
#include <iostream>
#include <map>

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

  mymap['x']=100;
  mymap['y']=200;
  mymap['z']=300;

  std::cout << "mymap contains:\n";
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  mymap.clear();
  mymap['a']=1101;
  mymap['b']=2202;

  std::cout << "mymap contains:\n";
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  return 0;
}

Output:
mymap contains:
x => 100
y => 200
z => 300
mymap contains:
a => 1101
b => 2202


Complexity

Linear in size (destructions).

Iterator validity

All iterators, pointers and references related to this container are invalidated.

Data races

The container is modified.
All contained elements are modified.

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also