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>::bucket_count

size_type bucket_count() const;

(C++11 起)

返回容器中的桶数。

参数

(无)

返回值

容器中的桶数。

复杂度

常数。

返回桶的最大数量

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::max_bucket_count

size_type max_bucket_count() const;

(C++11 起)

返回容器由于系统或库实现限制的能保有的最大桶数。

参数

(无)

返回值

最大桶数。

复杂度

常数。

返回在特定的桶中的元素数量

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::bucket_size

size_type bucket_size( size_type n ) const;

(C++11 起)

返回下标为 n 的桶中的元素数。

参数

n - 要检验的桶的下标

返回值

n 中的元素数。

复杂度

与桶 n 的大小成线性。

返回带有特定键的桶

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::bucket

size_type bucket( const Key& key ) const;

(C++11 起)

返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。返回值仅对 bucket_count() 返回相同值的容器实例合法。

若 bucket_count() 为零则行为未定义。

参数

key - 要检验的关键值

返回值

关键 key 的桶编号。

复杂度

常数。

调用示例

#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;
    while (unordered_map1.size() < 5)
    {
        //6) 插入来自 initializer_list ilist 的元素。
        unordered_map1.insert({generate()});
    }

    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(),
              std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map2;
    while (unordered_map2.size() < 5)
    {
        //6) 插入来自 initializer_list ilist 的元素。
        unordered_map2.insert({generate()});
    }

    std::cout << "unordered_map2:   ";
    std::copy(unordered_map2.begin(), unordered_map2.end(),
              std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //比较二个无序容器的内容。
    std::cout << "unordered_map1 == unordered_map2: " << (unordered_map1 == unordered_map2) << std::endl;
    std::cout << "unordered_map1 != unordered_map2: " << (unordered_map1 != unordered_map2) << std::endl;
    std::cout << std::endl;


    //为 std::unordered_map 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::swap(unordered_map1, unordered_map2);
    std::cout << "after swap:   " << std::endl;
    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(),
              std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "unordered_map2:   ";
    std::copy(unordered_map2.begin(), unordered_map2.end(),
              std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

void max_load_factor_load_factor()
{
    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;
    while (unordered_map1.size() < 6)
    {
        //返回每个桶元素的平均数,即 size() 除以 bucket_count() 。
        std::cout << "unordered_map1 load_factor :  " << unordered_map1.size()
                  << " / " << unordered_map1.bucket_count() << " = "
                  << unordered_map1.load_factor() << std::endl;

        //6) 插入来自 initializer_list ilist 的元素。
        unordered_map1.insert({generate()});
    }

    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(),
              std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map2;
    size_t index = 1;
    while (unordered_map2.size() < 3)
    {
        //管理最大加载因子(每个桶的平均元素数)。若加载因子超出此阈值,则容器自动增加桶数。
        //1) 返回最大加载因子。
        std::cout << "unordered_map2 max_load_factor    :  "
                  << unordered_map2.max_load_factor() << std::endl;

        //2) 设置最大加载因子为 ml 。
        unordered_map2.max_load_factor(index);
        std::cout << "unordered_map2 max_load_factor(" << index << ") :  "
                  << unordered_map2.max_load_factor() << std::endl;

        //返回每个桶元素的平均数,即 size() 除以 bucket_count() 。
        std::cout << "unordered_map2 load_factor        :  " << unordered_map2.size()
                  << " / " << unordered_map2.bucket_count() << " = "
                  << unordered_map2.load_factor() << std::endl;

        //6) 插入来自 initializer_list ilist 的元素。
        unordered_map2.insert({generate()});
        index++;
    }
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map3;
    index = 1;
    while (unordered_map3.size() < 3)
    {
        unordered_map3.max_load_factor(index);
        std::cout << "unordered_map3 max_load_factor(" << index << ") :  "
                  << unordered_map3.max_load_factor() << std::endl;

        //1) 返回最大加载因子。
        std::cout << "unordered_map3 max_load_factor    :  "
                  << unordered_map3.max_load_factor() << std::endl;

        //设置桶数为 count 并重哈希容器,即考虑桶总数已改变,再把元素放到适当的桶中
        unordered_map3.rehash(index);
        std::cout << "unordered_map3 rehash(" << index << ") " << std::endl;

        //返回每个桶元素的平均数,即 size() 除以 bucket_count() 。
        std::cout << "unordered_map3 load_factor        :  " << unordered_map3.size()
                  << " / " << unordered_map3.bucket_count() << " = "
                  << unordered_map3.load_factor() << std::endl;

        //6) 插入来自 initializer_list ilist 的元素。
        unordered_map3.insert({generate()});
        index++;
    }
    std::cout << std::endl;


    std::unordered_map<Cell, string, CHash, CEqual> unordered_map4;
    index = 1;
    while (unordered_map4.size() < 3)
    {
        unordered_map4.max_load_factor(index);
        std::cout << "unordered_map4 max_load_factor(" << index << ") :  "
                  << unordered_map4.max_load_factor() << std::endl;

        //1) 返回最大加载因子。
        std::cout << "unordered_map4 max_load_factor    :  "
                  << unordered_map4.max_load_factor() << std::endl;

        //设置桶数为 count 并重哈希容器,即考虑桶总数已改变,再把元素放到适当的桶中
        unordered_map4.reserve(index);
        std::cout << "unordered_map4 reserve(" << index << ") " << std::endl;

        //返回每个桶元素的平均数,即 size() 除以 bucket_count() 。
        std::cout << "unordered_map4 load_factor        :  " << unordered_map3.size()
                  << " / " << unordered_map4.bucket_count() << " = "
                  << unordered_map4.load_factor() << std::endl;

        //6) 插入来自 initializer_list ilist 的元素。
        unordered_map4.insert({generate()});
        index++;
    }
}

void cbegin_cend()
{
    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 的元素。
    while (unordered_map1.size() < 5)
    {
        unordered_map1.insert({generate()});
    }

    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(),
              std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    for (std::unordered_map<Cell, string, CHash, CEqual>::const_iterator cit =
                unordered_map1.cbegin(); cit != unordered_map1.end(); cit++)
    {
        //返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
        //返回值仅对 bucket_count() 返回相同值的容器实例合法。
        size_t bucket = unordered_map1.bucket(cit->first);
        std::cout << "unordered_map1 bucket(" << cit->first << ") :    "
                  << bucket << "    ";

        //返回指向下标为 n 的桶首元素的迭代器。
        std::unordered_map<Cell, string, CHash, CEqual>::const_local_iterator clit
            = unordered_map1.begin(bucket);
        std::cout << "const_local_iterator begin(" << bucket << "):    " << *clit << std::endl;
    }
    std::cout << std::endl;


    for (std::unordered_map<Cell, string, CHash, CEqual>::const_iterator cit =
                unordered_map1.cbegin(); cit != unordered_map1.end(); cit++)
    {
        //返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
        //返回值仅对 bucket_count() 返回相同值的容器实例合法。
        size_t bucket = unordered_map1.bucket(cit->first);
        std::cout << "unordered_map1 bucket(" << cit->first << ") :    "
                  << bucket << "    ";

        //返回指向下标为 n 的桶首元素的迭代器。
        std::unordered_map<Cell, string, CHash, CEqual>::local_iterator clit =
            unordered_map1.begin(bucket);
        std::cout << "local_iterator begin(" << bucket << "):    " << *clit << std::endl;
    }
    std::cout << std::endl;


    for (std::unordered_map<Cell, string, CHash, CEqual>::const_iterator cit =
                unordered_map1.cbegin(); cit != unordered_map1.end(); cit++)
    {
        //返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
        //返回值仅对 bucket_count() 返回相同值的容器实例合法。
        size_t bucket = unordered_map1.bucket(cit->first);
        std::cout << "unordered_map1 bucket(" << cit->first << ") :    "
                  << bucket << "    ";

        //返回指向下标为 n 的桶首元素的迭代器。
        std::unordered_map<Cell, string, CHash, CEqual>::const_local_iterator clit =
            unordered_map1.cbegin(bucket);
        std::cout << "const_local_iterator cbegin(" << bucket << "):    " << *clit << std::endl;
    }

}

void max_bucket_count_()
{
    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 的元素。
    while (unordered_map1.size() < 6)
    {
        unordered_map1.insert({generate()});
        //返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
        //返回值仅对 bucket_count() 返回相同值的容器实例合法。
        std::cout << "unordered_map1 bucket_count: " << unordered_map1.bucket_count() << std::endl;
    }
    std::cout << std::endl;

    std::cout << "unordered_map1:   ";
    std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator<pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    for (std::unordered_map<Cell, string, CHash, CEqual>::const_iterator cit =
                unordered_map1.cbegin(); cit != unordered_map1.end(); cit++)
    {
        //返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
        //返回值仅对 bucket_count() 返回相同值的容器实例合法。
        size_t bucket = unordered_map1.bucket(cit->first);
        std::cout << "unordered_map1 bucket(" << cit->first << ") :    "
                  << bucket << "    ";

        //返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。
        //返回值仅对 bucket_count() 返回相同值的容器实例合法。
        size_t bucket_size = unordered_map1.bucket_size(bucket);
        std::cout << "bucket_size(" << bucket << "):    " << bucket_size << std::endl;
    }
    std::cout << std::endl;


    //返回容器由于系统或库实现限制的能保有的最大桶数。
    std::unordered_map<char, char> unordered_map_c;
    std::cout << "unordered_map<char, char> max_bucket_count:           "
              << unordered_map_c.max_bucket_count() << std::endl;
    std::unordered_map<int, int> unordered_map_i;
    std::cout << "unordered_map<int, int> max_bucket_count:             "
              << unordered_map_i.max_bucket_count() << std::endl;
    std::unordered_map<uint8_t, uint8_t> unordered_map_ui8;
    std::cout << "unordered_map<uint8_t, uint8_t> max_bucket_count:     "
              << unordered_map_ui8.max_bucket_count() << std::endl;
    std::unordered_map<uint16_t, uint16_t> unordered_map_ui16;
    std::cout << "unordered_map<uint16_t, uint16_t> max_bucket_count:   "
              << unordered_map_ui16.max_bucket_count() << std::endl;
    std::unordered_map<uint32_t, uint32_t> unordered_map_ui32;
    std::cout << "unordered_map<uint32_t, uint32_t> max_bucket_count:   "
              << unordered_map_ui32.max_bucket_count() << std::endl;
    std::unordered_map<uint64_t, uint64_t> unordered_map_ui64;
    std::cout << "unordered_map<uint64_t, uint64_t> max_bucket_count:   "
              << unordered_map_ui64.max_bucket_count() << std::endl;
    std::unordered_map<short, short> unordered_map_s;
    std::cout << "unordered_map<short, short> max_bucket_count:         "
              << unordered_map_s.max_bucket_count() << std::endl;
    std::unordered_map<double, double> unordered_map_d;
    std::cout << "unordered_map<double, double> max_bucket_count:       "
              << unordered_map_d.max_bucket_count() << std::endl;
    std::unordered_map<float, float> unordered_map_f;
    std::cout << "unordered_map<float, float> max_bucket_count:         "
              << unordered_map_f.max_bucket_count() << std::endl;
    std::unordered_map<long, long> unordered_map_l;
    std::cout << "unordered_map<long, long> max_bucket_count:           "
              << unordered_map_l.max_bucket_count() << std::endl;
    std::unordered_map<long long, long long> unordered_map_ll;
    std::cout << "unordered_map<long long, long long> max_bucket_count: "
              << unordered_map_ll.max_bucket_count() << std::endl;
    std::unordered_map<string, string> unordered_map_str;
    std::cout << "unordered_map<string,string> max_bucket_count:       "
              << unordered_map_str.max_bucket_count() << std::endl;
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map_Cell;
    std::cout << "std::unordered_map<Cell, string, CHash, CEqual> max_bucket_count: "
              "" << unordered_map_Cell.max_bucket_count() << std::endl;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/129909294