C++11 standard template (STL) std::set (4)

Defined in the header file <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) (since C++17)

 

std::setis an associative container containing a sorted set of objects Keyof type . Use the comparison function Compare (Compare) for sorting. Search, removal, and insertion have logarithmic complexity. setUsually implemented as a red-black tree .

In every place where the standard library uses the Compare concept, an equivalence relation is used to determine uniqueness. Inexactly, two objects aand bconsidered equivalent if they are neither greater than nor less than each other: !comp(a, b) && !comp(b, a).

std::setMeet the requirements of Container , AllocatorAwareContainer , AssociativeContainer and ReversibleContainer .

iterator

Returns an iterator pointing to the first element of the container

std::set<Key,Compare,Allocator>::begin, 
std::set<Key,Compare,Allocator>::cbegin

iterator begin();

(until C++11)

iterator begin() noexcept;

(since C++11)

const_iterator begin() const;

(until C++11)

const_iterator begin() const noexcept;

(since C++11)

const_iterator cbegin() const noexcept;

(since C++11)

 Returns an iterator pointing to the first element of the container.

If the container is empty, the returned iterator will be equal to end().

 

parameter

(none)

return value

Iterator pointing to the first element.

the complexity

constant.

Notice

Since iteratorboth and const_iteratorare const iterators (and can actually be of the same type), it is not possible to modify container elements through the iterators returned by any of these member functions.

returns an iterator pointing to the end of the container

std::set<Key,Compare,Allocator>::end, 
std::set<Key,Compare,Allocator>::cend

iterator end();

(until C++11)

iterator end() noexcept;

(since C++11)

const_iterator end() const;

(until C++11)

const_iterator end() const noexcept;

(since C++11)

const_iterator cend() const noexcept;

(since C++11)

 Returns an iterator pointing to the element following the last element of the container.

This element behaves as a placeholder; attempting to access it results in undefined behavior.

 

parameter

(none)

return value

An iterator pointing to the last element following.

the complexity

constant.

Notice

Since iteratorboth and const_iteratorare const iterators (and can actually be of the same type), it is not possible to modify container elements through the iterators returned by any of these member functions.

Returns a reverse iterator pointing to the last element of the container

std::set<Key,Compare,Allocator>::rbegin, 
std::set<Key,Compare,Allocator>::crbegin

reverse_iterator rbegin();

(until C++11)

reverse_iterator rbegin() noexcept;

(since C++11)

const_reverse_iterator rbegin() const;

(until C++11)

const_reverse_iterator rbegin() const noexcept;

(since C++11)

const_reverse_iterator crbegin() const noexcept;

(since C++11)

 Returns a reverse iterator pointing to the first element of the reversed container. It corresponds to the last element of the non-reverse container.

 

parameter

(none)

return value

Reverse iterator pointing to the first element.

the complexity

constant.

Notice

Since iteratorboth and const_iteratorare const iterators (and can actually be of the same type), it is not possible to modify container elements through the iterators returned by any of these member functions.

returns a reverse iterator pointing to the front

std::set<Key,Compare,Allocator>::rend, 
std::set<Key,Compare,Allocator>::crend

reverse_iterator rend();

(until C++11)

reverse_iterator rend() noexcept;

(since C++11)

const_reverse_iterator rend() const;

(until C++11)

const_reverse_iterator rend() const noexcept;

(since C++11)

const_reverse_iterator crend() const noexcept;

(since C++11)

Returns a reverse iterator pointing to the element following the last element of the reversed container. It corresponds to the previous element of the first element of the non-reverse container. This element behaves as a placeholder and attempting to access it results in undefined behavior.

 

parameter

(none)

return value

Reverse iterator pointing to the element after the last element.

the complexity

constant.

Notice

Since iteratorboth and const_iteratorare const iterators (and can actually be of the same type), it is not possible to modify container elements through the iterators returned by any of these member functions.

 

call example

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

    //返回指向容器首元素的迭代器。若容器为空,则返回的迭代器将等于 end() 。
    //返回指向容器末元素后一元素的迭代器。此元素表现为占位符;试图访问它导致未定义行为。
    //返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
    //返回指向逆向容器末元素后一元素的逆向迭代器。
    //它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。

    std::set<Cell> set1{generate(), generate(), generate(), generate(), generate()};
    std::cout << "set1 const it:           ";
    for (std::set<Cell>::const_iterator cit = set1.cbegin(); cit != set1.cend(); cit++)
    {
        std::cout << *cit << " ";
    }
    std::cout << std::endl;
    std::cout << std::endl;

    std::cout << "set1 it:                 ";
    for (std::set<Cell>::iterator it = set1.begin(); it != set1.end(); it++)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    std::cout << std::endl;

    std::cout << "set1 const reverse it:   ";
    for (std::set<Cell>::const_reverse_iterator crit = set1.crbegin(); crit != set1.crend(); crit++)
    {
        std::cout << *crit << " ";
    }
    std::cout << std::endl;
    std::cout << std::endl;

    std::cout << "set1 reverse it:         ";
    for (std::set<Cell>::reverse_iterator rit = set1.rbegin(); rit != set1.rend(); rit++)
    {
        std::cout << *rit << " ";
    }
    std::cout << std::endl;
    std::cout << std::endl;
    return 0;
}

output

 

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/130660486