C++中map的用法

来源:互联网 发布:淘宝开店如何收费标准 编辑:程序博客网 时间:2024/06/05 16:12

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。

这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

下面的代码,包括map容器的一些常用方法,包括插入数据、查询数据、遍历数据、删除数据、map容器长度等。代码如下:

#include <iostream>#include <map>#include <string>#include <algorithm>using namespace std;int main(){  map<int,string> mapStudent;//map 插入数据有三种方式//map 的第一种插入数据方式  插入pair数据mapStudent.insert(pair<int,string>(10,"student_ten"));mapStudent.insert(pair<int,string>(1,"student_one"));mapStudent.insert(pair<int,string>(1,"student_1"));mapStudent.insert(pair<int,string>(2,"student_two"));mapStudent.insert(pair<int,string>(3,"student_three"));//map 第二种插入数据方式 插入value_type数据mapStudent.insert(map<int,string>::value_type (4,"student_four"));mapStudent.insert(map<int,string>::value_type (4,"student_4"));mapStudent.insert(map<int,string>::value_type (5,"student_5"));mapStudent.insert(map<int,string>::value_type (6,"student_6"));//map 第三种插入数据方式 用数组方式插入数据 mapStudent[7] = "student_seven";mapStudent[7] = "student_7";mapStudent[8] = "student_8";mapStudent[9] = "student_9";//查看map里的数据长度,用size函数cout<< "isize = "<< mapStudent.size() <<endl;//ps:用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入不了数据的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值。//map 遍历也有三种方式//遍历第一种方式 应用反相迭代器   map<int,string>::reverse_iterator iterr;for(iterr = mapStudent.rbegin(); iterr != mapStudent.rend(); iterr++){cout<<"遍历第1种"<<iterr->first<<" "<<iterr->second<<endl;}//遍历第二种方式 应用前向迭代器  常用的遍历方式map<int,string>::iterator iter;for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){cout<<"遍历第2种"<<iter->first<<" "<<iter->second<<endl;}//遍历第二种方式 数组方式  //也就是下标操作,只能特定情况使用。for(int nIndex = 1; nIndex <= mapStudent.size(); nIndex++){cout<<"遍历第3种"<<mapStudent[nIndex]<<endl;}//查看map里的数据长度,用size函数cout<< "isize1 = "<< mapStudent.size() <<endl;//map 查找的方式常用的有两种,find()用的比较多//第一种查找方式 count()函数 查找关键字为9的个数  只能查找关键字int icount = mapStudent.count(9);int icount1 = mapStudent.count(11);cout << "cout = "<< icount <<"; cout1 = "<< icount1<< endl;//第二种查找方式  find()函数iter = mapStudent.find(5);if (iter != mapStudent.end()){cout<<"Find,the value key: " <<iter->first<<", value: "<<iter->second<<endl;} else{cout<<"Do not Find"<<endl;}//清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空mapif (mapStudent.empty() != 0){cout<< "mapStudent  not have data!" <<endl;}else{cout<< "mapStudent  have data!" <<endl; }//删除元素有两种方式//第一种 按关键字删除mapStudent.erase(5);//查看map里的数据长度,用size函数cout<< "isize2 = "<< mapStudent.size() <<endl;//用迭代器,成片的删除iter = mapStudent.find(1);mapStudent.erase(iter);//查看map里的数据长度,用size函数cout<< "isize3 = "<< mapStudent.size() <<endl;//用迭代器,成片的删除//一下代码把整个map清空mapStudent.erase(mapStudent.begin(),mapStudent.end());//查看map里的数据长度,用size函数cout<< "isize4 = "<< mapStudent.size() <<endl;return 0;}  

各cout的输出结果如下:

isize = 10

遍历第1种10 student_ten

遍历第1种9 student_9

遍历第1种8 student_8

遍历第1种7 student_7

遍历第1种6 student_6

遍历第1种5 student_5

遍历第1种4 student_four

遍历第1种3 student_three

遍历第1种2 student_two

遍历第1种1 student_one

遍历第2种1 student_one

遍历第2种2 student_two

遍历第2种3 student_three

遍历第2种4 student_four

遍历第2种5 student_5

遍历第2种6 student_6

遍历第2种7 student_7

遍历第2种8 student_8

遍历第2种9 student_9

遍历第2种10 student_ten

遍历第3种student_one

遍历第3种student_two

遍历第3种student_three

遍历第3种student_four

遍历第3种student_5

遍历第3种student_6

遍历第3种student_7

遍历第3种student_8

遍历第3种student_9

遍历第3种student_ten

isize1 = 10

cout = 1; cout1 = 0

Find,the value key: 5, value: student_5

mapStudent  have data!

isize2 = 9

isize3 = 8

isize4 = 0



1 0