STL 简单介绍

map 简单用法:

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

using namespace std;

int main()
{
	cout << "第一种方式" << endl;
	map<int, string> mapStudent1;//第一种插入方式
	mapStudent1.insert(pair<int, string>(1, "student1_one"));
	mapStudent1.insert(pair<int, string>(2, "student1_two"));
	mapStudent1.insert(pair<int, string>(3, "student1_three"));
	map<int, string>::iterator  iter1;
	for (iter1 = mapStudent1.begin(); iter1 != mapStudent1.end(); iter1++)
	{
		cout << iter1->first << " " << iter1->second;
		cout << endl;
	}
	cout << endl;


	cout << "第二种方式" << endl;
	map<int, string> mapStudent2;//第二种插入方式
	mapStudent2.insert(map<int, string>::value_type(1, "student2_one"));
	mapStudent2.insert(map<int, string>::value_type(2, "student2_two"));
	mapStudent2.insert(map<int, string>::value_type(3, "student2_three"));
	map<int, string>::iterator  iter2;
	for (iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++)
	{
		cout << iter2->first << " " << iter2->second;
		cout << endl;
	}
	cout << endl;


	cout << "第三种方式" << endl;
	map<int, string> mapStudent3;//第三种插入方式
	mapStudent3[1] = "student3_one";
	mapStudent3[2] = "student3_two";
	mapStudent3[3] = "student3_three";
	map<int, string>::iterator  iter3;
	for (iter3 = mapStudent3.begin(); iter3 != mapStudent3.end(); iter3++)
	{
		cout << iter3->first << " " << iter3->second;
		cout << endl;
	}
	cout << endl;


	cout << "第一种方式的覆盖操作" << endl;
	map<int, string> mapStudent4;
	mapStudent4.insert(pair<int, string>(1, "student4_one"));
	mapStudent4.insert(pair<int, string>(1, "student4_mode"));
	map<int, string>::iterator  iter4 = mapStudent4.begin();
	cout << iter4->first << " " << iter4->second;
	cout << endl; cout << endl;


	cout << "验证查询操作是否成功" << endl;
	map<int, string> mapStudent5;
	pair<map<int, string>::iterator, bool> Insert_Pair;//用来验证是否插入成功
	mapStudent5.insert(map<int, string>::value_type(1, "student5_one"));
	Insert_Pair = mapStudent5.insert(map<int, string>::value_type(1, "student5_mode"));
	map<int, string>::iterator  iter5 = mapStudent5.begin();
	cout << iter5->first << " " << iter5->second;
	cout << endl;
	if ( Insert_Pair.second )//插入成功返回true,若插入的关键字不存在,则会成功
	{
		cout << "Insert No1 succeed." << endl;
	} 
	else
	{
		cout << "Insert No1 failed." << endl;
	}
	Insert_Pair = mapStudent5.insert(map<int, string>::value_type(2, "student5_mode"));
	if (Insert_Pair.second)
	{
		cout << "Insert No2 succeed." << endl;
	}
	else
	{
		cout << "Insert No2 failed." << endl;
	}
	cout << iter5->first << " " << iter5->second;
	cout << endl;
	iter5 ++;
	cout << iter5->first << " " << iter5->second;
	cout << endl;
	cout << endl;


	cout << "第三种方式的覆盖操作" << endl;
	map<int, string> mapStudent6;//第三种方式则会覆盖相同关键字的数据
	map<int, string>::iterator  iter6;
	mapStudent6[1] = "student6_one";
	iter6 = mapStudent6.begin();
	cout << iter6->first << " " << iter6->second;
	cout << endl;
	mapStudent6[1] = "student6_mode";
	cout << iter6->first << " " << iter6->second;
	cout << endl;
	cout << endl;


	cout << "查询mapStudent1中数据的个数" << endl;
	int nSize = 0;//用来查询map中数据的个数(即map的大小)
	nSize = mapStudent1.size();
	cout << nSize << endl;
	cout << endl;


	cout << "mapStudent1的反向索引" << endl;
	map<int, string>::reverse_iterator  iter7;
	for (iter7 = mapStudent1.rbegin(); iter7 != mapStudent1.rend(); iter7++)
	{
		cout << iter7->first << " " << iter7->second;
		cout << endl;
	}
	cout << endl;


	cout << "数组方式mapStudent1的索引" << endl;
	for (int nIndex = 1; nIndex <= nSize; nIndex++)
	{
		cout << mapStudent1[nIndex];
		cout << endl;
	}
	cout << endl;


	cout << "find方法查询mapStudent1" << endl;
	map<int, string>::iterator  iter8;
	iter8 = mapStudent1.find(2);
	cout << iter8->first << " " << iter8->second;
	cout << endl;
	cout << endl;

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值