c++11 标准模板(STL)(std::unordered_multiset)(五)

定义于头文件 <unordered_map>
template<

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

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

    template <class Key,
              class Hash = std::hash<Key>,
              class Pred = std::equal_to<Key>>
    using unordered_multiset = std::unordered_multiset<Key, Hash, Pred,
                                   std::pmr::polymorphic_allocator<Key>>

}
(2) (C++17 起)

unordered_multiset 是关联容器,含有可能非唯一 Key 类型对象的集合。搜索、插入和移除拥有平均常数时间复杂度。

元素在内部并不以任何顺序排序,只是被组织到桶中。元素被放入哪个桶完全依赖其值的哈希。这允许快速访问单独的元素,因为一旦计算哈希,它就指代放置该元素的准确的桶。

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


容量

检查容器是否为空

std::unordered_multiset<Key,Hash,KeyEqual,Allocator>::empty

bool empty() const noexcept;

(C++11 起)
(C++20 前)

[[nodiscard]] bool empty() const noexcept;

(C++20 起)

 检查容器是否无元素,即是否 begin() == end() 。

参数

(无)

返回值

若容器为空则为 true ,否则为 false

复杂度

常数。

返回容纳的元素数

std::unordered_multiset<Key,Hash,KeyEqual,Allocator>::size

size_type size() const noexcept;

(C++11 起)

返回容器中的元素数,即 std::distance(begin(), end()) 。

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

返回可容纳的最大元素数

std::unordered_multiset<Key,Hash,KeyEqual,Allocator>::max_size

size_type max_size() const noexcept;

(C++11 起)

 返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。

参数

(无)

返回值

元素数量的最大值。

复杂度

常数。

注意

此值通常反映容器大小上的理论极限,至多为 std::numeric_limits<difference_type>::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_size() 的值。

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_set>
#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<const int, Cell> &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 cell;
    };

    std::vector<Cell> vector1(6);
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:          ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //检查容器是否无元素,即是否 begin() == end() 。
    std::unordered_multiset<Cell, CHash, CEqual> unordered_multiset1;
    std::cout << "unordered_multiset1 is empty " << unordered_multiset1.empty() << std::endl;

    unordered_multiset1 = {generate(), generate(), generate(), generate(), generate(), generate()};;
    std::cout << "unordered_multiset1:   ";
    std::copy(unordered_multiset1.begin(), unordered_multiset1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "unordered_multiset1 is empty " << unordered_multiset1.empty() << std::endl;
    std::cout << std::endl;

    std::unordered_multiset<Cell, CHash, CEqual> unordered_multiset2;
    for (size_t index = 0; index < 6; index++)
    {
        unordered_multiset2.emplace(std::rand() % 10 + 100, std::rand() % 10 + 100);
        //返回容器中的元素数,即 std::distance(begin(), end()) 。
        std::cout << "unordered_multiset2 size:  " << unordered_multiset2.size() << std::endl;
        std::copy(unordered_multiset2.begin(), unordered_multiset2.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;

    //返回根据系统或库实现限制的容器可保有的元素最大数量,
    //即对于最大容器的 std::distance(begin(), end()) 。
    std::unordered_multiset<char> unordered_multiset_c;
    std::cout << "unordered_multiset<char> max_size:      " << unordered_multiset_c.max_size() << std::endl;
    std::unordered_multiset<int> unordered_multiset_i;
    std::cout << "unordered_multiset<int> max_size:       " << unordered_multiset_i.max_size() << std::endl;
    std::unordered_multiset<uint8_t> unordered_multiset_ui8;
    std::cout << "unordered_multiset<uint8_t> max_size:   " << unordered_multiset_ui8.max_size() << std::endl;
    std::unordered_multiset<uint16_t> unordered_multiset_ui16;
    std::cout << "unordered_multiset<uint16_t> max_size:  " << unordered_multiset_ui16.max_size() << std::endl;
    std::unordered_multiset<uint32_t> unordered_multiset_ui32;
    std::cout << "unordered_multiset<uint32_t> max_size:  " << unordered_multiset_ui32.max_size() << std::endl;
    std::unordered_multiset<uint64_t> unordered_multiset_ui64;
    std::cout << "unordered_multiset<uint64_t> max_size:  " << unordered_multiset_ui64.max_size() << std::endl;
    std::unordered_multiset<short> unordered_multiset_s;
    std::cout << "unordered_multiset<short> max_size:     " << unordered_multiset_s.max_size() << std::endl;
    std::unordered_multiset<double> unordered_multiset_d;
    std::cout << "unordered_multiset<double> max_size:    " << unordered_multiset_d.max_size() << std::endl;
    std::unordered_multiset<float> unordered_multiset_f;
    std::cout << "unordered_multiset<float> max_size:     " << unordered_multiset_f.max_size() << std::endl;
    std::unordered_multiset<long> unordered_multiset_l;
    std::cout << "unordered_multiset<long> max_size:      " << unordered_multiset_l.max_size() << std::endl;
    std::unordered_multiset<long long> unordered_multiset_ll;
    std::cout << "unordered_multiset<long long> max_size: " << unordered_multiset_ll.max_size() << std::endl;
    std::unordered_multiset<string> unordered_multiset_str;
    std::cout << "unordered_multiset<string> max_size:    " << unordered_multiset_str.max_size() << std::endl;
    std::unordered_multiset<Cell, CHash, CEqual> unordered_multiset_Cell;
    std::cout << "unordered_multiset<Cell> max_size:      " << unordered_multiset_Cell.max_size() << std::endl;
    return 0;
}

输出

猜你喜欢

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