c++ - 是否可以在 STL set<t> 上实现 next_permutation()

标签 c++ stl permutation

给定这个集合

   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/

相关文章:

c++ - 什么是最好的 "type renaming"方法?

c++ - STL+内存管理问题

algorithm - 如何找到两个不同排列之间的最短交换序列?

python - 在python中获得一个范围的所有排列而无需连续邻居的最快方法

python - 在 Python 中不重复输出的排列

c++ - 标准库实现中 __builtin_ 前缀的含义是什么?

android - 使用 OpenGL 和嵌入式 Webviews 的原生移动应用程序 (iOS/Android) 测试框架

c++ - 如何实现这个程序

c++ - vector 结束迭代器

c++ - 使用 STL/Boost 初始化硬编码集<vector<int>>