std::map 用法

添加元素

  • 用insert函数插入pair数据
#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle.insert(std::pair<int,std::string>(1,"hello"));

	return 0;
}
  • 用insert函数插入value_type数据

#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle.insert(std::map<int,std::string>::value_type(1,"hello"));

	return 0;
}
  • 用数组方式插入数据
#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";

	return 0;
}

遍历元素

#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";
	mapEle[2] = "world";
	mapEle[3] = "你好";
	mapEle[4] = "世界";

	for (auto iter = mapEle.begin(); iter != mapEle.end(); iter++)
	{
		int nKey = iter->first;					//key值
		std::string strValue = iter->second;	//value值
	}

	return 0;
}

删除元素

  • 通过迭代器删除指定元素

#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";
	mapEle[2] = "world";
	mapEle[3] = "你好";
	mapEle[4] = "世界";

	//通过迭代器删除
	auto iter = mapEle.erase(mapEle.begin());//返回下一个元素的迭代器

	return 0;
}
  • 通过key删除指定元素
#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";
	mapEle[2] = "world";
	mapEle[3] = "你好";
	mapEle[4] = "世界";

	//通过key删除
	int n = mapEle.erase(2);//如果删除了会返回1,否则返回0  

	return 0;
}
  • 通过迭代器删除指定范围元素
#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";
	mapEle[2] = "world";
	mapEle[3] = "你好";
	mapEle[4] = "世界";

	//通过迭代器删除
	auto iter = mapEle.erase(mapEle.begin(),mapEle.end());//返回下一个元素的迭代器

	return 0;
}

查找元素

#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";
	mapEle[2] = "world";
	mapEle[3] = "你好";
	mapEle[4] = "世界";

	//查找key值为2的元素
	auto iter = mapEle.find(2);
	if (iter != mapEle.end())
	{
		int nKey = iter->first;					//key值
		std::string strValue = iter->second;	//value值
	}
	else
		std::cout << "不存在" << std::endl;

	return 0;
}

判断元素是否存在

#include <iostream>
#include <map>
#include <string>

int main(int argc, char **argv)
{
	std::map<int, std::string> mapEle;
	mapEle[1] = "hello";
	mapEle[2] = "world";
	mapEle[3] = "你好";
	mapEle[4] = "世界";

	//判断容器中是否存在key值为2的元素
	bool bRt = mapEle.count(2);
	if (bRt)
		std::cout << "存在" << std::endl;
	else
		std::cout << "不存在" << std::endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值