Home‎ > ‎CS 11M‎ > ‎

Vectors

Questions, Answers and Review

  • Questions from last class?
  • Questions about labs or upcoming homework?

Vectors

Arrays have existed as long as there's been C and they way they work was considered a major advancement in the ability to efficiently program a computer. Their great efficiency comes at a cost to the programmer. They are difficult to use because they cannot be resized and stepping over their boundary causes programs to crash (or worse, is a way for computer viruses to enter the system). What if you wanted to store input into an array but you didn't know how much input your program would get? There's no simple answer to that question, but in C++ there's a better way (that's almost as efficient): vectors.

Defining a Vector

Just like an array a vector must have a type associated with it, also just like an array you can only store one type in a vector. Here an example of a vector declaration:

vector<int> my_vector_of_ints;
vector<char> my_vector_of_chars(5); 

Notice that, unlike an array, you don't have to specify a maximum size. Vectors automatically grow and shrink when you add and remove elements. The type of the vector is between the '<' and '>' symbols. This is called "template" syntax in C++ and is a topic for a more advanced course. Now that you have a vector what can you do with it?

Using Vectors

If you're familiar with arrays vectors are a relief. You can access elements in a vector just like accessing elements in an array:
vector<int> my_vector(1); 
...
vector[0] = 1; 
cout << vector[0];

You can also add an element to the back of the vector without first knowing how many elements are in the vector: 
vector<int> my_vector;
...
vector.push_back(10);
vector.push_back(11);
vector.push_back(12);

You don't need to keep track of how many elements are in a vector, it does that for you:
vector<int> my_vector;
...
int elements = vector.size(); 
vector.push_back(1);
vector.push_back(2);
int foo = vector.size() - elements;  // What is the value of foo? 

Passing Vectors to Functions

Vectors are designed for convenience and ease. As you saw in the previous section arrays are always passed to functions by reference. Vectors can be passed by reference or by value. Beware passing a vector by value makes a complete copy of the vector!

Check Yourself 

Write a program with these two functions:

void add_ten_by_reference(vector<int> &vect) {
 
  vect.push_back(10);

}

void add_ten_by_value(vector<int> vect) {  
  vect.push_back(10);

}

Your program should be able to print every value in an array. You can do that like this:

for (int i=0; i<vect.size(); i++) {
   
  cout << vect[i];

}

  In your program create a vector and call both of the above functions using your vector. 
  1. How many 10's were added to your vector?
  2. Why?

Exercise: Adding Items to a Vector

Write a function that takes the values from an array and places them into a vector. Start with the following code:

#include <ArduinoSTL.h>

void setup() {
  Serial.begin(9600);
}

// put toVect() here

void loop() {  
  vector<int> vect;
  int numbers[] = {10, 34, 23, 43};  
  toVector(vect, numbers, 4);

}

Specifications:
  1. Add the toVector() function before the loop() function.
  2. After calling toVector() the vector vect must have the same values as numbers[]
  3. The order is important (vect must be in the same order)
  4. Be sure to print the values in the loop() function. You may add a separate print() function.
  5. Define toVector() by reference, not value, for the vector<int> parameter.
Submit your vector.ino file on Canvas with the homework.

Comments