Software Design Using C++STL: MapsWhat is a Map?The map container class provides the programmer with a convenient way to store and retrieve data pairs consisting of a key and an associated value. Each key is associated with one value. (If you want to associate a key with more than one value, look up the multimap container class.)
This is similar in some ways to what we have done elsewhere in these Web
pages with a list-based table
and a binary search
tree-based table. It is also similar to our
B-tree-based table,
although that was stored on disk whereas a map is stored in main memory.
Another difference is that our tables were unordered; they provided no
way to sequence through the data in key order. Maps are ordered, so that
you can step through the data if you like.
Maps are guaranteed to be efficient. In general, inserting a new item into a map is supposed to take O(lg(n)) time, where n is the number of items in the map. However, if you want to insert at a specific location (specified by an iterator), it takes on average O(n) time. You can also sequence through the items in a map using an iterator, in ascending order by key, probably in an efficient manner as well, though this author knows of no specific time guarantee. Note, also, that we don't necessarily know what underlying data structure is used by a map. You need to include themap header file to use maps. There
are a lot of available class functions. Some of the most basic ones are
outlined below. You cannot use random-access iterators with
the map class, but iterators can be used to traverse a map
in either ascending or descending key order.
ExampleA map is ideal for storing dictionary-like data, a symbol table, etc. Our example program stores a very short dictionary of words and their meanings in a map and then allows the user to interactively look up words, in each case reporting their meanings if a match is found. string , which is used to contain one "line" of
text data (in this case, a word or its meaning). Note that the chosen data type
must have a < operator for the class used for the keys. This is
because a map is ordered according to the keys. Also, the class used for the
associated values has to have a
default constructor.
Here, the class is again string, so the default constructor is automatically provided.
To create an empty
Note that the same notation is used in specifying the type for the map when passing it as a parameter to the helping functions. For example, here is the header for one of the two helping functions:
Inside the code for this function you see the two basic methods for
creating a data pair for inserting into the map. The first one was
created by using the
The code for the
Note that the As an exercise you might want to modify the above program in various ways. For example, you might want to modify the program so that it reads the word and definition data from a suitable text file. That would certainly be more reasonable for a longer list of words. For example, you might want to use the text file btree.txt that was used in an earlier program. (If this file is too long to handle, you might cut it down in size.)
In general, the
The Related Items |