CS 10B Programming Concepts and Methodologies 1

Project 24.1

Rewrite this high scores program using an STL vector instead of an array. (Note, you will be using an STL vector, not the MyVector class developed in lesson 19.)

Clarifications and Additional Requirements:

  • Since you are working with code written by someone else, no documentation is required for this project.

  • Your program must use the following five function headers exactly as they appear here:

    void getVectorSize(int& size);
    void readData(vector<Highscore>& scores);
    void sortData(vector<Highscore>& scores);
    vector<Highscore>::iterator findLocationOfLargest(
                                    const vector<Highscore>::iterator startingLocation,
                                    const vector<Highscore>::iterator endingLocation);
    void displayData(const vector<Highscore>& scores);
    
    
  • The size parameter from the given code won't be needed now, since a vector knows its own size.

  • Notice that the findLocationOfLargest() function does not need the vector itself as a parameter, since you can access the vector using the provided iterator parameters.

  • The name field in the struct must still be a c-string

  • The focus of this assignment is to use iterators. You must use iterators wherever possible to access the vector. As a result, you must not use square brackets, the push_back() function, the at() function, etc. Also, the word "index" shouldn't appear in your code anywhere. You won't get full credit if you miss an opportunity to use iterators.

  • You should still ask the user to enter the number of scores there will be, and then you should create a vector with the required capacity. You can do this by using the vector class's constructor that creates a vector with the capacity indicated by its parameter. For example, to create a vector of size 100, use this:

    vector<sometype> myExampleVector(100);

    It is possible to write our program without bothering to indicate a size if we simply use push_back() to add each high score struct to the vector. We aren't doing it that way, because I want you to practice using iterators.

  • You could sort the scores by simply calling the STL sort() algorithm. I would suggest that you try this out because it's something you should know, but for your submitted program you are required to sort the vector as it is done in the given code, except using iterators to access the items in the vector. You won't get credit for the assignment if you use the STL sort() function.

  • In your displayData() function you'll need to use const_iterator instead of iterator. See lesson 19.4.

  • Here is an example that shows how to use an iterator to access a particular member of a struct:

    swap((*iter1).firstmember, (*iter2).firstmember);
    
    This will work just fine, but there is a C++ operator that combines these two (dereference and then select). I works like this:
    swap(iter1 -> firstmember, iter2 -> firstmember);

    (Note that you won't actually use this example code, because when you swap, you should just swap the entire struct. There's no need to swap the individual members of the struct separately.)