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

本文介绍了C++标准库中的std::multiset容器,它是一个保存键值对象有序集合的容器,允许键值相等的多个元素存在。文章详细讲解了multiset的empty()、size()和max_size()方法,用于检查容器状态和获取元素数量。并提供了一个示例程序,展示了如何使用这些方法以及multiset的基本操作。
摘要由CSDN通过智能技术生成
定义于头文件 <set>
template<

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>

> class multiset;
(1)
namespace pmr {

    template <class Key, class Compare = std::less<Key>>
    using multiset = std::multiset<Key, Compare,
                                   std::pmr::polymorphic_allocator<Key>>;

}
(2)(C++17 起)

std::multiset 是含有 Key 类型对象有序集的容器。不同于 set ,它允许多个关键拥有等价的值。用关键比较函数 Compare 进行排序。搜索、插入和移除操作拥有对数复杂度。

在标准库使用比较 (Compare) 概念的每处,都用描述于比较 (Compare) 的等价关系确定等价性。不精确地说,若二个对象 ab 互不比较小于对方: !comp(a, b) && !comp(b, a) ,则认为它们等价。

容量

检查容器是否为空

std::multiset<Key,Compare,Allocator>::empty

bool empty() const;

(C++11 前)

bool empty() const noexcept;

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

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

(C++20 起)

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

参数

(无)

返回值

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

复杂度

常数。

返回容纳的元素数

std::multiset<Key,Compare,Allocator>::size

size_type size() const;

(C++11 前)

size_type size() const noexcept;

(C++11 起)

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

参数

(无)

返回值

容器中的元素数量。

复杂度

常数。

返回可容纳的最大元素数

std::multiset<Key,Compare,Allocator>::max_size

size_type max_size() const;

(C++11 前)

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 <time.h>
#include <set>

using namespace std;

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;
    }
};

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

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

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

    auto generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    //检查容器是否无元素,即是否 begin() == end() 。
    std::multiset<Cell> multiset1;
    std::cout << "multiset1 is empty:   " << multiset1.empty() << std::endl;
    std::multiset<Cell> multiset2{generate()};
    std::cout << "multiset2 is empty:   " << multiset2.empty() << std::endl;
    std::cout << std::endl;


    //返回容器中的元素数,即 std::distance(begin(), end()) 。
    std::multiset<Cell> multiset3;
    for (size_t index = 0; index < 6; index++)
    {
        std::cout << "multiset3 size: " << multiset3.size() << "  ---  ";
        std::copy(multiset3.begin(), multiset3.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        multiset3.insert(generate());
    }

    std::cout << std::endl;


    //返回根据系统或库实现限制的容器可保有的元素最大数量,
    //即对于最大容器的 std::distance(begin(), end()) 。
    std::multiset<char> multiset_c;
    std::cout << "multiset<char> max_size:      " << multiset_c.max_size() << std::endl;
    std::multiset<int> multiset_i;
    std::cout << "multiset<int> max_size:       " << multiset_i.max_size() << std::endl;
    std::multiset<uint8_t> multiset_ui8;
    std::cout << "multiset<uint8_t> max_size:   " << multiset_ui8.max_size() << std::endl;
    std::multiset<uint16_t> multiset_ui16;
    std::cout << "multiset<uint16_t> max_size:  " << multiset_ui16.max_size() << std::endl;
    std::multiset<uint32_t> multiset_ui32;
    std::cout << "multiset<uint32_t> max_size:  " << multiset_ui32.max_size() << std::endl;
    std::multiset<uint64_t> multiset_ui64;
    std::cout << "multiset<uint64_t> max_size:  " << multiset_ui64.max_size() << std::endl;
    std::multiset<short> multiset_s;
    std::cout << "multiset<short> max_size:     " << multiset_s.max_size() << std::endl;
    std::multiset<double> multiset_d;
    std::cout << "multiset<double> max_size:    " << multiset_d.max_size() << std::endl;
    std::multiset<float> multiset_f;
    std::cout << "multiset<float> max_size:     " << multiset_f.max_size() << std::endl;
    std::multiset<long> multiset_l;
    std::cout << "multiset<long> max_size:      " << multiset_l.max_size() << std::endl;
    std::multiset<long long> multiset_ll;
    std::cout << "multiset<long long> max_size: " << multiset_ll.max_size() << std::endl;
    std::multiset<string> multiset_str;
    std::cout << "multiset<string> max_size:    " << multiset_str.max_size() << std::endl;
    std::multiset<Cell> multiset_Cell;
    std::cout << "multiset<Cell> max_size:      " << multiset_Cell.max_size() << std::endl;

    return 0;
}

输出

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值