> 文章列表 > c++11 标准模板(STL)(std::unordered_multimap)(十二)

c++11 标准模板(STL)(std::unordered_multimap)(十二)

c++11 标准模板(STL)(std::unordered_multimap)(十二)

定义于头文件 <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_multimap;

(1) (C++11 起)
namespace pmr {

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

}

(2) (C++17 起)

 unordered_multimap 是无序关联容器,支持等价的关键(一个 unordered_multimap 可含有每个关键值的多个副本)和将关键与另一类型的值关联。 unordered_multimap 类支持向前迭代器。搜索、插入和移除拥有平均常数时间复杂度。

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

不要求此容器的迭代顺序稳定(故例如 std::equal 不能用于比较二个 std::unordered_multimap ),除了关键比较等价(以 key_eq() 为比较器比较相等)的每组元素在迭代顺序中组成相接的子范围,它亦可用 equal_range() 访问。

桶接口

返回一个迭代器,指向指定的桶的开始

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::begin(size_type), 
std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::cbegin(size_type)

local_iterator begin( size_type n );

(C++11 起)

const_local_iterator begin( size_type n ) const;

(C++11 起)

const_local_iterator cbegin( size_type n ) const;

(C++11 起)

 返回指向下标n 的桶首元素的迭代器。

参数

n - 要访问的桶的下标

返回值

指向首元素的迭代器。

复杂度

常数。

返回一个迭代器,指向指定的桶的末尾

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::end(size_type), 
std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::cend(size_type)

local_iterator end( size_type n );

(C++11 起)

const_local_iterator end( size_type n ) const;

(C++11 起)

const_local_iterator cend( size_type n ) const;

(C++11 起)

返回后随下标为 n 的桶的最后元素的元素的迭代器。此元素表现为占位符,试图访问它会导致未定义行为。

参数

n - 要访问的桶的下标

返回值

指向后随最后元素的元素的迭代器。

复杂度

常数

调用示例

#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()
{auto generate = [](){int n = std::rand() % 10 + 110;Cell cell{n, n};return std::pair<Cell, string>(cell, std::to_string(n));};std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap1;//6) 插入来自 initializer_list ilist 的元素。while (unordered_multimap1.size() < 5){unordered_multimap1.insert({generate()});}std::cout << "unordered_multimap1:   ";std::copy(unordered_multimap1.begin(), unordered_multimap1.end(),std::ostream_iterator<pair<Cell, string>>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;for (std::unordered_multimap<Cell, string, CHash, CEqual>::const_iterator cit =unordered_multimap1.cbegin(); cit != unordered_multimap1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_multimap1.bucket(cit->first);std::cout << "unordered_multimap1 bucket(" << cit->first << ") :    "<< bucket << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_multimap<Cell, string, CHash, CEqual>::const_local_iterator clit= unordered_multimap1.begin(bucket);std::cout << "const_local_iterator begin(" << bucket << "):    " << *clit << std::endl;}std::cout << std::endl;for (std::unordered_multimap<Cell, string, CHash, CEqual>::const_iterator cit =unordered_multimap1.cbegin(); cit != unordered_multimap1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_multimap1.bucket(cit->first);std::cout << "unordered_multimap1 bucket(" << cit->first << ") :    "<< bucket << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_multimap<Cell, string, CHash, CEqual>::local_iterator clit =unordered_multimap1.begin(bucket);std::cout << "local_iterator begin(" << bucket << "):    " << *clit << std::endl;}std::cout << std::endl;for (std::unordered_multimap<Cell, string, CHash, CEqual>::const_iterator cit =unordered_multimap1.cbegin(); cit != unordered_multimap1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_multimap1.bucket(cit->first);std::cout << "unordered_multimap1 bucket(" << cit->first << ") :    "<< bucket << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_multimap<Cell, string, CHash, CEqual>::const_local_iterator clit =unordered_multimap1.cbegin(bucket);std::cout << "const_local_iterator cbegin(" << bucket << "):    " << *clit << std::endl;}return 0;
}

输出