C++STL哈希,Python哈希,Java哈希

C++STL哈希,Python哈希,Java哈希,第1张

概述哈希表-用法我提供了在Java,C++和Python中使用哈希集的示例。如果你不熟悉哈希集的用法,那么通过这一示例将会很有帮助。//C++#include<iostream>#include<unordered_set>//0.includethelibraryintmain(){//1.initializeahashs 哈希表 - 用法

我提供了在 Java,C++ 和 Python 中使用哈希集的示例。 如果你不熟悉哈希集的用法,那么通过这一示例将会很有帮助。

//C++#include <iostream>#include <unordered_set>                // 0. include the libraryint main() {    // 1. initialize a hash set    unordered_set<int> hashset;       // 2. insert a new key    hashset.insert(3);    hashset.insert(2);    hashset.insert(1);    // 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 hash set is: " << hashset.size() << endl;     // 6. iterate the hash set    for (auto it = hashset.begin(); it != hashset.end(); ++it) {        cout << (*it) << " ";    }    cout << "are 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;    }}
//java// "static voID main" must be defined in a public class.public class Main {    public static voID main(String[] args) {        // 1. initialize the hash set        Set<Integer> hashSet = new HashSet<>();             // 2. add a new key        hashSet.add(3);        hashSet.add(2);        hashSet.add(1);        // 3. remove the key        hashSet.remove(2);                // 4. check if the key is in the hash set        if (!hashSet.contains(2)) {            System.out.println("Key 2 is not in the hash set.");        }        // 5. get the size of the hash set        System.out.println("The size of has set is: " + hashSet.size());             // 6. iterate the hash set        for (Integer i : hashSet) {            System.out.print(i + " ");        }        System.out.println("are in the hash set.");        // 7. clear the hash set        hashSet.clear();        // 8. check if the hash set is empty        if (hashSet.isEmpty()) {            System.out.println("hash set is empty Now!");        }    }}
#python# 1. initialize the hash sethashset = set() # 2. add a new keyhashset.add(3)hashset.add(2)hashset.add(1)# 3. remove a keyhashset.remove(2)# 4. check if the key is in the hash setif (2 not in hashset):    print("Key 2 is not in the hash set.")# 5. get the size of the hash setprint("Size of hashset is:", len(hashset)) # 6. iterate the hash setfor x in hashset:    print(x, end=" ")print("are in the hash set.")# 7. clear the hash sethashset.clear()                         print("Size of hashset:", len(hashset))
总结

以上是内存溢出为你收集整理的C++STL哈希,Python哈希,Java哈希全部内容,希望文章能够帮你解决C++STL哈希,Python哈希,Java哈希所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1188491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存