zoukankan      html  css  js  c++  java
  • STL map用法总结(multimap)

    2017-08-19 10:58:52 

    writer;pprp

    #include <map>
    #include <string>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    typedef pair<int, string> PAIR;
    
    ostream & operator<<(ostream & out, const PAIR& p)
    {
        out << p.first << " " << p.second << endl;
        return out;
    }
    
    //函数对象,对()进行了重载
    struct CmpByValue
    {
        bool operator()(const PAIR&l, const PAIR&r)
        {
            return l.second < r.second;
        }
    };
    
    int main()
    {
        //数据插入
        map<int, string> mapStudent;
    
        mapStudent.insert(map<int, string>::value_type (1, "student_one"));
        mapStudent.insert(map<int, string>::value_type (2, "student_two"));
        mapStudent.insert(map<int, string>::value_type (3, "student_three"));
        mapStudent.insert(map<int, string>::value_type (4,"student_four"));
        mapStudent.insert(pair<int, string>(6, "student_six"));
        mapStudent.insert(make_pair(7, "student_seven"));
        mapStudent[5] = "student_five";
    
        //数据遍历
        //正向遍历
        map<int, string>::iterator  iter;
        for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
        {
            cout<<iter->first<<" "<<iter->second<<endl;
        }
        //反向遍历
        map<int, string>::reverse_iterator it;
        for(it = mapStudent.rbegin(); it != mapStudent.rend() ; it++)
        {
            cout << it->first <<" " << it->second << endl;
        }
        //数组形式的遍历
        int nSize = mapStudent.size();
        for(int i = 0 ; i < nSize ; i++)
        {
            cout << mapStudent[i] << endl;
        }
        cout << endl;
    
        //数据的查找
        map<int, string>::iterator iter1;
    
        iter1 = mapStudent.find(1);
    
        if(iter1 != mapStudent.end())
        {
            cout << iter1->second << endl;
        }
        else
        {
            cout << "can't find it " << endl;
        }
    
        //判断是否存在这个key
        int judge = mapStudent.count(1);
    
        if(judge)
        {
            cout << "exist" << endl;
        }
        else
        {
            cout << "not exist"  << endl;
        }
    
        cout << endl;
    
        //用lower_bound进行查找
        map<int, string> :: iterator it2;
        it2 = mapStudent.lower_bound(2);
        cout << it2->second << endl;
        it2 = mapStudent.upper_bound(2);
        cout << it2->second << endl;
    
        pair<map<int, string>::iterator, map<int, string>::iterator> mapair;
        mapair = mapStudent.equal_range(1);
        //如果相等的话那就说明没有找到,如果不等就说明找到了
        if(mapair.first == mapair.second)
            cout << "can't find it" << endl;
        else
            cout << "the value of it is " << mapair.first->second << endl;
    
        //数据的清空与判空
    //    mapStudent.clear();
        if(!mapStudent.empty())
            cout << "not empty" << endl;
        else
            cout << "empty" << endl;
    
        //数据的删除
        map<int, string> :: iterator it3;
        it3 = mapStudent.find(1);
        mapStudent.erase(it3);
    
        for(it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++)
            cout << it3->second << endl;
    
        judge = mapStudent.erase(2);
        if(judge)
        {
            cout << "delete successfully" << endl;
            for(it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++)
            {
                cout << it3->second << endl;
            }
        }
    
        //排序(按照key的大小排序)
        //map<int, string, less<int> >是默认的升序
        //map<int, string, greater<int> >是可以构造的降序
    
        //排序(按照value的大小排序)
        vector <PAIR> sortByValue(mapStudent.begin(), mapStudent.end());
    
        sort(sortByValue.begin(), sortByValue.end(),CmpByValue());
    
        for(int i = 0 ; i < mapStudent.size(); i++)
        {
            cout << sortByValue[i]<< endl;
        }
        return 0;
    }

     另外multimap用法与map类似,函数什么的都一样,只是支持一个key对多个value

    /*
    name : usage of List
    writer : pprp
    declare : null
    date ; 2017/8/20
    */
    #include <bits/stdc++.h>
    
    using namespace std;
    
    void print(multimap<string,double>&k)
    {
          multimap<string, double>::iterator it;
          for(it = k.begin(); it != k.end() ; it++)
          {
                cout << it->first << " " << it->second << endl;
          }
    }
    
    int main()
    {
        multimap<string, double>mp;
        mp.insert(pair<string,double>("jack",300.23));
        mp.insert(make_pair("green",234.1));
        mp.insert(make_pair("red",234.132));
        mp.insert(make_pair("yellow",2342.1));
        mp.insert(make_pair("blue",234.11));
        mp.insert(make_pair("orange",2324.1));
    
        multimap<string, double>::iterator it;
        
        print(mp);
        
        mp.erase("jack");
        
        print(mp);
        
        it = mp.find("orange");
        if(it != mp.end())
        {
              cout << it->first << " " << it->second << endl;
        }
    
        return 0;
    }
  • 相关阅读:
    Update SSM agent to each EC2 via Bat and bash script
    Shell脚本循环读取文件中的每一行
    通过psexec实现远程安装软件包
    Getting Private/Public IP address of EC2 instance using AWS-cli [closed]
    Python to list users in AWS
    BAT script to set local account password never expire
    Difference between Netbios and Host name
    PowerShell官方文档
    powershell for rename server name
    Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider
  • 原文地址:https://www.cnblogs.com/pprp/p/7395890.html
Copyright © 2011-2022 走看看