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


在本文中,我们将讨论 C++ STL 中 multimap::clear() 函数的工作原理、语法和示例。

什么是 C++ STL 中的 Multimap?

多重映射是关联容器,类似于映射容器。它还有助于按特定顺序存储由键值和映射值组合而成的元素。在一个多重映射容器中,可以有多个元素与同一个键关联。数据的内部排序始终借助于其关联键进行。

什么是 multimap::clear()?

multimap::clear() 函数是 C++ STL 中的一项内置函数,定义在 <map> 头文件中。clear() 用于从关联的多重映射容器中删除所有内容。此函数会删除所有值,并使容器的大小为 0。

语法

Map_name.clear();

参数

此函数不接受任何参数。

返回值

此函数不返回值

输入 

multimap<char, int > newmap;
newmap.insert(make_pair(‘a’, 1));
newmap.insert(make_pair(‘b’, 2));
newmap.insert(make_pair(‘c’, 3));
newmap.clear();

输出 

size of the multimap is: 0

示例

 在线演示

#include<iostream>
#include<map&g;
using namespace std;
int main(){
   multimap<int,int > mul_1;
   //inserting elements to multimap1
   mul_1.insert({1,10});
   mul_1.insert({2,20});
   mul_1.insert({3,30});
   mul_1.insert({4,40});
   mul_1.insert({5,50});
   cout << "Multimap size before using clear function : ";
   cout <<mul_1.size() << '\n';
   mul_1.clear();
   cout << "Multimap size after using clear function : ";
   cout << mul_1.size() << '\n';
}

输出

如果我们运行上述代码,将生成以下输出:

Multimap size before using clear function : 5
Multimap size after using clear function : 0

更新日期:2020 年 4 月 22 日

198 次浏览

开启你的 职业

完成该课程,获得认证

开始
广告
© . All rights reserved.