map容器总结(C/C++)

1.set容器+(multiset)介绍

1.1 介绍

 map容器是一个关联容器,用于存储 键值(key)和映射值(value) 的一个关系对,即内部用于存储形似 pair<key, value>这样子的一个关系对。
 map容器内部的元素通常使用键值对元素进行唯一的表示并排序,内部按照严格的弱排序进行排序(即从小到大进行排序),map容器相对于unorder_map容器使用键值直接进行访问的速度会较慢,但是它支持基于内部元素的顺序使用迭代器进行直接迭代。
 其内部当中的映射值允许使用括号操作符直接对其值进行访问,映射通常实现为二叉搜索树。

1.2 简单解析

 A. 头文件: map
 B. 关键区分
 映射容器map : 存储唯一键值(键是唯一的,重复相同键元素只保持最后一次赋值操作) + 内部集合有序(内部元素对象按照键进行排序)
 映射容器multimap : 存储重复键值(多个元素可以允许具有相同的键) + 内部集合有序(内部元素对象按照键进行排序,值一样不会干预其存储顺序)
 两者核心区别点就只在于multimap可以重复存储多个相同键的元素,而对于map来说其内部容器当中的元素的键都是唯一的标识不可以重复。
 C. 常用情景: 常用于存储元素之间联系,存储元素索引和元素本身的联系或者一些二维坐标信息等。

2.初始化构造操作(Constructor)

 通常使用的就是直接定义或者用同类型迭代器初始化

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;

int main(){
    
    
    map<int, int> mp;
    multimap<int, int> mmp;

    mp[1] = 1;
    mp[2] = 2;

    map<int, int> mp2(mp.begin(), mp.end()); // 同类型迭代器初始化
    map<int, int> mp3(mp);

    for(auto it : mp2){
    
    
        cout << it.first << ' ' << it.second << endl; // 1 1, 2 2
    }

    map<int, int> :: iterator it = mp3.begin();
    for(it; it != mp3.end(); it ++ ){
    
    
        // cout << (*it).first << ' ' << (*it).second << endl;
        cout << it -> first << ' ' << it -> second << endl; // 1 1, 2 2
    }
}

3.迭代器操作(Iterator)

在这里插入图片描述
 map.begin() —— 返回容器最开始位置的迭代器
 map.end() —— 返回容器末尾位置下一个位置的迭代器

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;

int main(){
    
    
    map<int, int> mp;
    mp[2] = 1;
    mp[1] = 1;
    mp[3] = 1;
    
    for(map<int, int> :: iterator it = mp.begin(); it != mp.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it -> second << endl;
        // 1 - 1, 2 - 1, 3 - 1
    }
    
}

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;

int main(){
    
    
    multimap<int, int> mmp;
    // mmp 的键不唯一,不能作为唯一标识进行赋值 mmp[1] = 1;
    // 只按照键进行排序
    mmp.insert(make_pair(2, 4));
    mmp.insert(make_pair(2, 3));
    mmp.insert(make_pair(1, 1));
    mmp.insert(make_pair(1, 2));

    for(multimap<int, int> :: iterator it = mmp.begin(); it != mmp.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it -> second << endl;
        // 1 - 1, 1 - 2, 2 - 4, 2 - 3
    }

}

4.容量操作(Capacity)

在这里插入图片描述
 map.empty() —— 判断容器是否为空
 map.size() —— 返回容器当前大小
 map.max_size() —— 返回容器最大存储容量

#include<iostream>
#include<algorithm>
#include<map>
using namespace std;

int main(){
    
    
    map<int, int> mp;
    cout << mp.empty() << endl; // 1

    for(int i = 0; i < 10; i ++ ) mp[i] = i;

    cout << mp.size() << endl; // 10
    cout << mp.max_size() << endl; // 461168601842738790
    cout << mp.empty() << endl; // 0

}

5.修改操作(Modifiers)

在这里插入图片描述
 map.insert() —— 向容器当中插入元素
在这里插入图片描述
 a. 直接插入元素进入容器,成功插入返回元素迭代器和true, 否则返回已经存在的元素迭代器和false. 而对于multimap因为其允许键值不唯一所以可以直接插入,没有bool值返回

#include<iostream>
#include<map>
using namespace std;

int main ()
{
    
    
  map<char, int> mp;
  mp['a'] = 100;
  mp['b'] = 200;

  pair<map<char, int>::iterator, bool> ret;
  ret = mp.insert(pair<char, int>('a', 200));
  if(ret.second == false){
    
    
    cout << ret.first -> first << ' '<< ret.first -> second << endl;
  } // a - 100

  return 0;
}

 map.erase() —— 容器当中删除元素
在这里插入图片描述

 a. 按照迭代器的位置删除容器内部的元素

#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    map<char, int> mp;
    map<char, int> :: iterator it;
    mp['a'] = 100;
    mp['b'] = 200;

    mp.erase(mp.begin());
    for(it = mp.begin(); it != mp.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it ->second << endl; // b - 200
    }
}

 b. 按照键的大小来删除容器内的元素,其中multimap可能会删除多个重复键大小的元素

#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    map<char, int> mp;
    map<char, int> :: iterator it;
    mp['a'] = 100;
    mp['b'] = 200;

    mp.erase('a');
    for(it = mp.begin(); it != mp.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it ->second << endl; // b - 200
    }
}
#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    multimap<char, int> mmp;
    multimap<char, int> :: iterator it;
    mmp.insert(make_pair('a', 100));
    mmp.insert(make_pair('a', 100));
    mmp.insert(make_pair('b', 200));
    mmp.insert(make_pair('b', 200));

    mmp.erase('a'); // 删除所有键为'a'的元素
    for(it = mmp.begin(); it != mmp.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it ->second << endl; // b - 200, b - 200
    }

}

 c. 按照迭代器的范围删除元素

#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    multimap<char, int> mmp;
    multimap<char, int> :: iterator it;
    mmp.insert(make_pair('a', 100));
    mmp.insert(make_pair('b', 200));
    mmp.insert(make_pair('c', 300));

    mmp.erase(mmp.begin(), mmp.find('b'));
    for(it = mmp.begin(); it != mmp.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it ->second << endl; // b - 200, c - 300
    }
    
}

 map.swap() —— 交换两个容器内部元素
 map.insert() —— 清空容器内部元素

#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    map<char, int> mp1, mp2;
    map<char, int> :: iterator it;

    mp1['a'] = 1;
    mp1['b'] = 2;

    mp2['a'] = 100;
    mp2['b'] = 200;

    mp1.swap(mp2);
    for(it = mp1.begin(); it != mp1.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it -> second << endl; // a - 100, b - 200
    }
    for(it = mp2.begin(); it != mp2.end(); it ++ ){
    
    
        cout << it -> first << ' ' << it -> second << endl; // a - 1, b - 2
    }

    mp1.clear();
    cout << mp1.size() << endl; // 0

}

6.其余操作(Operation)

在这里插入图片描述
 map.find() —— 按照元素的键进行查找返回元素的迭代器,若不存在返回end()

#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    map<char, int> mp;
    map<char, int> :: iterator it;

    mp['a'] = 1;
    mp['b'] = 2;
    mp['c'] = 3;

    it = mp.find('c');
    cout << it -> first << ' ' << it -> second << endl; // c - 3

    if(mp.find('d') == mp.end()){
    
    
        puts("该键查找的对应元素不存在"); // 不存在
    }

}

 map.count() —— 查找容器内部特定键元素的数量

#include<iostream>
#include<map>
using namespace std;

int main (){
    
    
    multimap<char, int> mmp;

    mmp.insert(pair<char, int>('a', 1));
    mmp.insert(pair<char, int>('a', 1));
    mmp.insert(pair<char, int>('b', 1));
    mmp.insert(pair<char, int>('c', 1));

    cout << mmp.count('a') << endl; // 2

}

猜你喜欢

转载自blog.csdn.net/weixin_51566349/article/details/129219805