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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/dopamy_busymonkey/article/details/47342059