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
请按任意键继续. . .
*/

 

posted @ 2017-02-23 22:58  sky20080101  阅读(164)  评论(0编辑  收藏  举报