Notes:
In class we discussed the Standard Template Library, and all the great things it provides for us. Specifically you were introduced to the set and vector ADT's, and the sort algorithm. Today you'll add another tool to your container toolkit: the map
. In addition to the overview provided in this lab, you should do some research of your own: http://www.cplusplus.com/reference/map/map/
The map is one of the most powerful container classes in the standard library. A map stores pairs of objects called key-value pairs. You search for a key in the map and, if it's found, the value paired with that key is returned. Usually it's the value you were interested in. The map class makes use of another templated class named pair
, which is defined in the utility
library. A pair has data members first
and second
, which may be of different types. Here is an example of a simple program using just about all you need to know about the class pair
:
sample.cpp
#include <iostream>
#include <utility>
using namespace std;
int main() {
pair<char,int> A, B;
cout << "Enter a letter and a number: ";
cin >> A.first >> A.second;
cout << "Enter a letter and a number: ";
cin >> B.first >> B.second;
if (A.first > B.first)
cout << "The larger letter is from the first" << endl;
else
cout << "The larger letter is from the second" << endl;
if (A.second > B.second)
cout << "The larger number is from the first" << endl;
else
cout << "The larger number is from the second" << endl;
return 0;
}
Compiling and Running
$ g++ -o sample sample.cpp $ ./sample Enter a letter and a number: D5 Enter a letter and a number: X4 The larger letter is from the second The larger number is from the first $
Do some research of the map class. Download part1.txt and answer the following questions in the file:
If you type "c++ pair" into google you'll get more information on the pair
class. When you declare an object of type map
, you specify types for the key and value of the map. For example if you say
map<string,double> M;
that says that the map stores values of type double
with keys of type string
. So you lookup double values using a string keys. Key-value pairs are added to the map with the insert function. Example:
map<string,double> M;
pair<string,double> PG("gold",14.21);
pair<string,double> PS("silver",8.00);
pair<string,double> PB("bronze",2.25);
M.insert(PG);
M.insert(PS);
M.insert(PB);
Now, given a key (a string in this case), you want to find the matching key-value pair. The find function takes a key as an argument and returns an iterator to a key-value pair with matching key. If no matching key is found, the iterator returned is equal to the M.end()
for map M
. Example:
map<string,double> M;
pair<string,double> PG("gold",14.21);
pair<string,double> PS("silver",8.00);
pair<string,double> PB("bronze",2.25);
M.insert(PG);
M.insert(PS);
M.insert(PB);
// The following should print out "2.25"
map<string,double>::iterator itr = M.find("bronze");
if (itr != M.end())
cout << (*itr).second << endl;
else
cout << "No match found!" << endl;
Your task:
map1.cpp
In this part we'll really use the power of maps. The file rates.txt contains exhange rates for March 25th, 2008. In particular, it gives a currency name, the worth of $1.00 in that currency, and a shorthand name for the currency.
Your task is to write a program called map2 that reads in those currency names (ignore the shorthand names) and exchange rates and uses the information to do currency conversion for the user. Here's an example run of the program:
$ ./map2 Enter <amt> <curr1> <curr2>: 4 Euro JapaneseYen 4 Euro is 620.501 JapaneseYen Enter <amt> <curr1> <curr2>: 3 AustrianSchilling UnitedStatesDollar Currency not found! Enter <amt> <curr1> <curr2>: 7 UnitedStatesDollar Euro 7 UnitedStatesDollar is 4.54723 Euro Enter <amt> <curr1> <curr2>:
To help you along with this, your first real map
programming experience, use the below template. You need to complete the three sections as indicated by the comment blocks:
map2.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main() {
// Read & store currency names and exchange rates
ifstream IN("rates.txt");
/* 1. create new map object named Val that uses a string key and double value */
string currency, shorthand;
double valOfDollar;
while(IN >> currency >> valOfDollar >> shorthand){
pair<string,double> KV(currency,valOfDollar);
/* 2. add the new pair to your map */
}
// Read amount, currency1 & currency2 and convert
string curr1, curr2;
double amt;
while(cout << "Enter <amt> <curr1> <curr2>: ", cin >> amt >> curr1 >> curr2) {
map<string,double>::iterator itr1 = Val.find(curr1);
map<string,double>::iterator itr2 = Val.find(curr2);
/* 3. What do we do now? Add code to determine whether both currencies were
* found and, if so, what the value of "amt" in "curr1" is in "curr2".
* Print results!
*/
}
return 0;
}
Submit your source code via the the submisison website: submit.cs.usna.edu.
Specifically:
part1.txt
map1.cpp
map2.cpp
.o
, or provided/unaltered files (i.e. rates.txt)!
~/bin/submit -c=SI221 -p=Lab09 part1.txt map1.cpp map2.cpp