有没有一种 STL 方法可以找到字符串的所有排列,给出一个以 C++ 为单位的大小?

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

本文关键字:排列 一个以 为单位 C++ 字符串 一种 STL 方法 有没有      更新时间:2023-10-16

查找按 C++ 大小指定的字符串的所有排列的最佳方法是什么?我正在从 python 过渡,想知道C++是否有一个内置的可以做到这一点,或者我是否必须从头开始实现它。在Python中,我只会做

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

这是我尝试模拟的功能。任何帮助将不胜感激,除了没有大小参数next_permutations之外,真的找不到任何东西。

不仅仅是 STL,而且非常简单:

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());