Reputation: 75
I am new to SGI's hash_map as well as to the C++ language, so please bear with me. I am trying to figure out how to initialize a basic hash_map and insert and remove from it.
I have declared the hash_map as such:
Sgi::hash_map<int, Process*> ProcessManager::ListProcesses;
I intend to hash by an int value and store a pointer to an object of class Process.
However, the SGI documentation is very vague and unhelpful. I'm reading through the hash_map file but also not understanding much of it. Could someone show me the proper way to insert and erase from an SGI hash_map?
To be clear: What I'm looking for is a BASIC example to learn from. Please and thank you!
Upvotes: -1
Views: 910
Reputation: 1164
You can do the following.
Sgi::hash_map<int, Process*> ListProcesses;
Process *p1; // Initialize these
Process *p2;
//Insertion
ListProcesses[10] = p1; // new element inserted
ListProcesses[20] = p2; // new element inserted
//Erase
ListProcesses.erase(20); //(20,p2) deleted
As ildjarn commented, you can use std::unordered_map<> instead of the SGI one.
Upvotes: -1
Reputation: 171303
What's wrong with the example in the SGI docs? It clearly shows how to declare a hash_map and how to add values to it.
hash_map<const char*, int, hash<const char*>, eqstr> months;
months["january"] = 31;
months["february"] = 28;
months["march"] = 31;
That variable months
is a hash_map
that uses keys of type const char*
and maps them to values of type int
, and because you don't want to compare the keys by comparing pointer values for equality, it uses a custom equality functor called eqstr
which says whether two const char*
strings have the same contents.
To erase you use the erase
member function, crazy eh.
size_type erase(const key_type& k)
Erases the element whose key is k.
So that would be:
months.erase("march");
The SGI docs are far from vague.
Upvotes: 1