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

std::pair<iterator,bool> insert( const value_type& value );

(1)(C++11 起)

std::pair<iterator,bool> insert( value_type&& value );

(2)(C++11 起)

iterator insert( const_iterator hint, const value_type& value );

(3)(C++11 起)

iterator insert( const_iterator hint, value_type&& value );

(4)(C++11 起)

template< class InputIt >
void insert( InputIt first, InputIt last );

(5)(C++11 起)

void insert( std::initializer_list<value_type> ilist );

(6)(C++11 起)

insert_return_type insert(node_type&& nh);

(7)(C++17 起)

iterator insert(const_iterator hint, node_type&& nh);

(8)(C++17 起)

 

若容器尚未含有带等价关键的元素,则插入元素到容器中。

1-2) 插入 value

3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。

5) 插入来自范围 [first, last) 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

7) 若 nh 是空的结点把柄,则不做任何事。否则插入 nh 所占有的元素到容器,若容器尚未含有拥有等价于 nh.key() 的关键的元素。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

8) 若 nh 是空的结点把柄,则不做任何事并返回尾迭代器。否则,插入 nh 所占有的元素到容器,若容器尚未含有拥有等价于 nh.key() 的关键的元素,并返回指向拥有等于 nh.key() 的关键的元素的迭代器(无关乎插入成功还是失败)。若插入成功,则从 nh 移动,否则它保持该元素的所有权。元素被插入到尽可能接近 hint 的位置。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

若因插入发生重哈希,则所有迭代器都被非法化。否则迭代器不受影响。引用不受影响。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。若插入成功,则在结点把柄保有元素时获得的指向该元素的指针和引用被非法化,而在提取前获得的指向元素的指针和引用变得合法。 (C++17 起)

参数

hint-迭代器,用作插入内容位置的建议
value-要插入的元素值
first, last-要插入的元素范围
ilist-插入值来源的 initializer_list
nh-兼容的结点把柄
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

返回值

1-2) 返回由指向被插入元素(或阻止插入的元素)的迭代器和指代插入是否发生的 bool 组成的 pair 。

3-4) 返回指向被插入元素,或阻止插入的元素的迭代器。

5-6) (无)

7) 返回 insert_return_type ,其成员初始化如下:若 nh 为空,则 insertedfalseposition 为 end() ,而 node 为空。否则发生插入, insertedtrueposition 指向被插入元素,而 node 为空。若插入失败,则 insertedfalsenode 拥有 nh 的先前值,而 position 指向拥有等价于 nh.key() 的关键的元素。

8) 若 nh 为空则为尾迭代器,若插入发生则为指向被插入元素的迭代器,而若插入失败则为指向拥有等价于 nh.key() 的关键的元素的迭代器。

异常

1-4) 若任何操作抛出异常,则插入无效果。

本节未完成
原因:情况 5-8

复杂度

1-4) 平均情况: O(1) ,最坏情况 O(size())

5-6) 平均情况: O(N) ,其中 N 是要插入的元素数。最坏情况: O(N*size()+N)

7-8) 平均情况: O(1) ,最坏情况 O(size())

注意

有提示插入 (3,4) 不返回 bool ,这是为了与顺序容器上的定位插入,如 std::vector::insert 签名兼容。这使得可以创建泛型插入器,例如 std::inserter 。检查有提示插入是否成功的一种方式是比较插入前后的 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;
    for (size_t index = 0; index < 5; index++)
    {
        //若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。
        std::pair<std::unordered_set<Cell, CHash, CEqual>::iterator, bool> iit
            = unordered_set1.insert(generate());
        std::cout << "unordered_set1 insert : " << *iit.first << "   " << iit.second << std::endl;
    }
    std::cout << "unordered_set1:   ";
    std::copy(unordered_set1.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set2;
    for (size_t index = 0; index < 5; index++)
    {
        Cell cell = generate();
        //若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。移动语义
        std::pair<std::unordered_set<Cell, CHash, CEqual>::iterator, bool> iit
            = unordered_set2.insert(std::move(cell));
        std::cout << "unordered_set2 insert : " << *iit.first << "   " << iit.second << std::endl;
    }
    std::cout << "unordered_set2:   ";
    std::copy(unordered_set2.begin(), unordered_set2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set3;
    for (size_t index = 0; index < 5; index++)
    {
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。
        std::unordered_set<Cell, CHash, CEqual>::iterator iit
            = unordered_set3.insert(unordered_set3.cbegin(), generate());
        std::cout << "unordered_set3 insert : " << *iit  << std::endl;
    }
    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;
    for (size_t index = 0; index < 5; index++)
    {
        Cell cell = generate();
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。移动语义
        std::unordered_set<Cell, CHash, CEqual>::iterator iit
            = unordered_set4.insert(unordered_set4.cbegin(), std::move(cell));
        std::cout << "unordered_set4 insert : " << *iit  << std::endl;
    }
    std::cout << "unordered_set4:   ";
    std::copy(unordered_set4.begin(), unordered_set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::vector<Cell> vector1(6);
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:          ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::unordered_set<Cell, CHash, CEqual> unordered_set5;
    //5) 插入来自范围 [first, last) 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_set5.insert(vector1.begin(), vector1.end());
    std::cout << "unordered_set5:   ";
    std::copy(unordered_set5.begin(), unordered_set5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_set<Cell, CHash, CEqual> unordered_set6;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_set6.insert({generate(), generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_set6:   ";
    std::copy(unordered_set6.begin(), unordered_set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

输出

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值