c ++ stl_从地图删除元素| C ++ STL

这篇博客介绍了如何在C++的STL中删除map的元素,包括使用`std::map::erase()`函数删除单个或范围元素,以及用`std::map::clear()`函数清空整个map。
摘要由CSDN通过智能技术生成

c ++ stl

We can use std::map::erase() function to erase single element or range of elements in a map. If we want to delete all the elements in a map we can use std::map::clear() function. Let discuss these functions in detail.

我们可以使用std :: map :: erase()函数来删除地图中的单个元素或元素范围。 如果要删除地图中的所有元素,可以使用std :: map :: clear()函数。 让我们详细讨论这些功能。

1. std :: map :: erase()函数 (1. std::map::erase() function )

It removes the element from a map and reduces the size of a map by 1.

它将元素从地图中删除,并将地图大小减小1。

Syntax:

句法:

    //Erase by using iterator: 
    MapName.erase (it);
    //Where ‘it’ is iterator of type map.

    //Erasing by key :
    MapName.erase (key);

    //Erasing by range :
    MapName.erase (first,last);
    //Where the range includes all the elements of the map 
    //between first and lasti.e[first,last).

Program:

程序:

#include <bits/stdc++.h>
using namespace std;

int main ()
{

	map<char,int> MyMap;

	// insert some elements in map
	MyMap['a']=1;
	MyMap['b']=2;
	MyMap['c']=3;
	MyMap['d']=4;
	MyMap['e']=5;
	MyMap['f']=6;
	MyMap['g']=7;

	map<char,int>::iterator it;

	cout<<"Elements in map : "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}
	cout<<endl;  
	
	//delete using key
	MyMap.erase ('f');

	// erasing by iterator
	it=MyMap.find('e');
	MyMap.erase (it); 

	cout<<"Elements in map after deleting f and e: "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}
	cout<<endl;  
	
	// erasing by range
	// note that last is not inclusive
	//delete elements from a to c 
	it = MyMap.find('d');             
	MyMap.erase ( MyMap.begin() , it );   

	cout<<"Elements in map after deleting a to c : "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}

	return 0;
}

Output

输出量

Elements in map :
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7

Elements in map after deleting f and e:
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = g value = 7

Elements in map after deleting a to c :
key = d value = 4
key = g value = 7


2. std :: map :: clear()函数 (2. std::map::clear() function )

This function removes all elements from a map and reduces map size to 0.

此功能从地图上删除所有元素,并将地图大小减小为0。

Syntax:

句法:

    MapName.clear();
    //There is no parameter to pass.

Program:

程序:

#include <bits/stdc++.h>
using namespace std;

int main ()
{
	map<char,int> MyMap;

	// insert some elements in map
	MyMap['a']=1;
	MyMap['b']=2;
	MyMap['c']=3;
	MyMap['d']=4;
	MyMap['e']=5;
	MyMap['f']=6;
	MyMap['g']=7;

	map<char,int>::iterator it;

	cout<<"Elements in map : "<<endl;
	for (it = MyMap.begin(); it != MyMap.end(); it++) 
	{ 
		cout  <<"key = "<< it->first <<" value = "<< it->second <<endl; 
	}
	cout<<endl;

	//erasing all elements    
	MyMap.clear();

	cout<<"After applying MyMap.clear() function : "<<endl;
	if(MyMap.size() == 0 )
		cout<<"map is empty !!!";

	return 0;
}

Output

输出量

Elements in map :
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7

After applying MyMap.clear() function :
map is empty !!!


翻译自: https://www.includehelp.com/stl/delete-elements-from-a-map.aspx

c ++ stl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值