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

hasher hash_function() const;

(C++11 起)

 返回对关键哈希的函数。

参数

(无)

返回值

哈希函数。

复杂度

常数。

返回用于比较键的相等性的函数

std::unordered_set<Key,Hash,KeyEqual,Allocator>::key_eq

key_equal key_eq() const;

(C++11 起)

返回比较关键相等性的函数。

参数

(无)

返回值

关键比较函数。

复杂度

常数。

 

非成员函数

比较 unordered_set 中的值
 

operator==,!=(std::unordered_set)
template< class Key, class Hash, class KeyEqual, class Allocator >

bool operator==( const unordered_set<Key,Hash,KeyEqual,Allocator>& lhs,

                 const unordered_set<Key,Hash,KeyEqual,Allocator>& rhs );
(1)
template< class Key, class Hash, class KeyEqual, class Allocator >

bool operator!=( const unordered_set<Key,Hash,KeyEqual,Allocator>& lhs,

                 const unordered_set<Key,Hash,KeyEqual,Allocator>& rhs );
(2)

 比较二个无序容器的内容。

若下列条件成立则二个无序容器 lhsrhs 相等:

  • lhs.size() == rhs.size()
  • 从 lhs.equal_range(lhs_eq1) 获得的每组等价元素 [lhs_eq1, lhs_eq2) 拥有在另一容器中从 rhs.equal_range(rhs_eq1) 获得的对应等价元素组 [rhs_eq1, rhs_eq2) ,且它们拥有下列属性:
  • std::distance(lhs_eq1, lhs_eq2) == std::distance(rhs_eq1, rhs_eq2) 。
  • std::is_permutation(lhs_eq1, lhs_eq2, rhs_eq1) == true 。

Key可相等比较 (EqualityComparable) 则行为未定义。

hash_function()key_eq() (C++20 前)key_eq() (C++20 起) 在 lhsrhs 上拥有不相同的行为,或若 Key 的 operator== 不是对于 key_eq() 所引入的等价关键组的细分(即若用 operator== 比较相等的二个元素落入不同划分),则行为未定义。

参数

lhs, rhs-要比较的无序容器

返回值

1) 若容器内容相等则为 true ,否则为 false 。

2) 若容器内容不相等则为 true ,否则为 false 。

复杂度

调用 value_type 上的 operator== 、调用 key_eq 所返回的谓词,及调用 hash_function 所返回的哈希器的次数,平均情况下与 N 成比例,最坏情况下与 N2 成比例,其中 N 是容器大小。

 

特化 std::swap 算法

std::swap(std::unordered_set)
template< class Key, class Hash, class KeyEqual, class Alloc >

void swap( unordered_set<Key,Hash,KeyEqual,Alloc>& lhs,

           unordered_set<Key,Hash,KeyEqual,Alloc>& rhs );
(C++11 起)
(C++17 前)
template< class Key, class Hash, class KeyEqual, class Alloc >

void swap( unordered_set<Key,Hash,KeyEqual,Alloc>& lhs,

           unordered_set<Key,Hash,KeyEqual,Alloc>& rhs ) noexcept(/* see below */);
(C++17 起)

 为 std::unordered_set 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

参数

lhs, rhs-要交换内容的容器

返回值

(无)

复杂度

常数。

异常

noexcept 规定:  

noexcept(noexcept(lhs.swap(rhs)))

(C++17 起)

调用示例 

#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 + 110;
        Cell cell{n, n};
        return cell;
    };

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

    std::cout << "unordered_set1:   ";
    std::copy(unordered_set1.begin(), unordered_set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

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

    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::cout << "unordered_set1 == unordered_set2: " << (unordered_set1 == unordered_set2) << std::endl;
    std::cout << "unordered_set1 != unordered_set2: " << (unordered_set1 != unordered_set2) << std::endl;
    std::cout << std::endl;


    //为 std::unordered_set 特化 std::swap 算法。交换 lhs 与 rhs 的内容。调用 lhs.swap(rhs) 。
    std::swap(unordered_set1, unordered_set2);
    std::cout << "after swap:   " << 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 << "unordered_set2:   ";
    std::copy(unordered_set2.begin(), unordered_set2.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、付费专栏及课程。

余额充值