QuickSort C++ Implmentation using Iterators #2 This is part 2 of the previous question asked which is at - QuickSort C++ Implementation using Iterators. 

Based on Jerry Coffin's answer, I have made the changes except for using templates.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;utility&gt;


typedef std::vector&lt;int&gt;::iterator itr;

itr partition(itr left,itr right)
{

    itr i=left-1;

    itr it=left;

    while(it&lt;right)
    {
        if(*it&lt;*right)
        {
            using std::swap;

            std::advance(i,1);
            swap(*i,*it);
        }
        std::advance(it,1);
    }

    using std::swap;

    swap(*(i+1),*right); 
    std::advance(i,1);
    return i;
}

void quicksort(itr left, itr right)
{
    if(std::distance(left,right)&gt;=2)
    {
        itr q=partition(left,right);
        quicksort(left,q-1);
        quicksort(q+1,right);
    }
}


int main()
{
    std::vector&lt;int&gt; v={6,10,4,5,3,9,13,8};

    std::copy(v.begin(),v.end(),std::ostream_iterator&lt;int&gt;(std::cout," "));
    std::cout&lt;&lt;std::endl;

    quicksort(std::begin(v),std::end(v)-1);

    std::copy(v.begin(),v.end(),std::ostream_iterator&lt;int&gt;(std::cout," "));
    std::cout&lt;&lt;std::endl;


    return 0;
}
You still have the same UB as in the original question. An unpleasant (i + 1) suggests the fix: first swap, then advance:

        itr i=left;
        itr it=left;

        while(it&lt;right)
        {
            if(*it&lt;*right)
            {
                using std::swap;

                swap(*i,*it);
                std::advance(i,1);
            }
            std::advance(it,1);
        }

        using std::swap;

        swap(*i,*right);
        return i;