• map容器


    一.摘要

    简介:

    • map中所有元素都是pair
    • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
    • 所有元素都会根据元素的键值自动排序(按key排序)

    本质:

    • map/multimap属于关联式容器,底层结构是用二叉树实现。

    优点:

    • 可以根据key值快速找到value值

    map和multimap区别

    • map不允许容器中有重复key值元素
    • multimap允许容器中有重复key值元素

    二.构造和赋值

    函数原型:

    map<T1, T2> mp; //map默认构造函数:
    map(const map &mp); //拷贝构造函数

    示例代码:

     1 /*map容器的构造函数*/
     2 #include<iostream>
     3 #include<ctime>
     4 #include<map>
     5 using namespace std;
     6 void printMap(const map<int, int>&m) {
     7     for (map<int, int>::const_iterator it = m.begin(); it!=m.end(); it++) {
     8         cout << "key=" << (*it).first << "	value=" << (*it).second << endl;
     9     }
    10 }
    11 int main() {
    12     srand((unsigned)time(NULL));
    13     map<int, int>m1;    //map<T1, T2> mp; //map默认构造函数:
    14     for (int i = 0; i < 5; i++) {
    15         m1.insert(pair<int,int>(i, rand()));
    16     }
    17     cout << "m1:" << endl;
    18     printMap(m1);
    19     map<int, int>m2(m1);    //map(const map &mp); //拷贝构造函数
    20     cout << "m2:" << endl;
    21     printMap(m2);
    22     map<int, int>m3;
    23     m3 = m1;
    24     cout << "m3:" << endl;
    25     printMap(m3);
    26     system("pause");
    27     return 0;
    28 }
    map容器的构造和赋值

    运行结果:

    三.大小和交换

    函数原型:

    size(); //返回容器中元素的数目
    empty(); //判断容器是否为空
    swap(st); //交换两个集合容器

    示例代码:

     1 /*map容器的大小和交换*/
     2 #include<iostream>
     3 #include<ctime>
     4 #include<map>
     5 using namespace std;
     6 void printMap(const map<int, int>&m) {
     7     for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
     8         cout << "key=" << (*it).first << "	value=" << (*it).second << endl;
     9     }
    10 }
    11 int main() {
    12     srand((unsigned)time(NULL));
    13     map<int, int>m1;
    14     for (int i = 0; i < 10; i++) {
    15         m1.insert(pair<int, int>(i, rand()));
    16     }
    17     if (m1.empty()) {    //empty(); //判断容器是否为空
    18         cout << "m1的大小为空!";
    19     }
    20     else {
    21         cout << "m1的大小为:" << m1.size() << endl;    //size(); //返回容器中元素的数目
    22     }
    23     printMap(m1);
    24     map<int, int>m2;
    25     m2.insert(make_pair(8, 8));
    26     cout << "m2:" << endl;
    27     printMap(m2);
    28     m1.swap(m2);    //swap(st); //交换两个集合容器
    29     cout << "交换后:" << endl;
    30     cout << "m1:" << endl;
    31     printMap(m1);
    32     cout << "m2:" << endl;
    33     printMap(m2);
    34     system("pause");
    35     return 0;
    36 }
    map容器的大小和交换

    运行结果:

     

    四.插入和删除

    函数原型:

    insert(elem); //在容器中插入元素。
    operator[]; //应该是返回value值
    clear(); //清除所有元素
    erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
    erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    erase(key); //删除容器中值为key的元素。

    示例代码:

     1 /*map容器的插入和删除*/
     2 #include<iostream>
     3 #include<ctime>
     4 #include<map>
     5 using namespace std;
     6 void printMap(const map<int, int>&m) {
     7     for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
     8         cout << "key=" << (*it).first << "	value=" << (*it).second << endl;
     9     }
    10 }
    11 int main() {
    12     srand((unsigned)time(NULL));
    13     map<int, int>m1;
    14     for (int i = 0; i < 5; i++) {
    15         //key:0 2 4 6 8
    16         m1.insert(pair<int, int>(i*2, rand()));        //insert(elem); //在容器中插入元素。
    17     }
    18     printMap(m1);
    19     cout << "在第3个位置插入元素“8”:" << endl;
    20     m1[3] = 8;    //operator[];  //应该是返回value值
    21     printMap(m1);
    22     cout << "删除第1个元素:" << endl;
    23     m1.erase(m1.begin());    //erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
    24     printMap(m1);
    25     cout << "删除第1到第2位置的元素:" << endl;
    26     m1.erase(m1.begin(), ++(++m1.begin()));    //erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
    27     printMap(m1);
    28     cout << "删除容器中key为6的元素:" << endl;    
    29     m1.erase(6);    //erase(key); //删除容器中值为key的元素。
    30     printMap(m1);
    31     cout << "清楚容器中的所有元素:" << endl;
    32     m1.clear();    //clear(); //清除所有元素
    33     printMap(m1);
    34     system("pause");
    35     return 0;
    36 }
    map容器的插入和删除

    运行结果:

    五.查找和统计

    函数原型:

    find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
    count(key); //统计key的元素个数

    示例代码:

     1 /*map容器的查找和统计*/
     2 #include<iostream>
     3 #include<map>
     4 using namespace std;
     5 void printMap(const map<int, int>&m) {
     6     for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
     7         cout << "key=" << (*it).first << "	value=" << (*it).second << endl;
     8     }
     9 }
    10 int main() {
    11     map<int, int>m1;
    12     m1.insert(pair<int, int>(1, 11));
    13     m1.insert(pair<int, int>(76, 33));
    14     m1.insert(pair<int, int>(45, 77));
    15     m1.insert(pair<int, int>(76, 66));
    16     m1.insert(pair<int, int>(3, 44));
    17     printMap(m1);
    18     if (m1.find(45) != m1.end()) {    //find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
    19         cout << "容器中存在key为“66”:" << endl;
    20     }
    21     else {
    22         cout << "容器中不存在key为“66”:" << endl;
    23     }
    24     if (m1.find(88) != m1.end()) {
    25         cout << "容器中存在key为“88”:" << endl;
    26     }
    27     else {
    28         cout << "容器中不存在key为“88”:" << endl;
    29     }
    30     int num = m1.count(76);    //count(key); //统计key的元素个数
    31     cout << "容器中key为“76”的元素共有 " << num << " 个(map容器中不能插入相同key值的元素)" << endl;    
    32     num = m1.count(88);
    33     cout << "容器中key为“88”的元素共有 " << num << "" << endl;
    34     system("pause");
    35     return 0;
    36 }
    map容器的查找和统计

    运行结果:

    六.排序

    方法:

    利用防函数控制排序规则

    示例代码:

     1 /*map容器排序*/
     2 #include<iostream>
     3 #include<ctime>
     4 #include<map>
     5 using namespace std;
     6 class MyCompare {
     7 public:
     8     bool operator()(int x, int y) {//重载小括号
     9         return x > y;    //降序排序
    10     }
    11 };
    12 void printMap(const map<int, int, MyCompare>&m) {
    13     for (map<int, int, MyCompare>::const_iterator it = m.begin(); it != m.end(); it++) {
    14         cout << "key=" << (*it).first << "	value=" << (*it).second << endl;
    15     }
    16 }
    17 int main() {
    18     srand((unsigned)time(NULL));
    19     map<int, int, MyCompare>m1;
    20     for (int i = 0; i < 5; i++) {
    21         m1.insert(pair<int, int>(i, rand()));    //key:0 2 4 6 8
    22     }
    23     cout << "按key值降序排列" << endl;
    24     printMap(m1);
    25     system("pause");
    26     return 0;
    27 }
    map容器排序

    运行结果:

     七.总结

      听说map容器很高效,和vector、list等容器一样用得很多,但目前我是不知道它的具体应用是什么

  • 相关阅读:
    看《环太平洋》归来
    在Fedora8上安装MySQL5.0.45的过程
    在Win7上安装MySql5.2遇到Write configuration file的解决
    每一个问题都是一把锁
    Fedora8上Apache Httpd与Tomcat6初集成
    在Fedora8上的Tomcat上deploy一个war
    在Fedora8上配置Tomcat6.0.37
    在Fedora8上配置Apache Httpd
    在Fedora8上安装jdk-7u25-linux-i586.rpm的步骤
    Java继承中的几道面试题
  • 原文地址:https://www.cnblogs.com/chasemeng/p/12929532.html
Copyright © 2020-2023  润新知