给定一个向量,如何找到该向量的所有子集和的原始索引
Given a vector, how to find all subsets of the vector and the original indices of
给定一个向量,如何从该向量中找到伴随原始索引的向量的所有子集?
例如,给定矢量
vector<float> numbers {1.3, 0.5, 2.4};
获取子集:
{}, {1.3}, {0.5}, {2.4}, {1.3, 2.4}, {0.5, 2.4}, {1.3, 0.5}, {1.3, 0.5, 2.4}
以及每个子集的相应索引:
{}, {0}, {1}, {2}, {0, 2}, {1, 2}, {0, 1}, {0, 1, 2}.
这是家庭作业吗?:-P
以下函数将生成索引子集。indices
参数只是一个临时的临时变量。
void getIndexSubsets(
int l, int u, std::vector<int>* indices,
std::vector<std::vector<int>>* result) {
if (l == u) {
result->push_back(*indices);
} else {
int next = l + 1;
getIndexSubsets(next, u, indices, result);
indices->push_back(l);
getIndexSubsets(next, u, indices, result);
indices->pop_back();
}
}
它是这样使用的:
std::vector<float> numbers{1.3, 0.5, 2.4};
std::vector<int> indices;
std::vector<std::vector<int>> indexResult;
getIndexSubsets(0, numbers.size(), &indices, &indexResult);
索引子集将在indexResult
中输出。给定索引子集,我们可以使用以下函数计算值子集:
std::vector<std::vector<float>> getValueSubsets(
const std::vector<float>& srcValues,
const std::vector<std::vector<int>>& src) {
std::vector<std::vector<float>> dst;
for (const auto& inds: src) {
std::vector<float> x;
for (auto i: inds) {
x.push_back(srcValues[i]);
}
dst.push_back(x);
}
return dst;
}
这样调用:
std::vector<std::vector<float>> valueResult = getValueSubsets(numbers, indexResult);
完整的解决方案如下所示。
#include <iostream>
#include <vector>
void getIndexSubsets(
int l, int u, std::vector<int>* indices,
std::vector<std::vector<int>>* result) {
if (l == u) {
result->push_back(*indices);
} else {
int next = l + 1;
getIndexSubsets(next, u, indices, result);
indices->push_back(l);
getIndexSubsets(next, u, indices, result);
indices->pop_back();
}
}
std::vector<std::vector<float>> getValueSubsets(
const std::vector<float>& srcValues,
const std::vector<std::vector<int>>& src) {
std::vector<std::vector<float>> dst;
for (const auto& inds: src) {
std::vector<float> x;
for (auto i: inds) {
x.push_back(srcValues[i]);
}
dst.push_back(x);
}
return dst;
}
template <typename T>
std::ostream& operator<<(std::ostream& s, const std::vector<T>& src) {
s << "{";
bool f = true;
for (auto x: src) {
s << (f? "" : " ") << x;
f = false;
}
s << "}";
return s;
}
int main() {
std::vector<float> numbers{1.3, 0.5, 2.4};
std::vector<int> indices;
std::vector<std::vector<int>> indexResult;
getIndexSubsets(0, numbers.size(), &indices, &indexResult);
std::vector<std::vector<float>> valueResult = getValueSubsets(numbers, indexResult);
for (int i = 0; i < indexResult.size(); i++) {
std::cout << "Subset inds=" << indexResult[i] << " values=" << valueResult[i] << std::endl;
}
return 0;
}
执行时,它将输出以下内容:
Subset inds={} values={}
Subset inds={2} values={2.4}
Subset inds={1} values={0.5}
Subset inds={1 2} values={0.5 2.4}
Subset inds={0} values={1.3}
Subset inds={0 2} values={1.3 2.4}
Subset inds={0 1} values={1.3 0.5}
Subset inds={0 1 2} values={1.3 0.5 2.4}
相关文章:
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 如何获得比较两个向量对的子集
- 在 Rcpp-Function 中使用布尔向量的子集(Rcpp 初学者的问题..)
- C 从向量的子集返回一个随机值
- 如何在满足谓词的向量中获得唯一PTR的子集
- 向量的子集/C++/STL
- 通过单个向量进行迭代并删除作为其他元素子集的元素
- 将函数应用于向量子集的最佳方法是什么
- 确定一个向量是否是另一个向量的子集的有效方法
- 对定义顺序的向量的子集进行高效排序
- 如何将向量拆分为子集
- 子集的向量
- 赋值给向量的跨步子集
- C++:读取数据集并检查向量是否<Class>是向量的<Class>子集
- 从向量生成元素的子集(无嵌套循环)
- C++ 无效的向量 T 子集
- 向量,包含指向同一内存的另一个向量的部分子集
- 如何从给定的向量中检测所有可能的子集
- 将向量的子集<uint8_t>转换为整数
- 计数在向量子集中的出现次数