C++ STL 中的 map::clear()
在本文中,我们将讨论 C++ STL 中 map::clear() 函数的工作原理、语法和示例。
什么是 C++ STL 中的映射?
映射是一种关联容器,它有助于以特定顺序存储由键值和映射值组合而成的元素。在映射容器中,数据在内部始终按其关联键的帮助进行排序。通过其唯一键访问映射容器中的值。
什么是 map::clear()?
map::clear() 函数是 C++ STL 中的一个内置函数,在中定义
语法
Map_name.clear();
参数
此函数不接受任何参数。
返回值
此函数不返回值
示例
输入
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.clear();
输出
size of the map is: 0
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, string> TP_1, TP_2;
//Insert values
TP_1[1] = "Tutorials";
TP_1[2] = "Point";
TP_1[3] = "is an";
TP_1[4] = "education portal";
//size of map
cout<< "Map size before clear() function: \n";
cout << "Size of map1 = "<<TP_1.size() << endl;
cout << "Size of map2 = "<<TP_2.size() << endl;
//call clear() to delete the elements
TP_1.clear();
TP_2.clear();
//now print the size of maps
cout<< "Map size after applying clear() function: \n";
cout << "Size of map1 = "<<TP_1.size() << endl;
cout << "Size of map2 = "<<TP_2.size() << endl;
return 0;
}输出
Map size before clear() function: Size of map1 = 4 Size of map2 = 0 Map size after applying clear() function: Size of map1 = 0 Size of map2 = 0
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP