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

本文介绍了C++标准库中的std::multiset容器,它是一个保存键值对的有序集合,允许有重复元素。文章详细讲解了multiset的比较函数、key_comp和value_comp,以及如何按照字典序比较不同multiset。此外,还展示了multiset的交换操作和std::swap的特化实现,以及相关示例代码来演示这些功能的使用。
摘要由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>::key_comp

key_compare key_comp() const;

 返回用于比较关键的函数对象,它是此容器构造函数参数 comp 的副本,它与 value_comp 相同。

参数

(无)

返回值

比较关键的函数对象。

复杂度

常数

返回用于在value_type类型的对象中比较键的函数

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

std::multiset::value_compare value_comp() const;

返回比较值的函数对象。它与 key_comp 相同。

参数

(无)

返回值

比较值的函数对象。

复杂度

常数。


非成员函数

按照字典顺序比较 multiset 中的值

operator==,!=,<,<=,>,>=(std::multiset)
template< class Key, class Compare, class Alloc >

bool operator==( const std::multiset<Key,Compare,Alloc>& lhs,

                 const std::multiset<Key,Compare,Alloc>& rhs );
(1)
template< class Key, class Compare, class Alloc >

bool operator!=( const std::multiset<Key,Compare,Alloc>& lhs,

                 const std::multiset<Key,Compare,Alloc>& rhs );
(2)
template< class Key, class Compare, class Alloc >

bool operator<( const std::multiset<Key,Compare,Alloc>& lhs,

                const std::multiset<Key,Compare,Alloc>& rhs );
(3)
template< class Key, class Compare, class Alloc >

bool operator<=( const std::multiset<Key,Compare,Alloc>& lhs,

                 const std::multiset<Key,Compare,Alloc>& rhs );
(4)
template< class Key, class Compare, class Alloc >

bool operator>( const std::multiset<Key,Compare,Alloc>& lhs,

                const std::multiset<Key,Compare,Alloc>& rhs );
(5)
template< class Key, class Compare, class Alloc >

bool operator>=( const std::multiset<Key,Compare,Alloc>& lhs,

                 const std::multiset<Key,Compare,Alloc>& rhs );
(6)

 比较二个容器的内容。

1-2) 检查 lhsrhs 的内容是否相等,即它们是否拥有相同数量的元素且 lhs 中每个元素与 rhs 的同位置元素比较相等。

3-6) 按字典序比较 lhsrhs 的内容。由等价于 std::lexicographical_compare 的函数进行比较。此比较忽略容器的定序 Compare 。

参数

lhs, rhs-要比较内容的容器
- 为使用重载 (1-2) , Key 必须满足可相等比较 (EqualityComparable) 的要求。

返回值

1) 若容器内容相等则为 true ,否则为 false

2) 若容器内容不相等则为 true ,否则为 false

3) 若 lhs 的内容按字典序小于 rhs 的内容则为 true ,否则为 false

4) 若 lhs 的内容按字典序小于等于 rhs 的内容则为 true ,否则为 false

5) 若 lhs 的内容按字典序大于 rhs 的内容则为 true ,否则为 false

6) 若 lhs 的内容按字典序大于等于 rhs 的内容则为 true ,否则为 false

复杂度

1-2) 若 lhsrhs 的大小不同则为常数,否则与容器大小成线性

3-6) 与容器大小成线性

特化 std::swap 算法

std::swap(std::multiset)
template< class Key, class Compare, class Alloc >

void swap( multiset<Key,Compare,Alloc>& lhs,

           multiset<Key,Compare,Alloc>& rhs );
(C++17 前)
template< class Key, class Compare, class Alloc >

void swap( multiset<Key,Compare,Alloc>& lhs,

           multiset<Key,Compare,Alloc>& rhs ) noexcept(/* see below */);
(C++17 起)

为 std::multiset 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换内容的容器

返回值

(无)

复杂度

常数。

异常

noexcept 规定:  

noexcept(noexcept(lhs.swap(rhs)))

(C++17 起)

调用示例

#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;
}
void size_();
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;
    };

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

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

    std::cout << "multiset1 == multiset2 :  " << (multiset1 == multiset2) << std::endl;
    std::cout << "multiset1 != multiset2 :  " << (multiset1 != multiset2) << std::endl;
    std::cout << "multiset1 <  multiset2 :  " << (multiset1 <  multiset2) << std::endl;
    std::cout << "multiset1 <= multiset2 :  " << (multiset1 <= multiset2) << std::endl;
    std::cout << "multiset1 >  multiset2 :  " << (multiset1 >  multiset2) << std::endl;
    std::cout << "multiset1 >= multiset2 :  " << (multiset1 >= multiset2) << std::endl;

    std::cout << std::endl;


    Cell cell1(generate());
    Cell cell2(generate());
    std::cout << "multiset1.key_comp()(cell1, cell2):   "
              << cell1 << " --- " << cell2 << ":    "
              << multiset1.key_comp()(cell1, cell2) << std::endl;
    std::cout << std::endl;


    std::cout << "swap before:" << std::endl;
    std::multiset<Cell> multiset3{generate(), generate(), generate(), generate(), generate()};
    std::cout << "multiset3:    ";
    std::copy(multiset3.begin(), multiset3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::multiset<Cell> multiset4{generate(), generate(), generate(), generate(), generate()};
    std::cout << "multiset4:    ";
    std::copy(multiset4.begin(), multiset4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    //为 std::multiset 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::swap(multiset3, multiset4);
    std::cout << "swap after:" << std::endl;
    std::cout << "multiset3:    ";
    std::copy(multiset3.begin(), multiset3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "multiset4:    ";
    std::copy(multiset4.begin(), multiset4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值