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>::key_comp

key_compare key_comp() const;

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

参数

(无)

返回值

比较关键的函数对象。

扫描二维码关注公众号,回复: 15184934 查看本文章

复杂度

常数

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

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

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

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

参数

(无)

返回值

比较值的函数对象。

复杂度

常数。

非成员函数

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

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

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

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

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

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

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

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

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

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

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

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

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

                 const std::set<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) 与容器大小成线性

调用示例

#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()
{
    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::set<Cell> set1{generate(), generate(), generate(), generate(), generate()};
    std::cout << "set1: ";
    std::copy(set1.begin(), set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

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

    std::cout << "set1 == set2 :  " << (set1 == set2) << std::endl;
    std::cout << "set1 != set2 :  " << (set1 != set2) << std::endl;
    std::cout << "set1 <  set2 :  " << (set1 <  set2) << std::endl;
    std::cout << "set1 <= set2 :  " << (set1 <= set2) << std::endl;
    std::cout << "set1 >  set2 :  " << (set1 >  set2) << std::endl;
    std::cout << "set1 >= set2 :  " << (set1 >= set2) << std::endl;

    std::cout << std::endl;


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


    std::cout << "swap before:" << 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> set4{generate(), generate(), generate(), generate(), generate()};
    std::cout << "set4:    ";
    std::copy(set4.begin(), set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    //为 std::set 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::swap(set3, set4);
    std::cout << "swap after:" << std::endl;
    std::cout << "set3:    ";
    std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "set4:    ";
    std::copy(set4.begin(), set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130778949
今日推荐