美文网首页
c++ 使用unordered_set实现hashset

c++ 使用unordered_set实现hashset

作者: TFprime | 来源:发表于2019-03-05 19:12 被阅读0次

使用unordered_set库来构造hashset

unordered_set 官方文档介绍

Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.
In an unordered_set, the value of an element is at the same time its key, that identifies it uniquely. Keys are immutable, therefore, the elements in an unordered_set cannot be modified once in the container - they can be inserted and removed, though.
Internally, the elements in the unordered_set are not sorted in any particular order, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their values (with a constant average time complexity on average).
unordered_set containers are faster than set containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.
Iterators in the container are at least forward iterators.
简单来说,unordered_key非常的高效,使用值本身作为键值

Example

#include<iostream>
#include<unordered_set>
using namespace std;

int main() {
    // 1. initialize a hash set
    unordered_set<int> hashset;
    // 2. insert a new key
    hashset.insert(1);
    hashset.insert(2);
    hashset.insert(3);
    // 3. delete a key
    hashset.erase(2);
    // 4. check if the key is in the hash set
    if (hashset.count(2) <= 0) {
        cout << "Key 2 is not in the hash set." << endl;
    }
    // 5. get the size of the hash set
    cout << "The size of the hash set is " << hashset.size() << endl;
    // 6. iterate the hash set
    for (auto it = hashset.begin(); it != hashset.end(); it++) {
        cout << (*it) << " ";
    }
    cout << "is in the hash set." << endl;
    // 7. clear the hash set
    hashset.clear();
    // 8. check if the hash set is empty
    if (hashset.empty()) {
        cout << "hash set is empty now!" << endl;
    }
    
    return 0;
}

相关文章

  • c++ 使用unordered_set实现hashset

    使用unordered_set库来构造hashset unordered_set 官方文档介绍 Unordered...

  • Java 编程技巧之数据结构

    使用HashSet判断主键是否存在 HashSet 实现 Set 接口,由哈希表(实际上是 HashMap )实现...

  • STL的unordered_set(原hash_set)使用和分

    unordered_set的成员函数 参考C++手册,可看到每个函数的参数返回值解释和使用举例:https://z...

  • java8中hashset源码分析

    分析大纲 hashset实现原理 hashset代码分析 1. hashset实现原理 hashset存储无序,不...

  • Set解析

    概览 上图为HashSet,LinkedSet,TreeSet对比 HashSet 可见HashSet其实就是实现...

  • java集合(二)——集合 Set

    一、HashSet类HashSet简介 HashSet是Set接口的典型实现,实现了Set接口中的所有方法,并没有...

  • HashMap 和 HashSet 区别

    看过 HashSet 源码的人就应该知道:HashSet 底层就是基于 HashMap 实现的。(HashSet ...

  • HashSet源码分析

    HashSet HashSet概述### HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)...

  • 基础:集合类

    集合类的继承关系 常用的几个实现类HashSet:没有排序的Set实现类,平时可以使用 TreeSet:实现类So...

  • HashSet和LinkedHashSet

    HashSet HashSet底层是基于HashMap来实现,所以也不是线程安全的 HashSet中不能插入相同的...

网友评论

      本文标题:c++ 使用unordered_set实现hashset

      本文链接:https://www.haomeiwen.com/subject/ougouqtx.html