-1

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!

user1034868
  • 65
  • 11
  • 2
    Are you aware that C++ has its own version of a hash map -- [`std::unordered_map<>`](http://en.cppreference.com/w/cpp/container/unordered_map)? Why use SGI's? – ildjarn Jun 28 '12 at 22:30

2 Answers2

1

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.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • So in this: hash_map, eqstr> months; Months is the name of the hashtable? And we insert by doing months[whatever we want to insert]? and then the 31 is the number being hashed by (the key), or is it what is being stored as data? – user1034868 Jun 28 '12 at 22:31
  • 3
    Yes. Seems like you're struggling with basic C++ grammar, not the SGI docs or `hash_map` in particular. – Jonathan Wakely Jun 28 '12 at 22:33
-1

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.

panickal
  • 1,154
  • 9
  • 13