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

📅  最后修改于: 2021-05-30 15:53:08             🧑  作者: Mango

unordered_multimap :: clear()是C++ STL中的内置函数,用于清除unordered_multimap容器的内容。调用该函数后,容器的最终大小为0。

句法:

unordered_multimap_name.clear()

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

返回值:不返回任何内容。

下面的程序说明了上述函数:

程序1:

// C++ program to illustrate the
// unordered_multimap::clear()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts key an delement
    sample.insert({ 10, 100 });
    sample.insert({ 10, 100 });
    sample.insert({ 20, 200 });
    sample.insert({ 30, 300 });
    sample.insert({ 15, 150 });
  
    cout << "Key and Elements of multimap are:";
  
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "\n{" << it->first << ", " << it->second << "}";
    }
  
    sample.clear();
  
    cout << "\nSize of container after function call: "
         << sample.size();
  
    return 0;
}
输出:
Key and Elements of multimap are:
{15, 150}
{30, 300}
{20, 200}
{10, 100}
{10, 100}
Size of container after function call: 0

程式2:

// C++ program to illustrate the
// unordered_multimap::clear()
#include 
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap sample;
  
    // inserts element
    sample.insert({ 'a', 'b' });
    sample.insert({ 'a', 'b' });
    sample.insert({ 'b', 'c' });
    sample.insert({ 'r', 'a' });
    sample.insert({ 'c', 'b' });
  
    cout << "Key and Elements of multimap are:";
  
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "\n{" << it->first << ", " << it->second << "}";
    }
  
    sample.clear();
  
    cout << "\nSize of container after function call: "
         << sample.size();
    return 0;
}
输出:
Key and Elements of multimap are:
{c, b}
{r, a}
{b, c}
{a, b}
{a, b}
Size of container after function call: 0
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”