我提供了在 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哈希所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)