Transcript [03]

CMSC 202
Lesson 3
C++ Primer: Vectors
#include <iostream>;
using namespace std;
Warmup


There are
several compiler
errors and logic
errors in the
following code.
int main()
{
cout >> "How many items?" >> endl;
int size;
cin << size;
int array[size];
for (int i = 0; i <= size; ++i)
{
cout >> "Next Item?" >> endl;
cin << array[i];
}
Identify them!
return 0;
}
Arrays are Evil, Why?

Arrays, Good?

Arrays, Bad?
Vectors, Gooooooood!



Similar to Arrays
•
•
Direct index-based access
Compact
Different than Arrays
•
•
•
•
Completely dynamic
Occasional memory re-allocation
Bounds-safe
Methods
#include <vector>
Vectors

Syntax
vector<type> vecName;
vector<type> vecName ( size );
vector<type> vecName ( size , initVal );

Example
vector<int> integers;
vector<Car> cars;
vector<string> messages;
vector<Student> students(100);
// 100 Students
vector<double> grades(100, 0.0);
// 100 doubles, all 0.0
Vector Access

Two methods
•
•
[index] – just like arrays
• Syntax
vecName[index] = newValue;
temp = vecName[index];
.at(index) – different from arrays
• Syntax
vecName.at(index) = newValue;
temp = vecName.at(index);

NEW: dot operator
•
•
Calls a method associated with that object
vecName is the object, at() is the method
Vector Access

Examples
vector<double> grades(100, 0.0);
grades[0] = 97.6;
grades.at(1) = 86.4;
grades[2] = grades.at(1) / 2.0;

Why use .at(i)?
•
Boundary checking!
• Throws an exception if out-of-bounds
If there are 10
items in a
vector, what is
the index of the
last item?
Vectors – Bigger is Better!

.push_back(item)
• Add an item to back of vector
What’s going
on here?
vector<int> numbers(9);
for (int i = 0; i < 9; ++i)
numbers.at(i) = i + 1;
numbers.push_back(10);
1
2
3
4
5
6
7
8
9 10
.push_back(10)
Vectors – Other Methods







.size()
•
Number of elements
.capacity()
•
Number of elements it “can” hold (reserved…)
.erase( iterator )
•
Erase the item at the iterator position
.reserve( size )
•
Reserves space for size number of elements
.resize( size )
•
Shrinks/grows the vector to size
.empty()
•
Returns true if vector is empty, false otherwise
.clear()
•
Erases all items in the vector
Vectors - Iterators

Erase? Iterators? How does that work?
•
Iterator
•
Using an index WITH an Iterator
•
Example
• Special “pointer” that points into the vector
• NOT the index of the item!
•vecName.begin() is an iterator
• Add the index to the iterator
numbers.erase( numbers.begin() + 3 );
• This will erase the 4th item (with index 3)
Vectors - Multidimensional

Multidimensional arrays
• 10 x 20 matrix, 10 rows, 20 columns
int scores[10][20]
• 3 x 3 x 3 matrix
int cube[3][3][3]

Multidimensional vectors
• A little different!
• For each dimension (except last)
• Push_back a collection of vectors!
Vectors - Multidimensional

Example
Extra space – VERY
IMPORTANT, why?
vector<vector<int> > integers;
for (int i = 0; i < 10; ++i)
{
integers.push_back( vector<int>() );
for (int j = 0; j < 20; ++j)
{
integers[i].push_back(i * j);
}
Push_back()
whole vector
}
Push_back()
integer
Practice

Create a vector of strings

Add your first, middle and last name to the
vector
• Hint: vector<…> …
• Hint: …push_back(…)
Challenge





Use vectors and strings
Ask the user to enter in text
Store each word in the vector
When they enter a period (as a word by
itself), terminate the input
Print the text, one word on a line