zoukankan      html  css  js  c++  java
  • C++ STL map 随手笔记

    template<

        class Key,     class T,     class Compare = std::less<Key>,     class Allocator = std::allocator<std::pair<const Key, T> >

    > class map;
     
    关联容器,提供一对一的数据处理能力,map内部自建一课红黑树(一种非严格意义上的平衡二叉树),具有自动排序的功能,因此map内部所有的数据都是有序的。
    快速查找的复杂度是log(n)。
     
    swap 不是同一个map的元素的swap,而是两个map的swap
    find
    erase
    count
     
    map中的元素是自动按Key升序排序,所以不能对map用sort函数;
     
     这里要讲的是一点比较高深的用法了,排序问题,STL中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是int 型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去。下面给出两个方法解决这个问题。
    第一种:小于号重载,程序举例。
    #include <iostream>  
    • #include <string>  
    • #include <map>  
    • using namespace std;  
    •   
    • typedef struct tagStudentinfo  
    •   
    • {  
    •   
    •        int      niD;  
    •   
    •        string   strName;  
    •   
    •        bool operator < (tagStudentinfo const& _A) const  
    •   
    •        {     //这个函数指定排序策略,按niD排序,如果niD相等的话,按strName排序  
    •   
    •             if(niD < _A.niD) return true;  
    •   
    •             if(niD == _A.niD)  
    •   
    •                 return strName.compare(_A.strName) < 0;  
    •   
    •         return false;  
    •   
    •        }  
    •   
    • }Studentinfo, *PStudentinfo; //学生信息  
    •   
    • int main()  
    •   
    • {  
    •   
    •     int nSize;   //用学生信息映射分数  
    •   
    •     map<Studentinfo, int>mapStudent;  
    •   
    •     map<Studentinfo, int>::iterator iter;  
    •   
    •     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));  
    •   
    •     for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
    •   
    •         cout<<iter->first.niD<<' '<<iter->first.strName<<' '<<iter->second<<endl;  
    •   
    •     return 0;  
    • }  

    第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明。

    //第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明  

    •   
    • #include <iostream>  
    •   
    • #include <map>  
    •   
    • #include <string>  
    •   
    • using namespace std;  
    •   
    • 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;  
    •   
    •     }  
    • };  
    •   
    • int main()  
    •   
    • {   //用学生信息映射分数  
    •   
    •     map<Studentinfo, int, sort>mapStudent;  
    •   
    •     map<Studentinfo, int>::iterator iter;  
    •   
    •     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));  
    •   
    •     for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
    •   
    •         cout<<iter->first.niD<<' '<<iter->first.strName<<' '<<iter->second<<endl;  
    • }  
  • 相关阅读:
    1203 有穷自动机
    [转]HTTP协议详解
    JavaScript中的正则表达式
    [译]JavaScript insertAdjacentHTML
    [译]Autoprefixer:用最可行的方式处理浏览器前缀的CSS后处理器
    [译]JavaScript 错误和处理
    [译]CSS content
    [译]当你在浏览器输入url后发生了什么
    display的小故事
    移动web屏幕适配方案
  • 原文地址:https://www.cnblogs.com/hipposinsilt/p/6770617.html
Copyright © 2011-2022 走看看