map使用

//                        map使用
1
#include <iostream> 2 #include "insertVal.h" 3 #include "sort.h" 4 using namespace std; 5 6 7 void main() 8 { 9 ////////////三种插入方式//////////// 10 InsertVal insert; 11 insert.insert_1(); 12 cout<<"-------------------"<<endl; 13 insert.insert_2(); 14 cout<<"-------------------"<<endl; 15 insert.insert_3(); 16 cout<<"-------------------"<<endl; 17 /*以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的, 18 当然了第一种和第二种在效果上是完全一样的,用insert函数插入数据, 19 在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时, 20 insert操作是插入不了数据的,但是用数组方式就不同了,它可以覆盖以 21 前该关键字对应的值。*/ 22 23 //////////map的大小////////// 24 map<int, int> iMap; 25 iMap[1] = 11; 26 iMap[2] = 22; 27 iMap[3] = 33; 28 cout<<iMap.size()<<endl; 29 30 //////////map的遍历////////// 31 //1 32 map<int, int>::reverse_iterator riter; 33 for(riter = iMap.rbegin(); riter != iMap.rend(); ++riter)//这里不要用后加,因为iterator是类模板,后加要返回一个无用的临时对象,
                                        //而it++是函数重载,所以编译器无法对其进行优化,所以每遍历一个元素,你就创建并销毁了一个无用的临时对象。
34 { 35 cout<<riter->first<<" "<<riter->second<<endl; 36 } 37 //2 38 map<int, int>::iterator iter; 39 for(iter = iMap.begin(); iter != iMap.end(); ++iter) 40 { 41 cout<<iter->first<<" "<<iter->second<<endl; 42 } 43 //3 44 map<string, int> sMap; 45 sMap["age"] = 12; 46 sMap["height"] = 176; 47 sMap["weight"] = 65; 48 string str[] = {"age", "height", "weight"}; 49 for (int i=0; i<sMap.size(); i++) 50 { 51 auto val = sMap.at(str[i]); 52 cout<<str[i]<<":"<<val<<endl; 53 } 54 55 //count函数来判定关键字是否出现,结果返回1/0 56 cout<<sMap.count("age")<<endl; 57 58 //用find函数来定位数据出现位置,它返回的一个迭代器, 59 //当数据出现时,它返回数据所在位置的迭代器,如果map 60 //中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器 61 auto iters = sMap.find("age"); 62 if(iters != sMap.end()) 63 cout<<iters->first<<":"<<iters->second<<endl; 64 else 65 cout<<"no find!"<<endl; 66 67 //Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器) 68 //Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器) 69 //例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2, 70 //而upper-bound(2)的话,返回的就是3 71 //Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器, 72 //pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话, 73 //则说明map中不出现这个关键字, 74 auto iter_1 = sMap.lower_bound("b"); 75 auto iter_2 = sMap.upper_bound("b"); 76 auto iter_pair = sMap.equal_range("ab"); 77 78 /////////erase 79 sMap.erase("age"); 80 sMap.erase(sMap.find("height")); 81 sMap.erase(sMap.begin(), --sMap.end());//删除一个区域是前闭后开的区间 82 83 84 ///////// 85 if(sMap.empty()) 86 sMap.clear(); 87 88 89 sortShow(); 90 return; 91 }

其中insertVal.h:

 1 #ifndef INSERTVAL_H
 2 #define INSERTVAL_H
 3 
 4 #include <iostream>
 5 #include <map>
 6 #include <string>
 7 using namespace std;
 8 
 9 class InsertVal
10 {
11 public:
12     void insert_1();
13     void insert_2();
14     void insert_3();
15 };
16 
17 void InsertVal::insert_1()
18 {
19     map<int, string> mapStudent;
20     mapStudent.insert(pair<int, string>(1, "Mical"));
21     mapStudent.insert(pair<int, string>(2, "Meria"));
22     mapStudent.insert(pair<int, string>(3, "Sam"));
23     map<int, string>::iterator  iter;
24     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
25     {
26         cout<<iter->first<<"号学生是:"<<iter->second<<endl;
27     }
28 }
29 
30 void InsertVal::insert_2()
31 {
32     map<int, string> mapStudent;
33     mapStudent.insert(map<int, string>::value_type (1, "Mical"));
34     mapStudent.insert(map<int, string>::value_type (2, "Meria"));
35     mapStudent.insert(map<int, string>::value_type (3, "Sam"));
36     map<int, string>::iterator  iter;
37     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
38     {
39         cout<<iter->first<<"号学生是:"<<iter->second<<endl;
40     }
41 }
42 
43 void InsertVal::insert_3()
44 {
45     map<int, string> mapStudent;
46     mapStudent[1] = "Mical";
47     mapStudent[2] = "Meria";
48     mapStudent[3] = "Sam";
49     map<int, string>::iterator  iter;
50     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
51     {
52         cout<<iter->first<<"号学生是:"<<iter->second<<endl;
53     }
54 }
55 
56 #endif

sort.h:

#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <map>
#include <string>
using namespace std;
/*
    map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的
    STL中默认是采用小于号来排序,
    在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,
    因为它没有小于号操作,insert等函数在编译的时候过不去
    方法一:重载小于号。略
    方法二:仿函数的应用,这个时候结构体中没有直接的小于号重载。如下
*/


typedef struct tagStudentInfo
{
    int      nID;
    string   strName;
}StudentInfo, *PStudentInfo;  //学生信息

class sort
{
public:
    bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
    {
        if(_A.nID < _B.nID) return true;
        if(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
        return false;
    }
};

void sortShow()
{
    //用学生信息映射分数
    map<StudentInfo, int, sort>mapStudent;
    StudentInfo studentInfo;
    studentInfo.nID = 1;
    studentInfo.strName = "student_one";
    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
    studentInfo.nID = 2;
    studentInfo.strName = "student_two";
    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
}

#endif
原文地址:https://www.cnblogs.com/wrbxdj/p/5379481.html