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

本文详细介绍了C++标准库中的std::multiset容器,它是一个允许键值等价的有序集合。讨论了clear()函数用于清除所有元素,以及erase()函数的不同用法来删除单个元素或范围内的元素,以及按键值删除元素。所有这些操作的时间复杂度和行为都进行了说明,并提供了代码示例来演示这些操作的使用。
摘要由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>::clear

void clear();

(C++11 前)

void clear() noexcept;

(C++11 起)

 从容器擦除所有元素。此调用后 size() 返回零。

非法化任何指代所含元素的引用、指针或迭代器。任何尾后迭代器保持合法。

参数

(无)

返回值

(无)

复杂度

与容器大小,即元素数成线性。

擦除元素

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

void erase( iterator pos );

(1)(C++11 前)

iterator erase( const_iterator pos );

(C++11 起)

iterator erase( iterator pos );

(C++17 起)

void erase( iterator first, iterator last );

(2)(C++11 前)

iterator erase( const_iterator first, const_iterator last );

(C++11 起)

size_type erase( const key_type& key );

(3)

 从容器移除指定的元素。

1) 移除位于 pos 的元素。

2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。

3) 移除关键等于 key 的所有元素。

指向被擦除元素的引用和迭代器被非法化。其他引用和迭代器不受影响。

迭代器 pos 必须合法且可解引用。从而 end() 迭代器(合法,但不可解引用)不能用作 pos 所用的值。

参数

pos-指向要移除的元素的迭代器
first, last-要移除的元素范围
key-要移除的元素关键值

返回值

1-2) 后随最后被移除的元素的迭代器。

3) 被移除的元素数。

异常

1,2) (无)

3) 任何 Compare 对象所抛的异常

复杂度

给定 multiset 的实例 c

1) 均摊常数

2) log(c.size()) + std::distance(first, last)

3) log(c.size()) + c.count(k)

调用示例

#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()};
    for (size_t index = 0; index < 3; index++)
    {
        std::cout << "multiset1:    ";
        std::copy(multiset1.begin(), multiset1.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        //1) 移除位于 pos 的元素。
        multiset1.erase(multiset1.begin());
    }
    std::cout << std::endl;

    std::multiset<Cell> multiset2{generate(), generate(), generate(), generate(), generate()};
    for (size_t index = 0; index < 3; index++)
    {
        std::cout << "multiset2:    ";
        std::copy(multiset2.begin(), multiset2.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        //1) 移除位于 pos 的元素。
        multiset2.erase(multiset2.cbegin());
    }
    std::cout << 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>::iterator itb = multiset3.begin();
    std::multiset<Cell>::iterator ite = multiset3.end();
    //2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。
    multiset3.erase(++itb, --ite);
    std::cout << "multiset3:    ";
    std::copy(multiset3.begin(), multiset3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::multiset<Cell> multiset4{generate(), generate(), generate(), generate(), generate()};
    std::cout << "multiset4:    ";
    std::copy(multiset4.cbegin(), multiset4.cend(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::multiset<Cell>::const_iterator itb1 = multiset4.cbegin();
    std::multiset<Cell>::const_iterator ite1 = multiset4.cend();
    //2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。
    multiset4.erase(++itb1, --ite1);
    std::cout << "multiset4:    ";
    std::copy(multiset4.cbegin(), multiset4.cend(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    //3) 移除关键等于 key 的所有元素。
    std::multiset<Cell> multiset5{generate(), generate(), generate(), generate(), generate()};
    std::cout << "multiset5:    ";
    std::copy(multiset5.cbegin(), multiset5.cend(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    for (size_t index = 0; index < 3; index++)
    {
        Cell cell = *multiset5.begin();
        std::cout << "erase cell:   " << cell << std::endl;
        multiset5.erase(cell);
        std::cout << "multiset5:    ";
        std::copy(multiset5.cbegin(), multiset5.cend(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;

    //从容器擦除所有元素。此调用后 size() 返回零。
    std::multiset<Cell> multiset6{generate(), generate(), generate(), generate(), generate()};
    std::cout << "multiset6:    ";
    std::copy(multiset6.cbegin(), multiset6.cend(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    multiset6.clear();
    std::cout << "after clear multiset6 size: " << multiset6.size() << std::endl;

    return 0;
}

输出

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值