Map容器常用函数

Map容器常用函数,第1张

Map容器常用函数
#include 
#include 
#include 
using namespace std;

int main()
{
	// 声明
	map m_map;
	// 插入元素
	m_map.insert(make_pair(1, "abc"));
	m_map.insert(pair(2,"bcd"));
	// 迭代器
	map::iterator ite;
	for (ite=m_map.begin();ite!=m_map.end();ite++)
	{
		int key = ite->first;
		string value = ite->second;
		cout << key << "," << value << endl;
	}
	// map大小
	int size = m_map.size();
	cout << size << endl;
	// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
	ite = m_map.find(1);
	cout << ite->second << endl;
	//迭代器刪除
	ite = m_map.find(1);
	m_map.erase(ite);
	//用关键字刪除
	int n = m_map.erase(1); //如果刪除了會返回1,否則返回0
	// map 是否为空
	bool stat = m_map.empty();
	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5651746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存