Module 0 (Iterator) - Algoritma-dan-Pemrograman-ITS/StrukturData GitHub Wiki

Basic Iterator

iterator

Before we talk about iterator, we should know what STL is. STL (Standard Template Library) is a collection of template class for several functions and data structures (also known as container). iterator here is similar to pointer used for accessing memory of STL data structures.

Declaration

iterator could be declared as:

STL_container<data_type>::iterator variable_name;
list<int>::iterator pointer;

begin() and end() functions

Every STL Container has a pair of functions called begin() and end(). begin() is a function that returns an iterator pointing to the first element of a certain STL Container. While end() returns an iterator pointing to the empty element next to the last element. Here is an illustration.

begin and end

Both of them has their respective reversed version of them, namely rbegin() and rend().

rbegin and rend

One application that use the functions above is sort()

#include <bits/stdc++.h>
/*
the library above includes all of C++ libraries

if you having difficulty using that library, use <algorithm> to use function sort
*/
using namespace std;

int main(){
    vector<int> v{3, 4, 2, 5, 1, 7};
    /*
    vector initialization is different compared to static array
    */

    sort(v.begin(), v.end());

    for(int i=0; i<v.size(); i++){
        cout << v[i] << " ";
        // vector would be sorted ascendingly
    }
    cout << endl;

    sort(v.rbegin(), v.rend());

    for(int i=0; i<v.size(); i++){
        cout << v[i] << " ";
        // vector would be sorted descendingly
    }
    cout << endl;

    return 0;
}

auto

auto is a reserved word on C++ Language. auto would make the program specified the data type of a variable by looking at initialized value of the variable (variable has to be initialized first when using auto).

Keyword auto would be useful to avoid typing an extremely long data type such as iterator.

ilustrasi auto

On the example above, data type of the variable pointer automatically set as the data type of iterator of vector.

Operator On iterator

iterator has several operator such as increment(++), decrement(--)(not all of iterator could be decremented) and dereference(*). There are tons of iterator operator depends on the kind of the iterator (More on that here).

Here is the usage of increment and dereference.

#include <bits/stdc++.h>
using namespace std;

int main(){
    list<int> l{1, 2, 3, 4, 5};

    auto pointer = l.begin();

    pointer++;
    // pointer now points to the second element of the list

    cout << *pointer << endl;
    // syntax above will show the second element of the operator

    return 0;
}

With increment and dereference, we could access every element of an STL Container without having to emptied an STL Container.

#include <bits/stdc++.h>
using namespace std;

int main(){
    list<int> l{1, 2, 3, 4, 5};

    for(auto it = l.begin(); it != l.end(); it++){
        cout << *it << endl;
    }
    
    return 0;
}

Is there a shorter syntax to iterate an STL Container? Yes, there is.

#include <bits/stdc++.h>
using namespace std;

int main(){
    list<int> l{1, 2, 3, 4, 5};

    for(auto it:l){
        cout << it << endl;
    }
    
    return 0;
}

(Data type of variable it is not an iterator anymore).

Practice >

⚠️ **GitHub.com Fallback** ⚠️