The C++ STL (Standard Template Library) is one of the advanced 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 is to write reusable and generic code that is data type independent.

The C++ STL Library contains five components including:

  1. Containers: commonly used in data structures like dynamic arrays, double-ended queues, doubly linked lists, associative arrays, 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

C++ 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 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 the current size of the vector but not the same as the capacity
  • capacity(): returns the maximum number of data elements before reallocation needed
  • max_size(): returns the 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 the 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 comparison operators: =, ==, !=, >, >=, <, <=

STL: Vector Container example code

&lt;/pre&gt;
&lt;pre&gt;#include&nbsp;"stdafx.h"
#include&nbsp;&lt;iostream&gt;
#include&nbsp;&lt;vector&gt;

using&nbsp;namespace&nbsp;std;
&nbsp;
int&nbsp;main(int&nbsp;argc,&nbsp;char*&nbsp;argv[])
{
	vector&lt;int&gt;&nbsp;myIntVector;&nbsp;
	int&nbsp;numValues&nbsp;=&nbsp;0;

	cout&nbsp;&lt;&lt;&nbsp;"\nInitial&nbsp;vector&nbsp;size:&nbsp;"&nbsp;&lt;&lt;&nbsp;myIntVector.size()&nbsp;&lt;&lt;&nbsp;endl;
	cout&nbsp;&lt;&lt;&nbsp;"\nEnter&nbsp;number&nbsp;of&nbsp;vector&nbsp;elements:&nbsp;";
	cin&nbsp;&gt;&gt;&nbsp;numValues;

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

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

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

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

	cout&nbsp;&lt;&lt;&nbsp;"\nInitialised&nbsp;vector&nbsp;size:&nbsp;"&nbsp;&lt;&lt;&nbsp;myIntVector.size()&nbsp;&lt;&lt;&nbsp;endl;
	cout&nbsp;&lt;&lt;&nbsp;"Vector&nbsp;capacity:&nbsp;"&nbsp;&lt;&lt;&nbsp;myIntVector.capacity()&nbsp;&lt;&lt;&nbsp;endl;

	myIntVector.resize(100);
	cout&nbsp;&lt;&lt;&nbsp;"Resized&nbsp;vector&nbsp;size:&nbsp;"&nbsp;&lt;&lt;&nbsp;myIntVector.size()&nbsp;&lt;&lt;&nbsp;endl;
	cout&nbsp;&lt;&lt;&nbsp;"Resized&nbsp;vector&nbsp;capacity:&nbsp;"&nbsp;&lt;&lt;&nbsp;myIntVector.capacity()&nbsp;&lt;&lt;&nbsp;endl;

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

&nbsp;&nbsp;&nbsp;return&nbsp;0;
}&lt;/pre&gt;
&lt;pre&gt;
Share this post