The purpose of this lab is to gain some familiarity with an STL Container, and the use of an external iterator. For this lab you will NOT be creating any classes of your own. In fact you can do the whole lab in the main, although I think that you will see that it is highly advisable to have at least one non-member function since there is a task that is repeated.
Begin by copying the file names.txt to your working directory. This file has a long list of first names, all taken from some of my classes. Briefly open it and look at it, or just skim through it with more names.txt
- At the top of your program #include
as well as iostream, string, and fstream. Declare a multiset that is capable of holding strings.
- Fill the multiset with all the names in the file. (Since there are no spaces in the names you can use >> or getline.)
- Now declare an iterator that is appropriate for traversing the multiset. Set it to the beginning of the multiset full of names, and walk through the list, outputting each name it finds. (There are a lot of them, so you may want to separate them with spaces instead of putting one on each line.)
- Run and test this program. Your name should be somewhere in the list. (Note that the names are now in alphabetical order.)
Here is the place where you want to pass your STL container to a non-member function, since we are doing something that is basically the same three times in a row. If you do you should pass the container by const and reference, since it is large. You will probably want one additional parameter.
- Declare two iterators.
- Move one of them through the list to the first name that begins with ‘M’. Since you know it is returning a string, you will know that (it).at(0) will return the first character in that string.
- Now set the second iterator equal to the first. Use it to count how many M names there are in the list. Print this number out.
- Using the second iterator, print out all the M names in reverse alphabetical order. (Remember that these are bidirectional iterators, so you –it works to go backwards.)
- Now do the above three steps for all the ‘C’ names and then for all the ‘Q’ names.
If I had asked for ‘Y’ (I didn’t) the output would look like:
There are 3 Y names in the list
They are:Yujia Yuanhang Yingjie
This program has no user interaction. When you have it working, simply start a script file, run the program once, and close the script file.
Submit your source code and the script file to Blackboard.