Standard Template Library
Submitted by pavel7_7_7 on Saturday, February 8, 2014 - 10:25.
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.
-
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: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:- vector<int> v1;
- vector<int> v2 (3,21);
- vector<int> v3 (v2.begin(),v2.end());
- vector<int> v4 (v3);
The- for (int i = 0; i<v2.size(); i++)
- cout << ' ' << v2[i];
- cout << endl;
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 assecond[index]
You can access first element usingfront()
function and the last element usingback()
function. To test, if the vector is empty,you can usebool empty()
function. To add element to the vector, usepush_back(element)
function. And to delete the last element usepop_back()
function.The list of some other functions of vector class- v1.push_back(3);
- v1.push_back(2);
- // now front equals 1, and back 2
- v1.front() -= v2.back();
- v1.pop_back();//only one element - 3 is in vector now
- insert - inserts elements in vector
- swap - swap content
- erase - erase element
- clear - delete all data from vector
-
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:
In addition to
- deque<int> d1; // empty deque of ints
- deque<int> d2 (3,21); // 4 ints with value 21
- deque<int> d3 (d2.begin(),d2.end()); // elements from first to last of d2
- deque<int> d4 (d3); // a copy of d3
push_back(object)
andpop_back()
functions that are used in vectors, deque provides two new functions:push_front(object)
andpop_front()
As an example:The ouptut of this code is 500 400 100 100 An example of use of- deque<int> d2 (2,100); // two ints with a value of 100
- d2.push_front (400);
- d2.push_front (500);
- cout << "d2 contains:";
- for (int i = 0; i != d2.size(); ++i)
- cout << ' ' <<d2[i] ;
- cout << endl;
pop_front()
is:And the ouptut of this programm is- deque<int> d1;
- d1.push_back (1);
- d1.push_back (2);
- d1.push_back (3);
- cout << "Popping out the elements in d1:";
- while (!mydeque.empty())
- {
- cout << ' ' << d1.front();
- d1.pop_front();
- }
- cout << "\nAfter popping out elements size of d1 is " << d1.size() << '\n';
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
-
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:
Elements of a list can be accessed only by iterators and
- list<int> l1; // empty list of ints
- list<int> l2(3,21); // 3 ints with value 21
- list<int> l3(l2.begin(),l2.end()); // copying all elements from begin to end of l2
- list<int> l4 (l3); // a copy of l3
front()
andback()
functions. Some interesting operations that can be performed on lists are:void reverse();
- reverse the elements of a list.Output is mylist contains: 9 8 7 6 5 4 3 2 1 Another function -- list<int> mylist;
- for (int i=1; i<10; ++i)
- mylist.push_back(i);
- mylist.reverse();
- cout << "mylist contains:";
- for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
- cout << ' ' << *it;
- cout << endl;
void sort();
Sorts list of comparable objects. It also can be called with a comparator as an argumentYou can also remove duplicates from the list using- template <class Compare>
- void sort (Compare comp);
void unique();
function.
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
- 47 views