Lab 5: STL Vector

Topics: Introduction to Vectors

Group work: Group work is allowed for the labs, but each person must do their own coding and each person must turn in an assignment. Copying other peoples' code / code files is not allowed. Copied assignments will receive 0% for everybody involved.

Assignments must build: Assignments that don't build will automatically just be given a flat 50% score. I may still give feedback on what went wrong, and 50% is better than 0%, so turn in something - but ideally, make sure it builds.



In C++, the basic array doesn't let you resize it and it doesn't keep track of the amount of elements within it. You can make a dynamic array using pointers, but you still have to manually manage that data and keep a variable to keep track of the amount of item stored in it vs. the size of the array.

In other languages, it is quite common to have some sort of resizable array class as a standard part of the language. In Java and C#, we have ArrayList, in Python we just use a List.In C++, we have to look at the Standard Template Library (STL) to find some pre-made data structures like this.

Here we will be looking at the vector class. The STL Vector is basically a Wrapper for a Dynamic Array. It handles the resizing for us, and gives us a .size() function to track how many items are stored in it, freeing us up to worry about other design issues.

This Data Structures class is all about writing these storts of structures, but before we get into the nitty-gritty of making our own Vector-like class we can look at how it can be used from the outside.

You can find the vector documentation, function descriptions, and sample code here:


Vector Functions

The vector is a templated class. If you didn't cover templates in a previous class, it is a way we can write one container class that deals with any data type. To declare a vector of strings, it would look like this:

vector<string> myStrings;

In the list of functions, value_type is whatever data type the vector is storing.

Function nameReturn typeParametersDescription
push_back void const value_type& val

Push the val into the vector.

pop_back void

Removes the last element, stored at the end of the vector's internal array (at index size-1)

clear void

Remove all elements from the array.

front value_type

Returns the first item in the vector's array (at position 0).

back value_type

Returns the last item in the vector's array (at position size-1).

at value_type size_type n

Returns the item in the vector's array at the given index.

operator[] value_type size_type n

Same as the at function.

size size_type

Returns the amount of items stored in the vector.


Setting up the project

In your Student Repository, open the CS250-Lab05-STLVector project. It will have the following files:

  • main.cpp
  • functions.hpp
  • functions.cpp

This code includes a program that deals with a vector and offers several options. It also has simple unit tests to check the functionality of each.

You will utilize the STL vector documentation to help implement each function.

**             Vector             **
 1. Set prerequisites
 2. Remove last item
 3. Clear the list
 4. Get first prerequisite
 5. Get last prerequisite
 6. Look at item
 7. Display all items
 8. Quit

Run which function? 

You will implement the functions:

  • void GetClassList(vector<string>& classList)
  • void RemoveLastItem(vector<string>& classList)
  • void ClearList(vector<string>& classList)
  • string GetFirst(vector<string>& classList)
  • string GetLast(vector<string>& classList)
  • string GetItem(vector<string>& classList, int index)
  • string GetAllItems(vector<string>& classList)


void GetClassList(vector<string>& classList)

Add the following items to the classList using .push_back(): cs134, cs200, cs235, cs250, cs210, and cs211.


void RemoveLastItem(vector<string>& classList)

Remove the last ("back") item from the classList passed in using .pop_back()


void ClearList(vector<string>& classList)

Clear all the items out of classList using .clear()


string GetFirst(vector<string>& classList)

Return the first ("front") item from classList using .front()


string GetLast(vector<string>& classList)

Return the last ("back") item from classList using .back()


string GetItem(vector<string>& classList, int index)

Get a specific item from the list at some index, using The subscript operator [] or .at()


string GetAllItems(vector<string>& classList)

Build a string with all the class names, separated by whitespace. You will also use The subscript operator [] or .at() here, as well as .size() with a for loop to get all vector values and concatenate each one to your string.

This function isn't responsible for doing any cout statements - it must return a string!


Running the program

Here the program will give you a menu of options to select, each one modifies the vector object in the program.

**             Vector             **
 1. Set prerequisites
 2. Remove last item
 3. Clear the list
 4. Get first prerequisite
 5. Get last prerequisite
 6. Look at item
 7. Display all items
 8. Quit

Run which function? 


Turn in

Turn in your functions.cpp file - no other files are necessary.