📜  C++ STL中的multimap clear()函数

📅  最后修改于: 2021-05-07 05:39:55             🧑  作者: Mango

multimap clear()函数是C++ STL中的内置函数,用于从multimap容器中删除所有元素(已销毁),使该容器的大小为0。

句法 :

mymultimap_name.clear()

参数:此函数不接受任何参数。

返回值:该函数不返回任何内容。该函数的返回类型为void。它只是清空整个容器。

下面的程序说明了C++中的multimap :: clear()函数:

// CPP program to illustrate the
// multimap::clear() function
  
#include 
#include 
#include 
  
using namespace std;
  
int main()
{
    // Creating multimap of string and int
    multimap mymultimap;
  
    // Inserting 3 Items with their value
    // using insert function
    mymultimap.insert(pair("Item1", 10));
    mymultimap.insert(pair("Item2", 20));
    mymultimap.insert(pair("Item3", 30));
  
    cout << "Size of the multimap before using "
         << "clear function : ";
    cout << mymultimap.size() << '\n';
  
    // Removing all the elements
    // present in the multimap
    mymultimap.clear();
  
    cout << "Size of the multimap after using"
         << " clear function : ";
    cout << mymultimap.size() << '\n';
  
    return 0;
}
输出:
Size of the multimap before using clear function : 3
Size of the multimap after using clear function : 0