c++11 标准模板(STL)(std::unordered_map)(八)

定义于头文件 <unordered_map>
template<

    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >

> class unordered_map;
(1)(C++11 起)
namespace pmr {

    template <class Key,
              class T,
              class Hash = std::hash<Key>,
              class KeyEqual = std::equal_to<Key>>
              using unordered_map = std::unordered_map<Key, T, Hash, Pred,
                              std::pmr::polymorphic_allocator<std::pair<const Key,T>>>;

}
(2)(C++17 起)

 

unordered_map 是关联容器,含有带唯一键的键-值 pair 。搜索、插入和元素移除拥有平均常数时间复杂度。

元素在内部不以任何特定顺序排序,而是组织进桶中。元素放进哪个桶完全依赖于其键的哈希。这允许对单独元素的快速访问,因为一旦计算哈希,则它准确指代元素所放进的桶。


修改器

清除内容

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::clear

void clear() noexcept;

(C++11 起)

 从容器擦除所有元素。此调用后 size() 返回零。

非法化任何指代所含元素的引用、指针或迭代器。可能亦非法化尾后迭代器。

参数

(无)

返回值

(无)

复杂度

与容器大小,即元素数成线性。

擦除元素

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::erase

iterator erase( const_iterator pos );

(1)(C++11 起)

iterator erase( const_iterator first, const_iterator last );

(2)(C++11 起)

size_type erase( const key_type& key );

(3)(C++11 起)

 从容器移除指定的元素。

1) 移除位于 pos 的元素。

2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。

3) 移除关键等于 key 的元素(若存在一个)。

到被擦除元素的引用和迭代器被非法化。其他迭代器和引用不被非法化。

迭代器 pos 必须合法且可解引用。从而 end() 迭代器(合法,但不可解引用)不能用作 pos 所用的值。

保留未被擦除的元素顺序(这使得可能在迭代通过容器时擦除单独的元素)。

(C++14 起)

参数

pos-指向要移除的元素的迭代器
first, last-要移除的元素范围
key-要移除的元素关键值

返回值

1-2) 后随最后被移除的元素的迭代器。

3) 被移除的元素数。

异常

1,2) (无)

3) 任何 Compare 对象所抛的异常

复杂度

给定 unordered_map 的实例 c

1) 平均情况:常数,最坏情况: c.size()

2) 平均情况: std::distance(first, last) ,最坏情况: c.size()

3) 平均情况: c.count(key) ,最坏情况: c.size()

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

struct myCompare
{
    bool operator()(const int &a, const int &b)
    {
        return a < b;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

std::ostream &operator<<(std::ostream &os, const std::pair<Cell, string> &pCell)
{
    os << pCell.first << "-" << pCell.second;
    return os;
}

struct CHash
{
    size_t operator()(const Cell& cell) const
    {
        size_t thash = std::hash<int>()(cell.x) | std::hash<int>()(cell.y);
//        std::cout << "CHash: " << thash << std::endl;
        return thash;
    }
};

struct CEqual
{
    bool operator()(const Cell &a, const Cell &b) const
    {
        return a.x == b.x && a.y == b.y;
    }
};

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return std::pair<Cell, string>(cell, std::to_string(n));
    };

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map1;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_map1.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "unordered_map1 empty :    " << unordered_map1.empty() << std::endl;
    //从容器擦除所有元素。此调用后 size() 返回零。
    unordered_map1.clear();
    std::cout << "unordered_map1 empty :    " << unordered_map1.empty() << std::endl;
    std::cout << "unordered_map1 size  :    " << unordered_map1.size() << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map2;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_map2.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_map2:   ";
    std::copy(unordered_map2.begin(), unordered_map1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    size_t usize = unordered_map2.size();
    for (size_t index = 0; index < usize - 1; index++)
    {
        //从容器移除指定的元素。1) 移除位于 pos 的元素。
        std::unordered_map<Cell, string, CHash, CEqual>::iterator rit = unordered_map2.erase(unordered_map2.cbegin());
        std::cout << "unordered_map2 erase iterator :   " << *rit << std::endl;
    }
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map3;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_map3.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_map3:   ";
    std::copy(unordered_map3.begin(), unordered_map3.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::unordered_map<Cell, string, CHash, CEqual>::const_iterator bcit = unordered_map3.cbegin();
    bcit++;
    std::unordered_map<Cell, string, CHash, CEqual>::const_iterator ecit = bcit;
    ecit++, ecit++;
    unordered_map3.erase(bcit, ecit);
    std::cout << "unordered_map3:   ";
    std::copy(unordered_map3.begin(), unordered_map3.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map4;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_map4.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_map4:   ";
    std::copy(unordered_map4.begin(), unordered_map4.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    usize = unordered_map4.size();
    for (size_t index = 0; index < usize - 1; index++)
    {
        //从容器移除指定的元素。3) 移除关键等于 key 的元素(若存在一个)。
        size_t esize = unordered_map4.erase(unordered_map4.cbegin()->first);
        std::cout << "unordered_map4 erase iterator esize:   " << esize << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

输出

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值