c++ unordered_map和

版权声明:欢迎转载,转载需要明确表明转自本文 https://blog.csdn.net/u012442157/article/details/82995837

unordered_map

一、概念

unordered_map是一个类模板,叫做无序映射表,是C++11的新特性,和map有一些不同,主要体现在无序上,不会根据key大小去排序。

Unordered maps are associative containers that store elements formed by the combination of a key value and a mapped value, and which allows for fast retrieval of individual elements based on their keys.

无需映射表是关联的容器,它存储的是由键值(key)和映射值(map)组合而成的元素,并且允许根据键值(key)快速检索单个元素。

In an unordered_map, the key value is generally used to uniquely identify the element, while the mapped value is an object with the content associated to this key. Types of key and mapped value may differ.

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).
内部怎么实现的我也不是很懂呢

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

Unordered maps implement the direct access operator (operator[]) which allows for direct access of the mapped value using its key value as argument.

Iterators in the container are at least forward iterators.

二、容器的特性

1、Associative(关联)

Elements in associative containers are referenced by their key and not by their absolute position in the container.

2、Unordered(无序)

Unordered containers organize their elements using hash tables that allow for fast access to elements by their key.

3、Map(映射)

Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value.

4、Unique keys(唯一键值)

No two elements in the container can have equivalent keys.

5、Allocator-aware(能够感知内存分配器的)

The container uses an allocator object to dynamically handle its storage needs.

三、成员函数

1.

四、代码示例

#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

void main(){
    unordered_map<string,int> months;
    //插入数据
    cout<<"insert data"<<endl;
    months["january"]=31;
    months["february"] = 28;
    months["march"] = 31;
    months["september"] = 30;

    //直接使用key值访问键值对,如果没有访问到,返回0
    cout<<"september->"<<months["september"]<<endl;
    cout<<"xx->"<<months["xx"]<<endl;

    typedef unordered_map<int,int> mymap;
    mymap mapping;
    mymap::iterator it;
    mapping[2]=110;
    mapping[5]=220;
    const int x=2;const int y=3;//const是一个C语言(ANSI C)的关键字,它限定一个变量不允许被改变,产生静态作用,提高安全性。

    //寻找是否存在键值对
    //方法一:
    cout<<"find data where key=2"<<endl;
    if( mapping.find(x)!=mapping.end() ){//找到key值为2的键值对
        cout<<"get data where key=2! and data="<<mapping[x]<<endl;
    }
    cout<<"find data where key=3"<<endl;
    if( mapping.find(y)!=mapping.end() ){//找到key值为3的键值对
        cout<<"get data where key=3!"<<endl;
    }
    //方法二:
    it=mapping.find(x);
    printf("find data where key=2 ?  %d\n",(it==mapping.end()));
    it=mapping.find(y);
    printf("find data where key=3 ?  %d\n",(it==mapping.end()));

    //遍历hash table
    for( mymap::iterator iter=mapping.begin();iter!=mapping.end();iter++ ){
        cout<<"key="<<iter->first<<" and value="<<iter->second<<endl;
    }

    system("pause");
}

五、参考

http://www.cplusplus.com/reference/unordered_map/unordered_map/

猜你喜欢

转载自blog.csdn.net/u012442157/article/details/82995837