Previous Lecture Lecture 2 Next Lecture

Lecture 2, Thu 04/05

STL, Vectors

Standard Template Library

It’s really rare if a programming language provides all of the necessary tools to accomplish a task “out of the box”

These libraries usually come standard with the language

Implementing and maintaining libraries come with a cost

Since C++ isn’t a product of a large organization, and is kinda organized like opensource

Standard Libarary Containers

There are many implementations of containers.

std::Vector

# Makefile
CXX=g++

main: main.o
        ${CXX} -o main -std=C++11 main.o

clean:
        rm -f *.o main
------------
// main.cpp
#include<vector>

using namespace std;

int main() {
	vector<int> v; // a vector containing int elements
return 0;
}

Under-the-hood, vectors are implemented using arrays and behave similar to arrays.

Adding to a vector example

// main.cp
#include<vector>

using namespace std;

template <class T>
void printVector(vector<T> &v) {
	for (int i = 0; i < v.size(); i++) {
		cout << "v[" << i << "] = " << v[i] << endl;

	// range-based for loop example
	// int index = 0;
	// for (int i : v) {
	// 	cout << "v[" << index << "] = " << i << endl;
	// }

	}
}

int main() {
	vector<int> v;
	for (int i = 0; i < 5; i++) // it could be any reasonable size…
		v.push_back(i);

	printVector(v);
	return 0;
}

Example

cout << v.at(4) << endl;
cout << v.at(5) << endl; // EXCEPTION THROWN
cout << v1[5] << endl; // JUNK

Other supported operations are:

Vector Initialization

push_back() is one way to create elements in a vector. Though it’s not the only way

Example:

vector<int> v1(100); // initializes vector with 100 elements.
vector<int> v2(100, 1); //initializes vector with 100 elements = 1

Example creating a vector on the heap with a pointer reference to the vector contents on the heap

vector<int>* v = new vector<int>(10,1); // vector with 10 elements = 1
cout << v->size() << endl;
printVector(*v);

Iterators

Example

vector<string> v2;

v2.push_back(“Hello.”);
v2.push_back(“My”);
v2.push_back(“name”);
v2.push_back(“is”);
v2.push_back(“Batman”);

for (vector<string>::iterator i = v2.begin(); i < v2.end(); i++) {
	cout << *i << “ “; // string value
	cout << i->size() << endl; // prints the size of the strings
}

In the above example, we’ve seen vector functions that deal specifically with iterators.

Example (Showing different ways to index elements using iterators):

vector<string>::iterator i = v2.begin();
cout << v2[4] << endl; 		// Batman
cout << i[4] << endl;		// Batman
cout << *(i + 4) << endl;	// Batman

In order to erase items in the vector, there is an erase method that requires iterators to do this

Example of erasing elements

// Removing 2nd index of the vector
// v2.erase(v2.begin() + 2); // remove "name"
// printVector(v2);

// Removing 1st and 2nd index - [1,3)
v2.erase(v2.begin() + 1, v2.begin() + 3);
printVector(v2);