C++基础 --- map与unordered_map 用法整理

1. map

map是STL的一个关联容器,它提供一对一的hash。
构建 map

#include <map>	// 使用 map 得包含 map 类所在的头文件
using namespace std;
// key 和 value 可以是任意你需要的类型,包括自定义类型。
map<Key, Value> mapExample;	// map 的构造函数
  • 第一个可以称为关键字(key),每个关键字只能在map中出现一次;
  • 第二个可能称为该关键字的值(value);

插入元素

#include <map>	// 使用 map 得包含 map 类所在的头文件
using namespace std;

// 定义一个map对象
map<int, string> mapStudent;
 
// 第一种 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
 
// 第二种 用insert函数插入value_type数据
// 涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是不能在插入数据的
mapStudent.insert(map<int, string>::value_type(001, "student_one"));
 
// 第三种 用"array"方式插入
mapStudent[123] = "student_first";
mapStudent[456] = "student_second";

查找元素

// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
iter = mapStudent.find("123");
 
if(iter != mapStudent.end())
       cout<<"Find, the value is"<<iter->second<<endl;
else
   cout<<"Do not Find"<<endl;

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

int main(){
    map<int, int> mapExample;
    mapExample.insert(pair<int,int>(11,3));
    mapExample.insert(pair<int,int>(12,4));
    mapExample.insert(pair<int,int>(13,5));

    auto position = mapExample.find(13);
    if (position != mapExample.end()){
        cout << "find element position is " << position->second << endl;
        cout << "find element position is " << position->first << endl;
    }
}

删除与清空元素与map大小

//迭代器刪除
iter = mapExample.find(12);
mapExample.erase(iter);
 
//用关键字刪除
int n = mapExample.erase(13); //如果刪除了會返回1,否則返回0
 
//用迭代器范围刪除 : 把整个map清空
mapExample.erase(mapExample.begin(), mapExample.end());
//等同于mapStudent.clear()

// 查询map的大小
int nSize = mapExample.size();

基本操作函数

	 begin()         返回指向map头部的迭代器

     clear()        删除所有元素

     count()         返回指定元素出现的次数

     empty()         如果map为空则返回true

     end()           返回指向map末尾的迭代器

     equal_range()   返回特殊条目的迭代器对

     erase()         删除一个元素

     find()          查找一个元素

     get_allocator() 返回map的配置器

     insert()        插入元素

     key_comp()      返回比较元素key的函数

     lower_bound()   返回键值>=给定元素的第一个位置

     max_size()      返回可以容纳的最大元素个数

     rbegin()        返回一个指向map尾部的逆向迭代器

     rend()          返回一个指向map头部的逆向迭代器

     size()          返回map中元素的个数

     swap()           交换两个map

     upper_bound()    返回键值>给定元素的第一个位置

     value_comp()     返回比较元素value的函数


2. unordered_map

构建 unordered_map

#include <unordered_map>	// 使用 map 得包含 map 类所在的头文件
using namespace std;
// key 和 value 可以是任意你需要的类型,包括自定义类型。
map<Key, Value> mapExample;	// map 的构造函数

基本操作

// 插入操作
mp.insert(Map::value_type(1,"Raoul"));
mp.insert(pair(1,"Raoul"));
mp[1]="Raoul";

// 查找操作
若有unordered_map <int, int> mp;查找x是否在map中
    方法1:  若存在  mp.find(x)!=mp.end()
    方法2:  若存在  mp.count(x)!=0
    // 可见查找元素方面和 map 类似


#include <unordered_map>	// 使用 map 得包含 map 类所在的头文件
#include <iostream>
using namespace std;
// key 和 value 可以是任意你需要的类型,包括自定义类型。
unordered_map<int, int> unordered_mapExample;	// map 的构造函数

int main(){
    unordered_mapExample.insert(pair<int,int>(11,3));
    unordered_mapExample.insert(pair<int,int>(12,4));
    unordered_mapExample.insert(pair<int,int>(13,5));
    unordered_mapExample[14] = 6;
    auto position = unordered_mapExample.find(14);
    if (position != unordered_mapExample.end()){
        cout << "find element position is " << position->second << endl;
        cout << "find element position is " << position->first << endl;
    }
}

遍历

 unordered_map<key,T>::iterator it;
    (*it).first;   //the key value
    (*it).second   //the mapped value
    for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
          cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second;

    //也可以这样
    for(auto& v : mp)
        print v.first and v.second

3. map 与 unordered_map 的区别

内部实现机理

  • map: map内部实现了一个红黑树,该结构具有自动排序的功能,因此map内部的所有元素都是有序的,红黑树的每一个节点都代表着map的一个元素,因此,对于map进行的查找,删除,添加等一系列的操作都相当于是对红黑树进行这样的操作,故红黑树的效率决定了map的效率。
  • unordered_map: unordered_map内部实现了一个哈希表,因此其元素的排列顺序是杂乱的,无序的

优缺点

  • map:
    优点:有序性,这是map结构最大的优点,其元素的有序性在很多应用中都会简化很多的操作红黑树,内部实现一个红黑书使得map的很多操作在的时间复杂度下就可以实现,因此效率非常的高
    缺点:空间占用率高,因为map内部实现了红黑树,虽然提高了运行效率,但是因为每一个节点都需要额外保存父节点,孩子节点以及红/黑性质,使得每一个节点都占用大量的空间

  • unordered_map
    优点:
    因为内部实现了哈希表,因此其查找速度非常的快
    缺点:
    哈希表的建立比较耗费时间

适用处

  • map:对于那些有顺序要求的问题,用map会更高效一些
  • unordered_map :对于查找问题,unordered_map会更加高效一些,因此遇到查找问题,常会考虑一下用unordered_map
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值