Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HashMap
//Title of this code #include <iostream> #include <list> using namespace std; template<class T1, class T2> class MyMap { public: struct Node { long key_hash; T2 value; }; private: hash<T1> hashing; list<Node*> nodes; public: MyMap(){}; ~MyMap(){}; T2& operator[](const T1&); }; template<class T1, class T2> T2& MyMap<T1, T2>::operator[](const T1& key) { long key_hash = hashing(key); //typename list<Node*>::iterator it; if (!nodes.empty()) for (auto it = nodes.begin(); it != nodes.end(); ++it) { if ((*it)->key_hash == key_hash) return (*it)->value; } Node *node = new Node(); node->key_hash = key_hash; node->value = 0; nodes.push_back(node); return node->value; } int main() { MyMap<string,int> m; string s = "hello"; m[s] = 10; cout << m[s]; }
run
|
edit
|
history
|
help
0
Spejmer
TemplateRemove
C++ virtual dispose
csv parser
Dar
Address Book
Dar
said
LIS
cppPyBatScoreSOLID