introduction to the C++ standand template library

The C++ Standard Template Library is one of the advance features in the C++ programming language. It can be an incredibly complicated topic as it covers a wide range of components but we will be looking at just a basic introduction to the C++ standard template library. The aim of the C++ STL is write reusable code and generic code which is data type independent.

The STL Library contains  five components including:

  1. Containers: commonly used in data structures like dynamic arrays, double ended queue, doubly linked lists, associative array etc
  2. Iterators: traversal through data in containers
  3. Algorithms: Commonly used algorithms like search, sort, count, swap data
  4. Functors: Classes that overload the function operator
  5. Adaptors: Modifications of a container for a specific purpose, e.g stack and queue

STL: Containers

The STL containers may be classified as

  • Sequence containers: no relationship exists between the data, eg dynamic array, linked list
  • Associative containers: relationship exists between data, eg pair, associative array

A common STL Container used is the Vector container, which is commonly used for dynamic arrays. The advantage is that it automatically resizes the array as required for the array element to operate the add/delete function. Vectors are efficient at random data access with [] and adding/deleting data from the end of the container, but poor at adding/deleting data from the front or middle of an array.

Vector container operations

  • size(): returns current size of vector but not the same as capacity
  • capacity(): returns maximum number of data elements before reallocation needed
  • max_size(): returns maximum number of data elements possible (not related to memory allocation)
  • resize(size_t n) : add/delete elements to make vector of size n
  • reserve(size_t n): set maximum number of data elements to n before allocation
  • empty(): returns true if vector is empty, false if not empty
  • push_back(&  newData): add one element to the end of the vector (vector is added one by one)
  • pop_back(): remove one element from the end of the vector
  • swap(vector& myOtherVector): swap contents of two vectors
  • clear(): erase all vector data elements

Accessing vector elements

  • Like arrays, vector data elements can be accessed with the [] operator
  • at(size_t m): function return the value at index m, equivalent to myVector[m] but is access-safe
  • front(): function returns the first value, equivalent to myVector0[0]
  • back(): function returns the last value
  • Supported comparision operators: =, ==, !=, >, >=, <, <=

STL: Vector Container example code

</pre>
<pre>#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;
 
int main(int argc, char* argv[])
{
	vector<int> myIntVector; 
	int numValues = 0;

	cout << "\nInitial vector size: " << myIntVector.size() << endl;
	cout << "\nEnter number of vector elements: ";
	cin >> numValues;

	for(int i = 0; i < numValues; i++)
        {       
                // Add values into the vector (at the end)
		myIntVector.push_back(i);
	}
	for(int i = 0; i < numValues; i++)
        { 
                // Print vector values using for loop
		cout << "Data element " << i << " of vector: " << myIntVector[i] << endl;
		//cout << "Data element " << i << " of vector: " << myIntVector.at(i+1) << endl;		
	}

	// Print vector values using iterator
	vector<int>::iterator myIntVectorIterator = myIntVector.begin();
	while(myIntVectorIterator != myIntVector.end()) 
        {
		cout << "Iterator data element of vector: " << *myIntVectorIterator << endl;
		myIntVectorIterator++;
	}

	// Print vector values using reverse iterator
	vector<int>::reverse_iterator myIntVectorIteratorBwd = myIntVector.rbegin();
	while( myIntVectorIteratorBwd != myIntVector.rend()) 
        {
		cout << "Reverse iterator data element of vector: " << *myIntVectorIteratorBwd << endl;
		myIntVectorIteratorBwd++;
	}

	// Note that end() points to a position AFTER the last element − this code is erroneous, may seg fault
	myIntVectorIterator = myIntVector.end();
	cout << "end() data element of vector: " << *myIntVectorIterator << endl;

	cout << "\nInitialised vector size: " << myIntVector.size() << endl;
	cout << "Vector capacity: " << myIntVector.capacity() << endl;

	myIntVector.resize(100);
	cout << "Resized vector size: " << myIntVector.size() << endl;
	cout << "Resized vector capacity: " << myIntVector.capacity() << endl;

	myIntVector.clear();
	cout << "Erased vector size: " << myIntVector.size() << endl;
	cout << "Erased vector capacity: " << myIntVector.capacity() << endl;
	cout << "Max vector size: " << myIntVector.max_size() << endl;	

   return 0;
}</pre>
<pre>
Share this post