共用方式為


unordered_map::clear

Removes all elements.

void clear();

Remarks

The member function calls unordered_map::erase( unordered_map::begin(), unordered_map::end()).

Example

 

// std_tr1__unordered_map__unordered_map_clear.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
 
typedef std::unordered_map<char, int> Mymap; 
int main() 
    { 
    Mymap c1; 
 
    c1.insert(Mymap::value_type('a', 1)); 
    c1.insert(Mymap::value_type('b', 2)); 
    c1.insert(Mymap::value_type('c', 3)); 
 
// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
// clear the container and reinspect 
    c1.clear(); 
    std::cout << "size == " << c1.size() << std::endl; 
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; 
    std::cout << std::endl; 
 
    c1.insert(Mymap::value_type('d', 4)); 
    c1.insert(Mymap::value_type('e', 5)); 
 
// display contents " [e 5] [d 4]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    std::cout << "size == " << c1.size() << std::endl; 
    std::cout << "empty() == " << std::boolalpha << c1.empty() << std::endl; 
 
    return (0); 
    } 
 
 [c, 3] [b, 2] [a, 1]
size == 0
empty() == true

 [e, 5] [d, 4]
size == 2
empty() == false

Requirements

Header: <unordered_map>

Namespace: std

See Also

Reference

<unordered_map>

unordered_map Class

unordered_map::erase