c++ unordered_map和

本文深入探讨了C++11中unordered_map容器的概念、特性和成员函数,包括其无序性、快速检索能力以及与map的区别。通过代码示例展示了如何使用unordered_map进行数据的插入、查找和遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

三、成员函数

构造函数(constructor)

Construct unordered_map (public member function )
(destructor)
Destroy unordered map (public member function)
operator=
Assign content (public member function )

容量

Capacity
empty
Test whether container is empty (public member function)
size
Return container size (public member function)
max_size
Return maximum size (public member function)

迭代器

Iterators
begin
Return iterator to beginning (public member function)
end
Return iterator to end (public member function)
cbegin
Return const_iterator to beginning (public member function)
cend
Return const_iterator to end (public member function)

获取元素

Element access
operator[]
Access element (public member function )
at
Access element (public member function)

查找元素

Element lookup
find
Get iterator to element (public member function)
count
Count elements with a specific key (public member function )
equal_range
Get range of elements with specific key (public member function)

修改

Modifiers
emplace
Construct and insert element (public member function )
emplace_hint
Construct and insert element with hint (public member function )
insert
Insert elements (public member function )
erase
Erase elements (public member function )
clear
Clear content (public member function )
swap
Swap content (public member function)

Buckets
bucket_count
Return number of buckets (public member function)
max_bucket_count
Return maximum number of buckets (public member function)
bucket_size
Return bucket size (public member type)
bucket
Locate element’s bucket (public member function)

哈希

Hash policy
load_factor
Return load factor (public member function)
max_load_factor
Get or set maximum load factor (public member function )
rehash
Set number of buckets (public member function )
reserve
Request a capacity change (public member function)

Observers

Observers
hash_function
Get hash function (public member type)
key_eq
Get key equivalence predicate (public member type)
get_allocator
Get allocator (public member function)

四、代码示例

#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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值