This is our first introduction to template classes. Briefly, templated classes allow you to specify the thing they store/utilize at run time instead of compile time. In some ways, they are similar to how we have been using typedef: There is an abstraction between a particular data structure (linked list) and what that structure actually stores (strings, Movie*, etc).
The pair template class is defined in <utility> to keep pairs of values. The header <utility> also overloads the relational operators ==, <, !=, >, >= and <= , so as to be able to compare pair objects of the same type directly:
Two pair objects are compared equal if the first elements in both objects compare equal to each other and both second elements compare equal to each other – they all have to match.
In inequality comparisons (<, >), only the first element is compared, except if both first elements compare equal to each other, in this case only the second element is taken into consideration for the comparison operation.
#include <iostream>
#include <utility>
#include <string>
using namespace std;
int main () {
pair <string,double> product1 ("tomatoes",3.25);
pair <string,double> product2;
pair <string,double> product3;
product2.first = "lightbulbs"; // type of first is string
product2.second = 0.99; // type of second is double
product3 = make_pair ("shoes",20.0);
cout << "The price of " << product1.first << " is $" << product1.second << "\n";
cout << "The price of " << product2.first << " is $" << product2.second << "\n";
cout << "The price of " << product3.first << " is $" << product3.second << "\n";
return 0;
}
The istringstream class creates a stream from a string. Allowing us to treat it as a stream and read in different values from it. More information about string stream
You would need to include to #include <sstream> to get access to this library and also include the string library to use this method
Here is a link to the C++ FAQ which indicates that string streams are the better way to convert strings to integers. Also has a lot of other good information on C++
ddd might be useful to debug your program and see how the system executes.
Again, to use ddd, add the ‘-g’ flag to the g++ statements of your makefile
After compiling your lab you can open it in ddd with
ddd ./lab04
See the Introduction Notes for a overview of the basics of ddd.
For this project, and most projects in eecs 268, you need to read in an input file from the command line (via argv[1]). To add this line in ddd:
Click on Program and then Run…
Type in the filename in the Run with Arguments pane.
Click “Run”
When you want to watch the value of a variable, this can be easily done inside ddd.
If you just want to see the value of a variable once, you can hover over the name of that variable and it will appear (Remember, your program has to be ‘running’ inside ddd for anything to work)
If you want to watch the value over an extended period of time you can:
Right-click on the variable name
Select Display
Another pane will appear above your source code with a box containing the value of the variable you are displaying. This box can be moved around to allow multiple variables to be viewed.
After a few recursive calls, it would be nice to see how any particular call was entered and the variable values during that recursion. Here the backtrace can be very useful.
Click on the “Status” Dropdown menu
Click on “Backtrace”
You can now move up and down the stack, using the Up and Down buttons. At each level of the backtrace, you can see the variable values by hovering over them or displaying them as described above