1

Possible Duplicate:
combination and permutation in C++

Let's say I have a vector with the following elements: {1,2,3}. How can I traverse the vector in a pair-wise fashion. So the first iteration would be {1,2}, the second {1,3} and finally the third is {2, 3}.

For triplets there would be only one iteration: {1,2,3} in this case.

Are there algorithms in STL or boost to accomplish that?

Thanks, Christian

Community
  • 1
  • 1
chhenning
  • 2,017
  • 3
  • 26
  • 44
  • 1
    stacked for loops have traditionally accomplished this task. for(int x=0; x – Louis Ricci Jan 09 '13 at 19:04
  • 3
    Check this [SO Questions](http://stackoverflow.com/questions/2211915/combination-and-permutation-in-c). This is actually a problem of combinations – Abhijit Jan 09 '13 at 19:05

1 Answers1

4
for (int i = 0; i < vec.size() - 1; ++i)
    for (int j = i + 1; j < vec.size(); ++j)
        std::cout << '{' << vec[i] << ',' << vec[j] << '}';
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Thanks! That's the way I'm doing it. But I'm looking for a way in STL or Boost. If there isn't that's no biggie. But I was hoping for something like next_permutation, or so. – chhenning Jan 09 '13 at 19:10