天天看點

c++11 标準模闆(STL)(std::unordered_set)(八)修改器

定義于頭檔案 <unordered_set>      

template<

    class Key,

    class Hash = std::hash<Key>,

    class KeyEqual = std::equal_to<Key>,

    class Allocator = std::allocator<Key>

> class unordered_set;

(1) (C++11 起)

namespace pmr {

    template <class Key,

              class Hash = std::hash<Key>,

              class Pred = std::equal_to<Key>>

    using unordered_set = std::unordered_set<Key, Hash, Pred,

                                             std::pmr::polymorphic_allocator<Key>>;

}

(2) (C++17 起)

unordered_set is 是含有 Key 類型唯一對象集合的關聯容器。搜尋、插入和移除擁有平均常數時間複雜度。

在内部,元素并不以任何特别順序排序,而是組織進桶中。元素被放進哪個桶完全依賴其值的哈希。這允許對單獨元素的快速通路,因為哈希一旦,就準确指代元素被放入的桶。

不可修改容器元素(即使通過非 const 疊代器),因為修改可能更改元素的哈希,并破壞容器。

修改器

清除内容

std::unordered_set<Key,Hash,KeyEqual,Allocator>::clear
           
void clear() noexcept; (C++11 起)

從容器擦除所有元素。此調用後 size() 傳回零。

非法化任何指代所含元素的引用、指針或疊代器。可能亦非法化尾後疊代器。

參數

(無)

傳回值

(無)

複雜度

與容器大小,即元素數成線性。

缺陷報告

下列更改行為的缺陷報告追溯地應用于以前出版的 C++ 标準。

擦除元素

std::unordered_set<Key,Hash,KeyEqual,Allocator>::erase
           
iterator erase( const_iterator pos ); (1) (C++11 起)
iterator erase( const_iterator first, const_iterator last ); (2) (C++11 起)
size_type erase( const key_type& key ); (3) (C++11 起)

從容器移除指定的元素。

1) 移除位于

pos

的元素。

2) 移除範圍

[first; last)

中的元素,它必須是 *this 中的合法範圍。

3) 移除關鍵等于

key

的元素(若存在一個)。

到被擦除元素的引用和疊代器被非法化。其他疊代器和引用不被非法化。

疊代器

pos

必須合法且可解引用。進而 end() 疊代器(合法,但不可解引用)不能用作

pos

所用的值。

保留未被擦除的元素順序(這使得可能在疊代通過容器時擦除單獨的元素)。 (C++14 起)

參數

pos - 指向要移除的元素的疊代器
first, last - 要移除的元素範圍
key - 要移除的元素關鍵值

傳回值

1-2) 後随最後被移除的元素的疊代器。

3) 被移除的元素數。

異常

1,2) (無)

3) 任何

Compare

對象所抛的異常

複雜度

給定

unordered_set

的執行個體

c

1) 平均情況:常數,最壞情況: c.size()

2) 平均情況: std::distance(first, last) ,最壞情況: c.size()

3) 平均情況: c.count(key) ,最壞情況: c.size()

調用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_set>
#include <time.h>

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

struct myCompare
{
    bool operator()(const int &a, const int &b)
    {
        return a < b;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{
    os << pCell.first << "-" << pCell.second;
    return os;
}

struct CHash
{
    size_t operator()(const Cell& cell) const
    {
        size_t thash = std::hash<int>()(cell.x) | std::hash<int>()(cell.y);
//        std::cout << "CHash: " << thash << std::endl;
        return thash;
    }
};

struct CEqual
{
    bool operator()(const Cell &a, const Cell &b) const
    {
        return a.x == b.x && a.y == b.y;
    }
};

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::unordered_set<Cell, CHash, CEqual> unordered_set1;
    //6) 插入來自 initializer_list ilist 的元素。若範圍中的多個元素擁有比較等價的關鍵,則插入哪個元素是未指定的
    unordered_set1.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set1:   ";
    std::copy(unordered_set1.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "unordered_set1 empty :    " << unordered_set1.empty() << std::endl;
    //從容器擦除所有元素。此調用後 size() 傳回零。
    unordered_set1.clear();
    std::cout << "unordered_set1 empty :    " << unordered_set1.empty() << std::endl;
    std::cout << "unordered_set1 size  :    " << unordered_set1.size() << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set2;
    //6) 插入來自 initializer_list ilist 的元素。若範圍中的多個元素擁有比較等價的關鍵,則插入哪個元素是未指定的
    unordered_set2.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set2:   ";
    std::copy(unordered_set2.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    size_t usize = unordered_set2.size();
    for (size_t index = 0; index < usize - 1; index++)
    {
        //從容器移除指定的元素。1) 移除位于 pos 的元素。
        std::unordered_set<Cell, CHash, CEqual>::iterator rit = unordered_set2.erase(unordered_set2.cbegin());
        std::cout << "unordered_set2 erase iterator :   " << *rit << std::endl;
    }
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set3;
    //6) 插入來自 initializer_list ilist 的元素。若範圍中的多個元素擁有比較等價的關鍵,則插入哪個元素是未指定的
    unordered_set3.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set3:   ";
    std::copy(unordered_set3.begin(), unordered_set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::unordered_set<Cell, CHash, CEqual>::const_iterator bcit = unordered_set3.cbegin();
    bcit++;
    std::unordered_set<Cell, CHash, CEqual>::const_iterator ecit = bcit;
    ecit++, ecit++;
    unordered_set3.erase(bcit, ecit);
    std::cout << "unordered_set3:   ";
    std::copy(unordered_set3.begin(), unordered_set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set4;
    //6) 插入來自 initializer_list ilist 的元素。若範圍中的多個元素擁有比較等價的關鍵,則插入哪個元素是未指定的
    unordered_set4.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set4:   ";
    std::copy(unordered_set4.begin(), unordered_set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    usize = unordered_set4.size();
    for (size_t index = 0; index < usize - 1; index++)
    {
        //從容器移除指定的元素。3) 移除關鍵等于 key 的元素(若存在一個)。
        size_t esize = unordered_set4.erase(*unordered_set4.cbegin());
        std::cout << "unordered_set4 erase iterator esize:   " << esize << std::endl;
    }
    std::cout << std::endl;

    return 0;
}
           

輸出

c++11 标準模闆(STL)(std::unordered_set)(八)修改器

繼續閱讀