Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Baaad1
//Title of this code #include <iostream> #include <list> #include <memory> using namespace std; template<class T1, class T2> class MyMap { public: struct Node { long key_hash; T2 value; Node(long key_hash) { this->key_hash = long(key_hash); } Node(long key_hash, T2 value) { this->key_hash = long(key_hash); this->value = T2(value); } Node(const Node& n) { *this = Node(n.key_hash, n.value); } }; typedef shared_ptr<Node> node_ptr; private: hash<T1> hashing; list<node_ptr> nodes; void print(list<node_ptr> l) { for (auto it = l.begin(); it != l.end(); ++it) cout << (*it)->value << " "; cout << endl; } public: MyMap(){} ~MyMap(){} //MyMap(const MyMap&); MyMap& operator=(const MyMap&); T2& operator[](const T1&); }; template<class T1, class T2> MyMap<T1, T2>& MyMap<T1, T2>::operator=(const MyMap& m) { if (this == &m) return *this; nodes = m.nodes; list<node_ptr> new_nodes; for (auto it = m.nodes.begin(); it != m.nodes.end(); ++it) { node_ptr node = node_ptr(new Node(**it)); new_nodes.push_back(node); } this->nodes = new_nodes; return *this; } 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_ptr node = node_ptr(new Node(key_hash)); nodes.push_back(node); return node->value; } int main() { MyMap<int,int> m; MyMap<int,int> n; m[0] = 10; n[0] = 20; m = n; m[0] = 30; n[0] = 40; cout << n[0] << " " << m[0] << endl; }
run
|
edit
|
history
|
help
0
Vector+Memory_Adv_C++_Tutorial
thread
C++ Array printing
Microsoft Question - MaxEmployeeAttendence (original question)
Insertion Sort
Stream9
override
Test
TwoANOVA
runtime template mode processor