在本文中,我们将讨论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; //插入值 TP_1[1] = "Tutorials"; TP_1[2] = "Point"; TP_1[3] = "is an"; TP_1[4] = "education portal"; //映射尺寸 cout<< "Map size before clear() function: \n"; cout << "映射尺寸1 = "<<TP_1.size() << endl; cout << "映射尺寸2 = "<<TP_2.size() << endl; //call clear() to delete the elements TP_1.clear(); TP_2.clear(); //now print the 映射尺寸s cout<< "Map size after applying clear() function: \n"; cout << "映射尺寸1 = "<<TP_1.size() << endl; cout << "映射尺寸2 = "<<TP_2.size() << endl; return 0; }
输出结果
Map size before clear() function: 映射尺寸1 = 4 映射尺寸2 = 0 Map size after applying clear() function: 映射尺寸1 = 0 映射尺寸2 = 0