给定这个集合
set<string> s = {"a","b","c"};
是否有可能实现 next_permutation() 以获得所有组合,其中元素不重复和排序很重要?
最佳答案
不,这是不可能的。 std::set
是一个关联容器并保持严格的弱排序。 std::next_permutation
转换给定的范围,这会破坏排序。
如果您需要获取 set
内容的排列,我建议您使用 std::vector
.您可以将集合复制到 vector 中,然后从中获取排列。
std::set<int> set_data;
//fill set
std::vector<int> temp(set_data.begin(), set_data.end());
do
{
// code goes here
}
while(std::next_permutation(temp.begin(), temp.end()));
关于c++ - 是否可以在 STL set<t> 上实现 next_permutation(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35530359/