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

定义于头文件 <set>
template<

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

> class set;
(1)
namespace pmr {

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

}
(2) (C++17 起)

std::set 是关联容器,含有 Key 类型对象的已排序集。用比较函数 比较 (Compare) 进行排序。搜索、移除和插入拥有对数复杂度。 set 通常以红黑树实现。

在每个标准库使用比较 (Compare) 概念的场所,用等价关系确定唯一性。不精确地说,若二个对象 ab 相互间既不比较大于亦不比较小于: !comp(a, b) && !comp(b, a) ,则认为它们等价。

std::set 满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。

修改器

清除内容

std::set<Key,Compare,Allocator>::clear

void clear();

(C++11 前)

void clear() noexcept;

(C++11 起)

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

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

参数

(无)

返回值

(无)

复杂度

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

擦除元素

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

void erase( iterator pos );

(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 对象所抛的异常

复杂度

给定 set 的实例 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;

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()
{
    auto generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    //从容器移除指定的元素
    std::set<Cell> set1{generate(), generate(), generate(), generate(), generate()};
    for (size_t index = 0; index < 3; index++)
    {
        std::cout << "set1:    ";
        std::copy(set1.begin(), set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        //1) 移除位于 pos 的元素。
        set1.erase(set1.begin());
    }
    std::cout << std::endl;

    std::set<Cell> set2{generate(), generate(), generate(), generate(), generate()};
    for (size_t index = 0; index < 3; index++)
    {
        std::cout << "set2:    ";
        std::copy(set2.begin(), set2.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        //1) 移除位于 pos 的元素。
        set2.erase(set2.cbegin());
    }
    std::cout << std::endl;


    std::set<Cell> set3{generate(), generate(), generate(), generate(), generate()};
    std::cout << "set3:    ";
    std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::set<Cell>::iterator itb = set3.begin();
    std::set<Cell>::iterator ite = set3.end();
    //2) 移除范围 [first; last) 中的元素,它必须是 *this 中的合法范围。
    set3.erase(++itb, --ite);
    std::cout << "set3:    ";
    std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


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

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

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

输出

猜你喜欢

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