CS134 Unit 07 Exercise: Lists/Vectors of data
Table of Contents
1. Introduction
1.1. Creating a list/vector
Python:
// General: LISTNAME = [] string_list = [] int_list = []
C++:
// General: vector<DATATYPE> VECTORNAME; vector<string> string_list; vector<int> int_list;
In C++, you'll need to have #include <vector>
at the top of the file.
1.2. Adding items to the list/vector
Python:
// General: LISTNAME.append( DATA ); studentList.append( "Kabe" ) name = "Luna" studentList.append( name )
C++:
// General: LISTNAME.push_back( DATA ); studentList.push_back( "Kabe" ); string name = "Luna"; studentList.push_back( name );
1.3. Accessing items at some index
Python:
// General: LISTNAME[ INDEX ] print( "first:", studentList[0] ) print( "second:", studentList[1] ) print( "third:", studentList[2] ) print( "fourth:", studentList[3] )
C++:
// General: LISTNAME[ INDEX ] cout << "first: " << studentList[0] << endl; cout << "second: " << studentList[1] << endl; cout << "third: " << studentList[2] << endl; cout << "fourth: " << studentList[3] << endl;
Using a variable to access an element:
Python:
index = int( input( "Enter index:" ) ) print( "That is:", studentList[index] )
C++:
int index; cout << "Enter index: "; cin >> index; cout << "That is: " << studentList[index] << endl;
1.4. Looping through all the elements
1.4.1. For loop 1: getting an index and element
Here we use a counter variable, which we usually name i
. i
will begin at 0, go up to 1, then 2, and so on until it hits the last index of the list of items.
i
represents the index and studentList[i]
represents the element.
Python:
for i in range( len( LISTNAME ) ): # Display index (i) and # then the element (LISTNAME[i]): print( i, LISTNAME[i] )
C++:
for ( int i = 0; i < LISTNAME.size(); i++ ) { // Display index (i) and // then the element (LISTNAME[i]): cout << i << ". " << LISTNAME[i] << endl; }
1.4.2. For loop 2: getting just the element
For these loops, they use an alias variable - each time through the loop, the student
placeholder is filled with the next item in the list.
Python:
for item in LISTNAME: print( item )
C++:
for ( auto& item : LISTNAME ) { cout << item << endl; }
2. Program part 1: Recipe names
Recipes: 0. Gulab Jamun 1. Aloo Paratha 2. Mango Lassi
Create a list/vector of strings
named recipes
. Each element should be the name of a different recipe.
Without using a loop, display each element's index (e.g., "0") and then the element's value (e.g., "Chocolate Chip Cookies").
The index number you print out, like 0, 1, 2, will just be hard coded, like print( "0. ")
. The element value will be
accessed by using the list name (recipes
), the subscript operator ([ ]
), and the index number (0
)… so… recipes[0]
.
3. Program part 2: Adding onto the list
Recipes: 0. Gulab Jamun 1. Aloo Paratha 2. Mango Lassi Enter a new recipe name: Rasgulla 3. Rasgulla There are now 4 recipe(s).
Continue editing the same program file. After the previous items, ask the user to etner a new recipe name. This will be string input.
After getting their input and storing it in a temporary variable, append/push their selection to your recipes
list.
At the end, do another print/cout to display the new recipe item's idnex and value.
You can also display how many items are in the recipes
list with:
Python:
print( len( recipes ) )
C++:
cout << recipes.size() << endl;
4. Program part 3: Looping through the list
Adding onto the previous part, create a loop that displays all the recipes at the end. Use the examples below to
write your for loop to display the index (i
) and the value (replace "LISTNAME" with recipes
.)
Python:
for i in range( len( LISTNAME ) ): # Display index (i) and # then the element (LISTNAME[i]): print( i, LISTNAME[i] )
C++:
for ( int i = 0; i < LISTNAME.size(); i++ ) { // Display index (i) and // then the element (LISTNAME[i]): cout << i << ". " << LISTNAME[i] << endl; }
Recipes: 0. Gulab Jamun 1. Aloo Paratha 2. Mango Lassi Enter a new recipe name: Rasgulla 3. Rasgulla There are now 4 recipe(s). 0. Gulab Jamun 1. Aloo Paratha 2. Mango Lassi 3. Rasgulla