C++中map的常用方法

引用地址:http://fhqllt.iteye.com/blog/964510

/************************************************************************/
/*
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,
第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,
在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),
这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
1. map的构造函数  
map<int, string> maphai;
map<char,int> maphai;
map<string,char> mapstring;
map<string,int> mapstring;
map<int ,char>mapint;
map<char,string>mapchar;  
*/
/************************************************************************/

#include <map>
#include <string>
#include <iostream>
using namespace std;

int main(){	

	// 第一种:用insert函数插入pair数据
	map<int, string> mapStudent;	
	mapStudent.insert(pair<int, string>(1, "student_one"));	
	mapStudent.insert(pair<int, string>(2, "student_two"));	
	mapStudent.insert(pair<int, string>(3, "student_three"));
	mapStudent.insert(pair<int,string>(3,"student_four"));
	map<int, string>::iterator iter;
	for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
		cout<<iter->first<<""<<iter->second<<endl;
	}

	// 用insert函数插入value_type数据,下面举例说明
	map<int, string> mapStudent1;	
	mapStudent1.insert(map<int, string>::value_type (1, "student_one"));	
	mapStudent1.insert(map<int, string>::value_type (2, "student_two"));	
	mapStudent1.insert(map<int, string>::value_type (3, "student_three"));
	mapStudent1.insert(map<int,string>::value_type (3,"student_four"));
	map<int, string>::iterator iter1;	
	for(iter1 = mapStudent.begin(); iter1 != mapStudent.end(); iter1++){		
		cout<<iter1->first<<""<<iter1->second<<endl;	
	}

	// 用数组方式插入数据,下面举例说明
	map<int, string> mapStudent2;	
	mapStudent2[1] = "student_one";
	mapStudent2[2] = "student_two";
	mapStudent2[3] = "student_three";
	mapStudent2[3] = "student_four";
	map<int, string>::iterator iter2;	
	for(iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++){
		cout<<iter2->first<<""<<iter2->second<<endl;
	}

	// 演示插入成功与否问题
	map<int, string> mapStudent3;
	pair<map<int, string>::iterator, bool> Insert_Pair;
	Insert_Pair = mapStudent3.insert(pair<int, string>(1, "student_one"));
	if(Insert_Pair.second == true){		
		cout<<"Insert Successfully"<<endl;
	}else{
		cout<<"Insert Failure"<<endl;
	}
	Insert_Pair = mapStudent3.insert(pair<int, string>(1, "student_two"));
	if(Insert_Pair.second == true){		
		cout<<"Insert Successfully"<<endl;
	}else{
		cout<<"Insert Failure"<<endl;
	}
	map<int, string>::iterator iter3;
	
	for(iter3 = mapStudent3.begin(); iter3!=mapStudent3.end(); iter3++)
	{
		cout<<iter3->first<<""<<iter3->second<<endl;		
	}

	// 数组插入在数据覆盖上的效果
	map<int, string> mapStudent4;
	
	mapStudent4[1] = "student_one";
	
	mapStudent4[1] = "student_two";
	
	mapStudent4[2] = "student_three";
	
	map<int, string>::iterator iter4;
	
	for(iter4 = mapStudent4.begin(); iter4 != mapStudent4.end(); iter4++){
		cout<<iter4->first<<""<<iter4->second<<endl;
	}


    // 输出map的长度
	int nSize = mapStudent.size();
	cout << nSize<<endl;

	//应用反相迭代器,下面举例说明,要体会效果,请自个动手运行程序
	map<int, string> mapStudent5;
	mapStudent5.insert(pair<int, string>(1, "student_one"));	
	mapStudent5.insert(pair<int, string>(2, "student_two"));	
	mapStudent5.insert(pair<int, string>(3, "student_three"));	
	map<int, string>::reverse_iterator iter5;
	for(iter5 = mapStudent5.rbegin(); iter5 != mapStudent5.rend(); iter5++){
		cout<<iter5->first<<""<<iter5->second<<endl;
	}

	// 
	map<int, string> mapStudent6;
	
	mapStudent6.insert(pair<int, string>(1, "student_one"));
	
	mapStudent6.insert(pair<int, string>(2, "student_two"));
	
	mapStudent6.insert(pair<int, string>(3, "student_three"));
	
	int nSize1 = mapStudent6.size();
	//此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++) 
	//by rainfish
	for(int nIndex = 0; nIndex < nSize1; nIndex++){
		cout<<mapStudent6[nIndex]<<endl;
	}
	
	/************************************************************************/
	/* 用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,
	它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器*/
	/************************************************************************/
	map<int, string> mapStudent7;	
	mapStudent7.insert(pair<int, string>(1, "student_one"));
	mapStudent7.insert(pair<int, string>(2, "student_two"));
	mapStudent7.insert(pair<int, string>(3, "student_three"));
	map<int, string>::iterator iter7;
	iter7 = mapStudent7.find(1);
	if(iter7 != mapStudent7.end()){
		cout<<"Find, the value is "<<iter7->second<<endl;
	}else{
		cout<<"Do not Find"<<endl;
	}

	// 判断map 是否为空
	if(mapStudent7.empty()){
		cout << "map is empty" <<endl;
	} else {
		cout <<"map is not empty" << endl;
	}
	// 清除map
	mapStudent7.clear();
	if(mapStudent7.empty()){
		cout << "map is empty" <<endl;
	} else {
		cout <<"map is not empty" << endl;
	}


	map<int, string> mapStudent8;	
	mapStudent8.insert(pair<int, string>(1, "student_one"));
	mapStudent8.insert(pair<int, string>(2, "student_two"));
	mapStudent8.insert(pair<int, string>(3, "student_three"));
	map<int, string>::iterator iter8;
	// 通过迭代器删除
	iter8 = mapStudent8.find(1);
	mapStudent8.erase(iter8);
	
	for(iter8 = mapStudent8.begin(); iter8 != mapStudent8.end(); iter8++){
		cout<<iter8->first<<""<<iter8->second<<endl;
	}

	//如果删除了会返回1,否则返回0
	int retValue = mapStudent8.erase(1);
	cout << retValue <<endl;
	retValue = mapStudent8.erase(2);
	cout << retValue <<endl;

	return 1;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值