zoukankan      html  css  js  c++  java
  • cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器

    cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器
    map(映射,key不能重复,一对一对的,value_type(1, "one")),multimap(多映射key可以重复)
    红黑树(数据结构)map,multimap就是红黑树-二叉树
    基本操作
    insert:4 种方法
    count和find
    erase:3种方法
    注意:不能通过find进行修改。

    a.insert(map<int, string>::value_type(1, "one")); 数值1就是key键,"one"就是值。就是一对

    map 与 multimap是存储key-value(键-值 对)类型的容器。

    不同之处在于:map只允许key与 value一一对应;multimap一个key可对应多个value;

    STL在线手册英文链接 :http://www.cplusplus.com/reference/stl/

    STL在线手册中文链接 :http://c.biancheng.net/stl/

    txwtech@163.com

      1 /*cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器
      2 map(映射,key不能重复,一对一对的,value_type(1, "one")),multimap(多映射key可以重复)
      3 红黑树(数据结构)map,multimap就是红黑树-二叉树
      4 基本操作
      5 insert:4 种方法
      6 count和find
      7 erase:3种方法
      8 注意:不能通过find进行修改。
      9 
     10 a.insert(map<int, string>::value_type(1, "one")); 数值1就是key键,"one"就是值。就是一对
     11 
     12 map 与 multimap是存储key-value(键-值 对)类型的容器。
     13 
     14 不同之处在于:map只允许key与 value一一对应;multimap一个key可对应多个value;
     15 
     16 STL在线手册英文链接 :http://www.cplusplus.com/reference/stl/
     17 
     18 STL在线手册中文链接 :http://c.biancheng.net/stl/
     19 */
     20 #include <iostream>
     21 #include <map>
     22 #include <string>
     23 
     24 using namespace std;
     25 
     26 int main()
     27 {
     28     map<int, string> a;
     29     map<string, int> score;
     30     multimap<int, string> ma;
     31     
     32     cout << "插入数据" << endl;
     33     a.insert(map<int, string>::value_type(1, "one"));//数值1就是键,"one"就是值。就是一对
     34     a.insert(map<int, string>::value_type(2, "two"));
     35     a.insert(map<int, string>::value_type(3, "Three"));
     36     a.insert(make_pair(-1, "Minus One"));//插入方法2
     37     a.insert(pair<int, string>(1000, "One Thousand"));//插入方法3
     38     a[1000000] = "One Million";//插入方法4,不能用于multimap
     39 
     40     score.insert(make_pair("scott", 100));
     41     score.insert(make_pair("sunny", 100));
     42     score.insert(make_pair("Gates", 88));
     43     score["bill"] = 99;
     44     cout << "bill score is:" << score["bill"] << endl;
     45     cout << "Gates score is: " << score["Gates"] << endl;
     46 
     47     cout << "map查找返回的是一个常迭代器" << endl;
     48     cout << a[3] << endl;//下标3对应three;
     49     cout << a[-1] << endl;//-1,对应--Minus One
     50 
     51     cout << "map里面里面一共有:" << a.size() << " 个键值对数据";
     52     cout << "这些数据是:" << endl;
     53     map<int, string>::const_iterator i;
     54     for (i = a.begin(); i != a.end(); ++i)
     55     {
     56         cout << "Key:" << i->first;
     57         cout << " Value:" << i->second.c_str();
     58         cout << endl;
     59     }
     60     ma.insert(multimap<int, string>::value_type(3, "Three"));
     61     ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
     62     ma.insert(make_pair(-1, "Minus one1"));
     63     ma.insert(pair<int, string>(1000, "One Thousand"));
     64     ma.insert(pair<int, string>(1000, "one Thousand"));
     65 
     66     cout << endl << "multimap" << ma.size() << " 个数据。" << endl;
     67     cout << "multimap数据显示:" << endl;
     68     multimap<int, string>::const_iterator im;
     69     for (im = ma.begin(); im != ma.end(); ++im)
     70     {
     71         cout << "Key: " << im->first;
     72         //cout << " Value:" << im->second;//如果出错就用c_str()
     73         cout << " value:" << im->second.c_str();
     74         cout << endl;
     75     }
     76     cout << "multimap有" << ma.count(1000) << " 个1000" << endl;
     77     
     78     cout << "multimap查找返回的是一个常迭代器" << endl;
     79     multimap<int, string>::const_iterator fi;
     80     
     81 
     82     fi=ma.find(45);
     83     if (fi != ma.end())
     84     {
     85         cout << "找到了:" << fi->first << "=" << fi->second.c_str() << endl;
     86 
     87     }
     88     else
     89     {
     90         cout << "没有找到" << endl;
     91     }
     92     fi = ma.find(1000); //多个1000都找出来显示
     93     if (fi != ma.end())
     94     {
     95         cout<<"找到了1000!!!"<<endl;
     96         size_t n = ma.count(1000);
     97         for (size_t i = 0; i < n; ++i)
     98         {
     99             cout << "	 Key: " << fi->first;
    100             cout << ", Value[" << i << "]=";
    101             cout << fi->second << endl;
    102             ++fi;
    103         }
    104     }
    105 
    106     cout << "erase删除方法的使用" << endl;
    107     if (ma.erase(-1) > 0)
    108         cout << "通过key删除成功,结果大于0:" << endl;
    109 
    110     cout << "通过查找,再删除" << endl;
    111 
    112     multimap<int, string>::iterator iElementFound = ma.find(45);
    113     if (iElementFound != ma.end())
    114     {
    115         ma.erase(iElementFound);
    116         cout << "删除45成功咯" << endl;
    117     }
    118     
    119     ma.erase(ma.lower_bound(1000), ma.upper_bound(1000));
    120     cout << "从第一个1000,到最后一个1000的数据都删除了" << endl;
    121 
    122 
    123 
    124 
    125     return 0;
    126 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    中断向量表
    内核进程的堆栈 [转]
    int指令理解
    Linux进程创建和结束
    Linux 信号signal处理机制
    wait和waitpid详解
    linux 如何清理僵尸进程
    API:System V & POSI
    shell
    Code POJ
  • 原文地址:https://www.cnblogs.com/txwtech/p/12325209.html
Copyright © 2011-2022 走看看