map and arrays


How can I write a map in c++?

From what I've heard arrays aren't really good for super large elements since memory becomes an issue. But again how would "maps" be created? Is it a matter of writing a linked lists?
closed account (D80DSL3A)
I have written a rudimentary map container as an exercise. I implemented mine as an ordered doubly linked list. Each node in the list stores a key-value pair, so it's a good exercise in writing a template class with 2 template parameters.

A new pair gets inserted in the list only if that key does not already exist in the list.
The "real" (STL) map class insert() returns a bool-iterator pair to indicate if the insertion was made and to give an iterator to the element where the pair is stored. My insert() returns a bool-node* pair instead. You may want to write a find() and a remove() as well.
What do you mean by "super large elements"?

A map is good when you need to relate keys to values, so could be used as a sparse array. e.g. where you need elements 12, 123334, 8939393933, ... and it would be a waste of space to implement the other elements.

If you have a large number of elements you need to store in a map, you should prob be using a hash table mechanism, like the STL unordered_map (some versions of STL, the pre- or non-standard name hash_map is used for this class).

Or do you mean that the individual elements are each large?
I think "super large elements" refers to a large amount of continuos memory which with a map the memory is non-continuos.
Topic archived. No new replies allowed.