Iterating over unequal unordered pairs in one collection This code kinda hurts my feelings -- everything else in my code is done in neat one-liners, exploiting algorithms and, sometimes boost::bind, except for this piece. To say nothing about awkward if(b!=a).

Is there a better way to do the task?

#include &lt;iostream&gt;
#include &lt;list&gt;
using namespace std;

int main()
{
    int arr[] = {1,2,3,4,5,6};
    list&lt;int&gt; lst(arr,arr+sizeof(arr)/sizeof(int));

    for(list&lt;int&gt;::iterator a = lst.begin(); a != lst.end();a++)
    {
            for(list&lt;int&gt;::iterator b = a; b != lst.end(); b++)
                    if(b != a) cout &lt;&lt; " ("&lt;&lt;*a&lt;&lt;","&lt;&lt;*b&lt;&lt;")";
            cout &lt;&lt; "\n";
    }
    return 0;
}
Also not really pretty, but perhaps a start

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;list&gt;
using namespace std;

int main()
{
    list&lt;int&gt; lst = {1,2,3,4,5,6};

    auto a = lst.cbegin();
    int first = *a;
    auto output_pairs = [&amp;first](const int second){ cout &lt;&lt; " ("&lt;&lt;first&lt;&lt;','&lt;&lt;second&lt;&lt;')'; };
    while(++a != lst.cend())
    {
        for_each(a, lst.cend(), output_pairs);
        cout &lt;&lt; '\n';
        first = *a;
    }
    cout &lt;&lt; flush;
}


It's C++0x because I was too lazy to write a functor for that lambda and I don't know the Boost.Lambda syntax off the top of my head.