To understand how a set works you might want to read:

http://www.cplusplus.com/reference/set/set/[^]

One of the important things is that a set keeps its elements in sorted order. When you insert a new element, a binary search is used to find the place where the element has to be inserted.

See http://www.cplusplus.com/reference/set/set/insert/[^] to read more about the insert operation.

Hope that made it a little clearer.

[AMENDED]

Here is a line by line description of what happens:

C++

do { vector<int> v; // declares a vector of ints // this loop copies the values of the myints array // into v (note that the call to next_permutation will rearrange // the values in myints at the end of each iteration) for (int i = 0; i < N; i++) v.push_back(myints[i]); // inserts the vector v into the set s; as a set keeps its elements // in an ordered sequence, the insert function has to compare the new // element (v) with the elements already stored in the set. This comparison // is performed by the compare function specified in the sets // template parameters, and as no such parameter is specified, the // operation< member function of vector<int> is used, which compares two // vectors according to their lexicographical order. (Perhaps this is // the part you didn't understand) s.insert(v); // calls std::next_permutations, which rearranges the values in // the myints array; once the original permutation (0, ...9) has been reached, // next_permutation will return false and the loop ends } while ( std::next_permutation(myints,myints+10) );

[2nd AMENDMENT]

C++

// loop over all elements of set s and assign each one in turn to a variable c // (note: the auto keyword lets c assume the type that is the element type of // set s, which is vector<int>) for(auto c: s){ // so now we have in c one of the vector<int> elements stored in s; in // the inner loop, we walk over all elements of that vector<int> and each // is in turn assigned to variable i for (auto i: c) // we output that element i and a space cout << i << " "; // after the inner loop has output all elements of c, we terminate // the output line cout << endl; }

I hope that makes it clear what the nested loops do.

—SA

if you want to know how a std::set works, look at the header, though knowing the specifics wouldn't be too useful IMHO