This example comes from the book of "Addison-Wesley, Designing Components with the C++ STL, Electronic 3rd Edition.pdf". Its a simple list implements an iterator in the class.

Look at each function carefully and get a clear idea of the mechnnism you will understand the core of the STL.

 
 
#ifndef SLIST_H
#define SLIST_H
namespace br_stl {
#include <cassert>
#include <iterator>
 
template < class T >
class slist {
public:
    typedef T value_type;
    typedef ptrdiff_t difference_type;
    typedef T* pointer;
    typedef T& reference;
 
 
 
private:
    struct ListElement {
        T Data;
        ListElement * Next;
        ListElement ( const T& Datum , ListElement * p ) : Data ( Datum ) , Next ( p ) {}
    };
    ListElement * firstElement;
    size_t Count;
 
public:
    slist() : firstElement ( 0 ) , Count ( 0 ) {}
    void push_front ( const T& Datum ) {
        firstElement = new ListElement ( Datum ,  firstElement );
        ++Count;
    }
 
    class iterator {
    friend class slist;
    public:
        typedef std::forward_iterator_tag iterator_category;
        typedef T value_type;
        typedef T* pointer;
        typedef T& reference;
        typedef size_t size_type;
        typedef ptrdiff_t difference_type;
 
    private:
        ListElement * current;
 
    public:
 
        iterator ( ListElement * Init = 0 ) : current ( Init ) {}
 
        T& operator * () {
            return current->Data;
        }
 
        const T& operator * () const {
            return current->Data;
        }
 
        iterator& operator ++ () {
            if ( current )
                current = current->Next;
            return *this;
        }
 
        iterator operator ++ (int) {
            iterator tmp = *this;
            ++*this;
            return tmp;
        }
 
        bool operator == ( const  iterator & x ) const {
            return current == x.current;
        }
 
        bool operator != ( const  iterator & x ) const {
            return current != x.current;
        }
 
    };
 
    iterator begin() const { return iterator ( firstElement ) ; }
    iterator end() const { return iterator ( ) ; }
    iterator erase ( iterator p ) {
 
        if ( !firstElement || !p.current ) {
            std::cout << "empty list or the p is the end" << std::endl;
            return end();
        }
 
        iterator Successor = p;
        ++Successor;
 
        ListElement * toBeDeleted = p.current, * Predecessor = firstElement;
 
        if ( toBeDeleted != firstElement ) {
            while ( Predecessor->Next != toBeDeleted )
                Predecessor = Predecessor->Next;
            Predecessor->Next = toBeDeleted->Next; 
        } else {
            firstElement = toBeDeleted->Next;
 
        }
 
        delete toBeDeleted;
        --Count;
        return Successor;
    }
 
    void printlist() {
        for ( iterator i = begin() ; i != end() ; i++ ) {
            std::cout << *i << std::endl;
        }
        std::cout << "Count is :" << Count << std::endl;
    }
};
 
 
template < class Iterator > 
int operator - ( Iterator second , Iterator first ) {
    int count = 0;
 
    while ( first != second && first != Iterator() ) {
        ++first;
        ++count;
    }
 
    assert( first == second );
    return count;
}
};
#endif
 

And here is the test program:

 
#include<algorithm>
#include<iostream>
#include "slist.h"
 
int main () {
    const int count = 100;
    br_stl::slist<int> aContainer;
 
    for ( int i = count ; i >= 0 ; --i ) {
        std::cout << "pushing " << 2 * i << std::endl;
        aContainer.push_front( 2 * i ) ;
    }
 
    int Number = -1 ;
 
    while ( Number != -1 ) {
        std::cout << "enter number:";
        std::cin >> Number;
 
        if ( Number != -1 ) {
 
            br_stl::slist<int>::iterator position = std::find ( aContainer.begin() , aContainer.end() , Number );
 
            if ( position != aContainer.end() ) 
                std::cout << "found at poition " << ( position -  aContainer.begin() ) << std::endl;
            else
                std::cout << Number << " not found" << std::endl;
        }
    }
 
    aContainer.printlist();
    br_stl::slist<int>::iterator i = aContainer.erase(aContainer.begin());
    if( i != aContainer.end() )
        std::cout << "after eraing :" << *i << std::endl;
    else
        std::cout << " return end() " << std::endl;
    aContainer.printlist();
}
 

The three cornerstone of STL is Container , Iterator and algorithm, in here we get a Container slist, the container provide an iterator to access the element of the container, in test program we use the algorithm from the standard library , accept iterator and apply the algorithm on the container.

This example shows the basic principle of most other container and iterators works.