迭代计算集合或向量的幂集
Iteratively calculate the power set of a set or vector
虽然有很多关于如何生成集合的实际幂集的例子,但我找不到任何关于迭代(如std::iterator
)生成幂集的东西。我之所以喜欢这样的算法,是因为我的基集的大小。由于n元素集的幂集有2^n个元素,所以在实际计算该集时,我会很快耗尽内存。那么,有什么方法可以为给定集的幂集创建迭代器吗?这可能吗?
- 如果更容易的话,创建
int
集合的迭代器就可以了——我可以将它们用作实际集合/向量的索引 - 由于我实际上在
std::vector
上工作,如果必要的话,随机访问是可能的
使用Combinations and Permutations中的for_each_combination
,可以轻松地遍历std::vector<AnyType>
的幂集的所有成员。例如:
#include <vector>
#include <iostream>
#include "../combinations/combinations"
int
main()
{
std::vector<int> v{1, 2, 3, 4, 5};
std::size_t num_visits = 0;
for (std::size_t k = 0; k <= v.size(); ++k)
for_each_combination(v.begin(), v.begin()+k, v.end(),
[&](auto first, auto last)
{
std::cout << '{';
if (first != last)
{
std::cout << *first;
for (++first; first != last; ++first)
std::cout << ", " << *first;
}
std::cout << "}n";
++num_visits;
return false;
});
std::cout << "num_visits = " << num_visits << 'n';
}
这访问vector
的每个幂集成员,并执行函子,函子只计算访问次数并打印出当前幂集:
{}
{1}
{2}
{3}
{4}
{5}
{1, 2}
{1, 3}
{1, 4}
{1, 5}
{2, 3}
{2, 4}
{2, 5}
{3, 4}
{3, 5}
{4, 5}
{1, 2, 3}
{1, 2, 4}
{1, 2, 5}
{1, 3, 4}
{1, 3, 5}
{1, 4, 5}
{2, 3, 4}
{2, 3, 5}
{2, 4, 5}
{3, 4, 5}
{1, 2, 3, 4}
{1, 2, 3, 5}
{1, 2, 4, 5}
{1, 3, 4, 5}
{2, 3, 4, 5}
{1, 2, 3, 4, 5}
num_visits = 32
我上面使用的语法是C++14。如果你有C++11,你需要更改:
[&](auto first, auto last)
至:
[&](std::vector<int>::const_iterator first, std::vector<int>::const_iterator last)
如果您在C++98/03中,则必须编写一个函子或函数来替换lambda。
for_each_combination
函数不分配额外的存储空间。这一切都是通过将CCD_ 8的成员交换到范围CCD_。在对for_each_combination
的每次调用结束时,向量保持其原始状态。
如果出于某种原因想要提前"退出"for_each_combination
,只需返回true
而不是false
。
相关文章:
- 计算排序向量的向量中唯一值的计数
- C++ 中的特征向量计算
- C++ 带有向量的 IF 计算操作数
- 如何计算向量[5][1] N次
- 如何在向量中某个数字之后重新计算进程
- 计算数组的特征值/向量,而不是使用特征 3 计算矩阵
- 如何使用count_if计算向量中的可变数字范围
- 如何计算单个元素在向量<float>中所有元素之和中的百分比
- 如何以C++为单位计算向量中的字节数?
- 通过将文本文件读取为字符串/向量来计算加权/未加权 GPA
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 计算 cv::P oints2f 向量中的重复项
- 如何计算 2D 数组中每行的总和并放置在向量中
- 计算向量中大于数字的元素
- 使用向量计算到期日期
- 通过查找重复顶点从 vec3 对象的向量计算索引
- 用stl向量计算复杂度
- OBB在第二次通过"Assertion Failed"时崩溃的特征向量计算
- 用C++中的向量计算大整数
- 如何使用向量计算点之间的距离