clear在c语言中用法,C++ map::clear()用法及代码示例

Map是字典一样的数据结构。它是(键,值)对的关联数组,其中每个唯一键仅与单个值相关联。

map::clear()

clear()函数用于从Map容器中删除所有元素,从而使其大小保持为0。

用法:

map1.clear()

where map1 is the name of the map.

参数:

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值