standard-library-in-x

Notes and readings for STL workshop

View on GitHub

Iterators

INTRODUCTION

What are iterators? In STL iterators are the most general way to access data in containers
An iterator is any object that, points to some element in a range of elements (such as an array or a container) and has the ability to iterate through those elements using a set of operators (with at least the increment (++) and dereference (*) operators).
Iterators provide some common additional functionality to container and makes it possible to iterate(traverse) through the containers which can be accessed through the bracket( [ ] ) operator.
The main advantage of iterators, of course, is that they greatly increase the reuse of code: your own algorithms, based on iterators, will work on a wide range of containers, and your own containers, which provide iterators, may be passed to a wide range of standard functions.

CONSTRUCTION

general construction of iterators looks like

Container <datatype>::iterator name;

ex. for creating an iterator for a vector

vector <int>::iterator  it;

for list

list <int>::iterator it;


FUNCTIONS

Function
What it does ?
begin() This function is used to return the beginning position of the container.
end() This function is used to return the end position of the container
advance() This function is used to increment the iterator position till the specified number mentioned in its arguments
next() This function returns the new iterator that the iterator would point after advancing the positions mentioned in its arguments
prev() This function returns the new iterator that the iterator would point after decrementing the positions mentioned in its arguments
inserter() This function is used to insert the elements at any position in the container. It accepts 2 arguments, the container and iterator to position where the elements have to be inserted.
distance() Calculates the number of elements between first and last

Note - A few datatypes allow ‘-‘ operation to find the distance second-first but these aren’t allowed for all containers hence we use distance().

IMPLEMENTATION

The following is the implementation of few iterator function in vectors,


#include<iostream>
#include<iterator> // for iterators
#include<vector> // for vectors
using namespace std;
int main()
{
    vector<int> ar = { 1, 2, 3, 4, 5 };
    // Declaring iterator to a vector
    vector<int>::iterator ptr;
    // Displaying vector elements using begin() and end()
    cout << "The vector elements are : ";
    for (ptr = ar.begin(); ptr < ar.end(); ptr++)
        cout << *ptr << " ";
    cout<<"\n";
    vector<int>::iterator ptr1 = ar.begin();
    advance(ptr1, 3);
    cout << "The position of iterator after advancing is : ";
    cout << *ptr1 << " ";
    return 0;    
}

Output:

The vector elements are :
1 2 3 4 5
The position of iterator after advancing is :
4


REVERSE ITERATORS

INTRODUCTION

Reverse iterator is an iterator adaptor that reverses the direction of a given iterator. In other words, when provided with a bidirectional iterator, std::reverse_iterator produces a new iterator that moves from the end to the beginning of the sequence defined by the underlying bidirectional iterator.
Here we make us of rend() and rbegin() where rbegin() points to the last element of the container and rend() points to one position before the first element.
Using the operation ++ makes the iterator move a step towards the first element

IMPLEMENTATION

for(vector<int>::reverse_iterator i=ar.rbegin();i!=ar.rend();++i)
{
	cout<<*i<<" ";
}

Output:

5 4 3 2 1

All other functions are the same as that of the iterators.

Note - (For some lazy people) some efforts in typing of syntax can be reduced if you make use of auto keyword used provided by c++, using this the above implementation would look like:

for(auto i=ar.rbegin();i!=ar.rend();++i)
{
	cout<<*i<<" ";
}