前言 前面的文章我们学习了unordered_set和unordered_map的使用以及哈希表,并且我们提到了unordered_set和unordered_map的底层结构其实就是哈希表。 那这篇文章我们就对之前我们实现的哈希表(拉链法实现的那个)进行一个改造,并用它模拟实现一下unordered_set和unordered_map。 那在模拟实现之前要声明一下: 我们这里的模...
second << endl; } } 4. Test.cpp #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; #include <vector> #include "HashTable.h" #include "UnorderedSet.h" #include "UnorderedMap.h" #include <string> int main() { //UnorderedSet_test1(); UnorderedMap_test1();...
三、完整代码 HashTable.cpp文件 #pragma oncenamespace hash_bucket{template<class K>struct DefaultHashFunc{size_t operator()(const K& key){return (size_t)key;}};template<>struct DefaultHashFunc<string>{size_t operator()(const string& str){size_t hashi = 0;for (auto ch : str){hashi =...
());constint&ref1=*(a1.begin());constint&ref2=*(a2.begin());std::cout<<a1<<a2<<*it1<<' '<<*it2<<' '<<ref1<<' '<<ref2<<'\n';a1.swap(a2);std::cout<<a1<<a2<<*it1<<' '<<*it2<<' '<<ref1<<' '<<ref2<<'\n';// 注意交换前指代一个容器中的元素的每个...
// std__unordered_set__unordered_set_bucket.cpp // compile with: /EHsc #include <unordered_set> #include <iostream> typedef std::unordered_set<char> Myset; int main() { Myset c1; c1.insert('a'); c1.insert('b'); c1.insert('c'); // display contents "[c] [b] [a] " fo...
简介:unordered_map和unordered_set的源码模拟实现 在源码中,这两个STL容器都是共用一个框架的所以,我们就用一个框架实现两个 容器,代码细节都在注释上 HashTable.h #pragma once#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>#include <vector>#include <string>#include <utility>#include <algorithm>#...
Complexity Constant on average, worst case linear in the size of the container. Notes Feature-testmacroValueStdFeature __cpp_lib_generic_unordered_lookup201811L(C++20)Heterogeneous comparison lookup inunordered associative containers; overloads(3,4) ...
class Ref,class Ptr, class KeyOfT, class Hash > struct HTIterator { typedef HashNode<T> Node; Node* _node; typedef HTIterator<K, T, Ref, Ptr,KeyOfT, Hash> Self; //哈希表的指针 const HashTable< K, T, KeyOfT, Hash>* _pht;//向上查找哈希表 HTIterator(Node* node, const HashTable...
test.cpp Link: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"D:\code\unordered_xxxVSxxx\Release\unordered_xxxVSxxx.exe" /INCREMENTAL:NO /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32...
ref)) printf(" %g", *j.ref); c_drop(Vec2D, &vec_a, &vec_b, &clone); // Free all 9 vectors. }This example uses four different container types:[ Run this code ]#include <stdio.h> #define i_type hset_int, int #include ...