Map是像字典一样的数据结构。它是(键,值)对的关联数组,其中每个唯一键仅与单个值相关联。
map :: clear()
clear()函数用于从地图容器中删除所有元素,从而使其大小保持为0。
句法:
map1.clear()
where map1 is the name of the map.
Parameters:
No parameters are passed.
返回值:无
例子:
Input : map1 = {
{1, "India"},
{2, "Nepal"},
{3, "Sri Lanka"},
{4, "Myanmar"}
}
map1.clear();
Output: map1 = {}
Input : map2 = {}
map2.clear();
Output: map2 = {}
// CPP program to illustrate
// Implementation of clear() function
#include
using namespace std;
int main()
{
// Take any two maps
map map1, map2;
// Inserting values
map1[1] = "India";
map1[2] = "Nepal";
map1[3] = "Sri Lanka";
map1[4] = "Myanmar";
// Print the size of map
cout<< "Map size before running function: \n";
cout << "map1 size = " << map1.size() << endl;
cout << "map2 size = " << map2.size() << endl;;
// Deleting the map elements
map1.clear();
map2.clear();
// Print the size of map
cout<< "Map size after running function: \n";
cout << "map1 size = " << map1.size() << endl;
cout << "map2 size = " << map2.size();
return 0;
}
输出:
Map size before running function:
map1 size = 4
map2 size = 0
Map size after running function:
map1 size = 0
map2 size = 0
时间复杂度:线性即O(n)
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。