-2

I need to access all possible number of combinations of characters in a string. Without Using Any loop, because i have to filter records in thousands. loop can give performance issue. lets Take a Example : Any Custom method ABCD ===>> ABCD, ABDC,ACBD,ACDB, ADCB, BACD, BADC, BCAD, BCDA, BDAC , BDCA , CADB,CBDA,CDBA, CBAD, DABC, DBCA, DCBA,DCAB......

thanks in Advance

Ajjjjjjjj
  • 669
  • 4
  • 12

1 Answers1

1

There is in fact a function in the C++ standard library for this: std::next_permutation. Here is the example from the link, adapted to your case.

#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    std::string s = "ABCD";
    std::sort(s.begin(), s.end());
    do
    {
        std::cout << s << '\n';
    }
    while(std::next_permutation(s.begin(), s.end()));
}

Online here

Jonas
  • 6,915
  • 8
  • 35
  • 53
  • dear please give custom logic for next_permutation... no need of inbuilt function – Ajjjjjjjj Apr 21 '17 at 10:17
  • Just look at the **Possible implementation** here: http://en.cppreference.com/w/cpp/algorithm/next_permutation – Jonas Apr 21 '17 at 10:18