zoukankan      html  css  js  c++  java
  • map_multimap

    #include<iostream>
    #include<string>
    #include<map>
    using namespace std;
    
    struct SelfType{
        int number;
    };
    
    void map_find()
    {
        //map.lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器 
        //map.upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器
        //map.equal_range(keyElem);//返回一个pair,
        //pair.first为第一个key>=keyElem元素的迭代器
        //pair.second为第一个key>keyElem元素的迭代器
    
        map<int,string> mapStu;
        //1、通过pair的方式插入对象
        //mapStu.insert( pair<int,string>(3, "小张"));
        //2、通过value_type的方式插入对象
        mapStu.insert( map<int, string>::value_type(1, "小李") );
        //3、通过数组的方式插入值
        mapStu[2] = "小张";
        mapStu[3] = "小刘";
        mapStu[4] = "小赵";
        mapStu[5] = "小王";
    
        map<int,string>::iterator it_lower,it_upper;
        it_lower=mapStu.lower_bound(3);
        it_upper=mapStu.upper_bound(3);
        cout<<it_lower->first<<","<<it_lower->second<<endl;
        cout<<it_upper->first<<","<<it_upper->second<<endl;
        pair<map<int,string>::iterator,map<int,string>::iterator> mapStuPair=mapStu.equal_range(5);
        it_lower=mapStuPair.first;
        it_upper=mapStuPair.second;
        cout<<it_lower->first<<","<<it_lower->second<<endl;
        if(it_upper !=mapStu.end())
        {
            cout<<it_upper->first<<","<<it_upper->second<<endl;
        }
        else
        {
            cout<<"it_upper ==mapStu.end()"<<endl;
        }
    /*
    3,小刘
    4,小赵
    5,小王
    it_upper ==mapStu.end()
    请按任意键继续. . .
    */
    }
    
    void map_greater()
    {
        map<int,string,greater<int>> mapStu;
        //1、通过pair的方式插入对象
        mapStu.insert( pair<int,string>(3, "小张"));
        //2、通过value_type的方式插入对象
        mapStu.insert( map<int, string>::value_type(1, "小李") );
        //3、通过数组的方式插入值
        mapStu[3] = "小刘";
        mapStu[5] = "小王";
    
        map<int,string,greater<int>>::iterator it;
        for(it=mapStu.begin();it !=mapStu.end();++it)
        {
            cout<<it->first<<","<<it->second<<" ";
        }
        cout<<endl;
    
    }
    void main()
    {
        map_find();return;
        map_greater();return;
    
        map<int, char> mapA;
        map<string, float> mapB;
        //其中T1,T2还可以用各种指针类型或自定义类型
        map<float, char> mapC;
        map<int*, float> mapD;
        map<SelfType, char> mapE;
    
        map<int,string> mapStu;
        //1、通过pair的方式插入对象
        mapStu.insert( pair<int,string>(3, "小张"));
        //2、通过value_type的方式插入对象
        mapStu.insert( map<int, string>::value_type(1, "小李") );
        //3、通过数组的方式插入值
        mapStu[3] = "小刘";
        mapStu[5] = "小王";
    
        map<int,string>::iterator it;
        for(it=mapStu.begin();it !=mapStu.end();++it)
        {
            cout<<it->first<<","<<it->second<<" ";
        }
        cout<<endl;
    
        string strName = mapStu[2];//取操作
        cout<<strName.length()<<endl;
       //只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。
        for(it=mapStu.begin();it !=mapStu.end();++it)
        {
            cout<<it->first<<","<<it->second<<" ";
        }
        cout<<endl;
    
    }
    #include <iostream>  
    #include <string>  
    #include <map>  
    using namespace std;  
        
    int main()  
    {  
        multimap<string,int> m_map;  
        string s("中国"),s1("美国");  
        m_map.insert(make_pair(s,50));  
        m_map.insert(make_pair(s,55));  
        m_map.insert(make_pair(s,60));  
        m_map.insert(make_pair(s1,30));  
        m_map.insert(make_pair(s1,20));  
        m_map.insert(make_pair(s1,10));  
        //方式1  
        int k;  
        multimap<string,int>::iterator m;  
        m = m_map.find(s);  
        for(k = 0;k != m_map.count(s);k++,m++)  
            cout<<m->first<<"--"<<m->second<<endl;  
        //方式2  
        multimap<string,int>::iterator beg,end;  
        beg = m_map.lower_bound(s1);  
        end = m_map.upper_bound(s1);  
        for(m = beg;m != end;m++)  
            cout<<m->first<<"--"<<m->second<<endl;  
        //方式3  
        beg = m_map.equal_range(s).first;  
        end = m_map.equal_range(s).second;  
        for(m = beg;m != end;m++)  
            cout<<m->first<<"--"<<m->second<<endl;  
        return 0;  
    } 
    
    /*
    中国--50
    中国--55
    中国--60
    美国--30
    美国--20
    美国--10
    中国--50
    中国--55
    中国--60
    请按任意键继续. . .
    */
  • 相关阅读:
    模板元编程实现素数判定
    JDBC开发
    4.9 当相应行存在时更新
    QT5中如何使用QFtp类(这个类虽然没有被收录,但一直在更新)
    gcc和g++的区别
    Awesome C/C++(图像部分)
    Ubuntu更新源
    GO的GDB调试
    内核探测工具systemtap简介
    列举一下项目中使用的产品和技术
  • 原文地址:https://www.cnblogs.com/sky20080101/p/6435991.html
Copyright © 2011-2022 走看看