c++ - powerset 中的组合或子集的 next_permutation

标签 c++ permutation combinations powerset

是否有一些等效的库或函数可以为我提供一组值的下一个组合,例如 next_permutation 对我有帮助吗?

最佳答案

组合:来自 Mark Nelson 关于同一主题的文章,我们有 next_combination http://marknelson.us/2002/03/01/next-permutation
排列:从 STL 我们有 std::next_permutation

 template <typename Iterator>
 inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
 {
    if ((first == last) || (first == k) || (last == k))
       return false;
    Iterator itr1 = first;
    Iterator itr2 = last;
    ++itr1;
    if (last == itr1)
       return false;
    itr1 = last;
    --itr1;
    itr1 = k;
    --itr2;
    while (first != itr1)
    {
       if (*--itr1 < *itr2)
       {
          Iterator j = k;
          while (!(*itr1 < *j)) ++j;
          std::iter_swap(itr1,j);
          ++itr1;
          ++j;
          itr2 = k;
          std::rotate(itr1,j,last);
          while (last != j)
          {
             ++j;
             ++itr2;
          }
          std::rotate(k,itr2,last);
          return true;
       }
    }
    std::rotate(first,k,last);
    return false;
 }

关于c++ - powerset 中的组合或子集的 next_permutation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2685501/

相关文章:

c++ - 在 opencv 中将 bgr 转换为 hsv 时出现 block 状行为

algorithm - 生成所有长度为 n 且设置了 k 位的二进制字符串

math - 可能有多少种不同的组合?

python - 在列表中找到 X 个数字的总和 (Python)

java - 查找可能的组合数

c++ if(cin>>input) 在 while 循环中不能正常工作

进行文件操作时,C++ 程序在 Windows 和 Linux 上运行速度慢得多?

c++ - 如何在 glsl 中编写基本着色器?

C++:难以掌握 std::next_permutation 和 std::prev_permutation 的工作

swift - 使用 Swift 多线程组合数组中的项目