c++stl

Is there an STL method to find all permutations of strings gives a size in C++?


What is the best way to find all permutations of a string specified by size in C++? I'm making the transition from python and was wondering if C++ had a builtin that could do this or if I would have to implement this from scratch. In Python I would just do

import itertools

print(list(itertools.permutations(string,size))

This is the functionality I'm trying to emulate. Any help would be greatly appreciated, couldn't really find anything other than next_permutations which doesn't have a size argument.


Solution

  • Not just STL, but that's as simple as it gets:

    std::string s = ...;
    std::sort(s.begin(), s.end()); // to know when you've been through all permutations
    std::vector<std::string> v; // v.reserve(boost::math::factorial(s.size());
    do {
        v.push_back(s); // or process in-place
    } while (std::next_permutation(s.begin(), s.end());