Standard Template Library

Standard Template Library (STL)is a software library of c++. This library consists of a lot of useful components. All the classes, that are part of STL can be divided in four categories : containers,algorithms, functional, and iterators. In programming container is an abstract data type (ADT) which represents a collection of other objects. Algorithms in c++ language are components, that perform a list of operations on containers and other data sequences. The iterator is an object that provides possibility to traverse a collection. We will start our introduction from the containers and we will speak about vector, deque and list.
  1. Vector. template < class T, class Alloc = allocator<T> > class vector; Vector is a sequence container that represent an array, that can change it's size dynamically. There are four types of vector's constructors:
    1. vector<int> v1;                                
    2. vector<int> v2 (3,21);
    3. vector<int> v3 (v2.begin(),v2.end());
    4. vector<int> v4 (v3);
    The first constructor create an empty vector of integers. The second constructor create a vector of 3 integers with value 21. The third constructor iterates through the second vector and copy elements from the begin of 2nd to it's end And the last constructor creates a vector, that is copy of the v3. You can access elements of a vector like elements of an array using operator[]. For example:
    1. for (int i = 0; i<v2.size(); i++)
    2.     cout << ' ' << v2[i];
    3.   cout << endl;
    The  v2.size() returns the number of elements stored in vector. There are another ways to access vector's elements: v2.at(index) is the same as second[index] You can access first element using front() function and the last element using back() function. To test, if the vector is empty,you can use bool empty() function. To add element to the vector, use push_back(element) function. And to delete the last element use pop_back() function.
    1. v1.push_back(3);
    2. v1.push_back(2);
    3. // now front equals 1, and back 2
    4. v1.front() -= v2.back();
    5. v1.pop_back();//only one element - 3 is in vector now
    The list of some other functions of vector class
    • insert - inserts elements in vector
    • swap - swap content
    • erase - erase element
    • clear - delete all data from vector
  2. deque acronym of double-ended queue. Deques are sequence containers that can be expanded or contracted on both ends. The main difference from vector consists in the fact that deques are not guaranteed to store all its elements in contiguous storage locations, thus not allowing direct access by offsetting pointers to elements. The constructors of deque are similar to the vector's constructors:
    1. deque<int> d1;                                // empty deque of ints
    2. deque<int> d2 (3,21);                       // 4 ints with value 21
    3. deque<int> d3 (d2.begin(),d2.end());  // elements from first to last of d2
    4. deque<int> d4 (d3);                       // a copy of d3
    In addition to push_back(object) and pop_back() functions that are used in vectors, deque provides two new functions: push_front(object) and  pop_front() As an example:
    1. deque<int> d2 (2,100);     // two ints with a value of 100
    2. d2.push_front (400);
    3. d2.push_front (500);
    4.  
    5. cout << "d2 contains:";
    6. for (int i = 0; i != d2.size(); ++i)
    7.     cout << ' ' <<d2[i] ;
    8.   cout << endl;
    The ouptut of this code is 500 400 100 100 An example of use of pop_front() is:
    1. deque<int> d1;
    2.  
    3. d1.push_back (1);
    4. d1.push_back (2);
    5. d1.push_back (3);
    6.  
    7. cout << "Popping out the elements in d1:";
    8. while (!mydeque.empty())
    9. {
    10.     cout << ' ' << d1.front();
    11.     d1.pop_front();
    12. }
    13.  
    14. cout << "\nAfter popping out elements size of d1 is " << d1.size() << '\n';
    And the ouptut of this programm is
    Popping out the elements in d1: 1 2 3 After popping out elements size of d1 is 0
    The list of some other functions of deque class:
    • insert - inserts elements in deque
    • swap - swap content of deque
    • erase - erase element from deque
    • clear - delete all data from deques
  3. List is a sequence containers that allows insert and erase operations anywhere in container, and iteration in both directions. The main quality of list is that it allows to perform inserting and deleting operations faster then other containers from STL. That's why is often used in different algorithms as sorting. Example of creating a list is:
    1. list<int> l1;                                // empty list of ints
    2. list<int> l2(3,21);                       // 3 ints with value 21
    3. list<int> l3(l2.begin(),l2.end());  // copying all elements from begin to end of l2
    4. list<int> l4 (l3);                       // a copy of l3
    Elements of a list can be accessed only by iterators and front() and  back() functions. Some interesting operations that can be performed on lists are: void reverse(); - reverse the elements of a list.
    1. list<int> mylist;
    2. for (int i=1; i<10; ++i)
    3. mylist.push_back(i);
    4.  
    5. mylist.reverse();
    6.  
    7. cout << "mylist contains:";
    8. for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    9.     cout << ' ' << *it;
    10. cout << endl;
    Output is mylist contains: 9 8 7 6 5 4 3 2 1 Another function -  void sort(); Sorts list of comparable objects. It also can be called with a comparator as an argument
    1. template <class Compare>
    2.   void sort (Compare comp);
    You can also remove duplicates from the list using void unique(); function.
  4. STL is really a huge collection of classes that can be used for different purposes. In the next tutorial I'll speak about the algorithms of the STL.

Add new comment