在本文中,我们将讨论C ++ STL中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;
   //将元素插入到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