Lab #3: Using a map<string,float> object
for maintaining the information of names and values of variables.
#1. Read the following to understand the
basics of maps in C++:
i.
A map as an associative
container in C++: A map is internally implemented as a search tree for storing
key-and-value pairs. It maps a given key to its associated value. You can
access a map either (i) through the use of iterators and calls to the
appropriate methods or (ii) use it as an associative array using syntax similar
to what you do with ordinary arrays.
ii.
Viewing a map as an
associative array: A very convenient way to use a map is to view it as an
associative array. For an ordinary array X, given a valid non-negative integer key index i, the
ordinary array X
maps it to the value X[i]. (Note that if i is not a valid index, accessing X[i] can
crash your program.) For a map<string,float> object Y as
an associative array, given a valid key string s, the map<string,float> object Y maps it to the float value Y[i]. (Note that if s is not
a key to any values currently stored in the map, accessing Y[s] will create a new key-and-value
pair that map s to something Y[s] accordingly.)
Download and examine the code in mapAsAssociativeArray.cpp
and compile it to see how you can use a map as an associative array to (i)
insert new key-and-value pairs, (ii) examine and print out all of the current
key-and-value pairs, (iii) modify the value of a key-and-value pair, and (iv)
erase a key-and-value pair with a specific key.
iii.
Viewing a map as a
collection of key-and-value pairs: A map is internally a search tree storing
key-and-value pairs, which can be accessed through the use of iterators and calls
to the appropriate methods. Download and examine the code in map.cpp
and compile it to see how you can (i) insert new key-and-value pairs, (ii)
examine and print out all of the current key-and-value pairs, (iii) search for
a key-and-value pair with a five key, (iv) erase a key-and-value pair, and (v)
modify the values of the pairs using a loop. Note that when printing out the contents in the map it is in order
of the keys even if the pairs were inserted into the map in any random order.
This is because the map is internally a search tree that automatically provides
a way to sort out things in order of the keys when we traverse the search tree
through the iterator. Also read Section 10.1 in C++ Primer, which tells you about the pair type used to store key-value pairs in a map.
iv.
Further references: Sections 11.1~11.3 in the 5th
ed. of C++ Primer.
#2. Create a C++ project and do the following
in the main function:
Declare
a map<string,float> object
variableTable first. Use a loop to repeatedly (a) display a menu about the services depicted
in the following, (b) ask the user to select one of the services by entering a
corresponding character, and (c) provide
the selected service accordingly :
·
i (insert): ask the user to provide
the name and the value of a variable and the program should store the information
into variableTable.
·
e (erase): user provides the
name of a variable and the program should delete the variable (and the information
of its value) from variableTable.
·
v (value): user provides the
name of a variable and the program should display the value of the variable if a
variable of that name is present in variableTable. If no variable of that name is present in variableTable, inform the
user there is no such variable.
·
d (display): display the
information of names and values of all the variables stored in variableTable.
#3. Submission: (i) Compress
your entire Lab 3 folder into a zip file and upload it through Biola Canvas. (ii) Carefully
fill out this self-evaluation report (no need
for a peer review) and upload it through Biola Canvas.