I am trying to find the permutation and combination of elements with 2 objects each - nC2 or nP2. I can find the combinations by the below code. Is there any elegant way to rewrite this? Also, is there any way to find permutations? The below is just an example, My datasets consists of close to 2000 elements. So, speed is a factor too.
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> array = {"a", "b", "c", "d", "e"};
std::vector<std::string>::iterator it = array.begin();
for ( ; it < array.end(); it++ ) {
for (std::vector<std::string>::iterator it_next = it+1 ; it_next < array.end(); it_next++ ) {
std::cout << *it << *it_next << "\n";
}
}
}
Program output -
gcc version 4.6.3
ab ac ad ae bc bd be cd ce de