TMP:如何推广向量的笛卡尔积
TMP: how to generalize a Cartesian Product of Vectors?
对于整数向量的向量的笛卡尔积,有一个很好的C++解(实际上是 2 个解:递归和非递归)。 为了说明/简单起见,让我们只关注非递归版本。
我的问题是,如何使用模板概括此代码以获取如下所示的齐次向量std::tuple
:
{{2,5,9},{"foo","bar"}}
并生成均匀的tuple
载体
{{2,"foo"},{2,"bar"},{5,"foo"},{5,"bar"},{9,"foo"},{9,"bar"}}
如果它使生活更轻松,让我们假设输入中的内部向量都是齐次的。 所以不允许这样的输入:{{5,"baz"}{'c',-2}}
编辑将输入从锯齿向量更改为元组
更简单的递归解决方案。它将向量作为函数参数,而不是元组。此版本不构建临时元组,而是使用 lambda。现在它不会进行不必要的复制/移动,并且似乎已成功优化。
#include<tuple>
#include<vector>
// cross_imp(f, v...) means "do `f` for each element of cartesian product of v..."
template<typename F>
inline void cross_imp(F f) {
f();
}
template<typename F, typename H, typename... Ts>
inline void cross_imp(F f, std::vector<H> const& h,
std::vector<Ts> const&... t) {
for(H const& he: h)
cross_imp([&](Ts const&... ts){
f(he, ts...);
}, t...);
}
template<typename... Ts>
std::vector<std::tuple<Ts...>> cross(std::vector<Ts> const&... in) {
std::vector<std::tuple<Ts...>> res;
cross_imp([&](Ts const&... ts){
res.emplace_back(ts...);
}, in...);
return res;
}
#include<iostream>
int main() {
std::vector<int> is = {2,5,9};
std::vector<char const*> cps = {"foo","bar"};
std::vector<double> ds = {1.5, 3.14, 2.71};
auto res = cross(is, cps, ds);
for(auto& a: res) {
std::cout << '{' << std::get<0>(a) << ',' <<
std::get<1>(a) << ',' <<
std::get<2>(a) << "}n";
}
}
自从我这样做以来已经有一段时间了,但这是第一次尝试。毫无疑问,它是可以改进的。
template<unsigned fixedIndex, class T>
class DynamicTupleGetter
{
typedef typename std::tuple_element<fixedIndex, T>::type RetType;
public:
static RetType get(unsigned dynIndex, const T& tupleInstance)
{
const RetType& ret = std::get<fixedIndex>(tupleInstance);
if (fixedIndex == dynIndex)
return ret;
return DynamicTupleGetter<fixedIndex - 1, T>::get(dynIndex, tupleInstance);
}
};
template<class T>
class DynamicTupleGetter<0, T>
{
typedef typename std::tuple_element<0, T>::type RetType;
public:
static RetType get(unsigned dynIndex, const T& tupleInstance)
{
assert(dynIndex == 0);
return std::get<0>(tupleInstance);
}
};
template<class Source>
struct Converter
{
typedef typename std::tuple_element<0, Source>::type Zeroth;
typedef typename std::tuple_element<1, Source>::type First;
static const size_t size0 = std::tuple_size<Zeroth>::value;
static const size_t size1 = std::tuple_size<First>::value;
static const size_t outerProductSize = size0 * size1;
typedef typename std::tuple_element<0, Zeroth>::type BaseType0;
typedef typename std::tuple_element<0, First>::type BaseType1;
typedef typename std::tuple<BaseType0, BaseType1> EntryType;
typedef std::array<EntryType, outerProductSize> DestinationType;
DestinationType create(const Source& source)
{
Zeroth zeroth = std::get<0>(source);
First first = std::get<1>(source);
typedef typename DynamicTupleGetter<size0 -1, Zeroth> ZerothGetter;
typedef typename DynamicTupleGetter<size1 -1, First> FirstGetter;
DestinationType result;
size_t resultIndex = 0;
for(size_t i = 0; i < size0; ++i)
for(size_t j = 0; j < size1; ++j)
{
std::get<0>(result[resultIndex]) = ZerothGetter::get(i, zeroth) ;
std::get<1>(result[resultIndex]) = FirstGetter::get(j, first);
++resultIndex;
}
return result;
}
};
template<class T>
void create(const T& source)
{
Converter<T> converter;
Converter<T>::DestinationType result = converter.create(source);
std::cout << std::get<0>(std::get<3>(result)) << "," << std::get<1>(std::get<3>(result)) << std::endl;
}
auto intPart = std::make_tuple(2,5,9);
auto stringPart = std::make_tuple("foo","bar");
auto source = std::make_tuple(intPart, stringPart);
void f()
{
create(source);
}
相关文章:
- 在 OpenGL 中将笛卡尔世界坐标转换为球面局部坐标
- C++ 中unordered_map的有效笛卡尔积
- 谷歌测试:模板模板的笛卡尔乘积的类型列表与模板
- 使用多个输入向量中值的笛卡尔乘积调用 lambda
- 如何在C++中创建类型列表的 n 路笛卡尔积?
- c++17通过生成预先声明的类型列表的笛卡尔乘积来生成std::变体
- 用于将笛卡尔 (x,y,z) 转换为圆柱坐标 (ρ,θ,z) 坐标 2D/3D 的代码
- 将笛卡尔点存储在矢量和输出距离中
- 使用迭代器和可变参数模板的笛卡尔乘积
- 圆的面积 使用笛卡尔平面上的点
- 当元素数> 1000 时,如何制作向量的笛卡尔积?
- 使用提示用户输入笛卡尔平面中某些点的x-y坐标(浮点类型)的函数
- 有没有STL函数可以得到两个C++向量的笛卡尔乘积
- TMP:如何推广向量的笛卡尔积
- 就地生成 2 个向量的笛卡尔积<string>?
- 如何创建类型列表的笛卡尔积
- 如何遍历两个类似stl的容器(笛卡尔积)
- 如何使用笛卡尔积将向量元组转换为元组的向量
- 得到笛卡尔积的算法
- 多字符串的笛卡尔积