是否可以通过一个模板函数实现列操作和行操作?
Is it possible to realize column-wise operation and row-wise operation by one template function?
这是我的问题,我正在处理n维数据。为简单起见,假设 n=2。我还有一个用于 1D 数据的算法。为了将这个算法扩展到 2D 问题,我可以做
for each row
apply algorithm
但是,如果我想对每一列应用此算法,则需要编写一个新函数
for each column
apply algorithm
例如,假设我有一个函数:
void func(vector<T> &vec);
然后,要将这个函数应用于向量,我可以简单地调用这个函数:
vector<T> vec;
func(vec);
对于 3D 数据:
T multiArray[l][m][n];
据我所知,如果我想将上述函数应用于第一维中的所有向量,我会做:
for(int j=0;j<m;j++){
for(int k=0;k<n;k++){
vector<T> singleArray;
for(int i=0;i<l;i++){
singleArray.push_back(multiArray[i][j][k]);
}
func(singleArray);
}
}
但是,对于相同的数据,如果我想将上述函数应用于第三维中的所有向量,则需要将其重写为:
for(int i=0;i<l;i++){
for(int j=0;j<m;j++){
vector<T> singleArray;
for(int k=0;k<n;k++){
singleArray.push_back(multiArray[i][j][k]);
}
func(singleArray);
}
}
基本上,除了每个 for 循环中的迭代器之外,一切都是一样的。我希望有一些方法可以用一个函数实现这两个计算。
感谢
我不知道一般的解决方案,但您可以使用引用解决您的特定问题(使用第一个,第二个或第三个索引或...)。
在3D情况下,首先可以声明循环变量(i
、j
和k
)
std::size_t i, j, k;
接下来,你可以根据模板值"链接"另一个变量(r
)i
、j
或k
I
std::size_t & r = (I == 0U ? i : (I == 1U ? j : k));
下面是一个可编译的示例
#include <vector>
#include <iostream>
template <std::size_t I>
void func (std::vector<std::vector<std::vector<double> > > & d)
{
std::size_t i, j, k;
std::size_t & r = (I == 0U ? i : (I == 1U ? j : k));
for ( i = 0U ; i < d.size() ; ++i )
for ( j = 0U ; j < d[i].size() ; ++j )
for ( k = 0U ; k < d[i][j].size() ; ++k )
d[i][j][k] += r+1;
}
int main()
{
std::vector<std::vector<std::vector<double> > > data;
// some data in data
func<0>(data); // r is i
func<1>(data); // r is j
func<2>(data); // r is k
}
---编辑---
OP问
无论如何,这个函数可以适用于任意维度吗?
不。
不是这个函数。
但我提出了一个完全不同的(也是更复杂的)解决方案。我写了它,但不要求我检查它是否真的有效。
这个想法不再基于参考,而是基于模板专业化。
这次的模板索引是从 1 开始的:如果要截取第一个索引(exx
),2
如果要截取第二个索引(exy
),请使用模板值1
等。
所以你打电话
foo<1U>(data1); // r is the first index
对于一维矢量,
foo<1U>(data2); // r is the first index
foo<2U>(data2); // r is the second index
用于 2D 矢量等。
如果您致电
foo<I>(data)
如果I
大于data
的维度,则会出现编译错误。
如果您致电
foo<0>(data)
您会收到编译错误,但仅当您编译 C++11 或更高版本(C++98r
变为零;但您可以添加assert()
以获取运行时错误)。
示例
#include <vector>
#include <iostream>
template <std::size_t I>
struct bar
{
template <typename T>
static void baz (std::vector<T> & v, std::size_t)
{
for ( std::size_t i = 0U ; i < v.size() ; ++i )
bar<I-1U>::baz(v[i], i);
}
};
template <>
struct bar<0U>
{
template <typename T>
static void baz (std::vector<T> & v, std::size_t r)
{
for ( std::size_t i = 0U ; i < v.size() ; ++i )
baz(v[i], r);
}
static void baz (double & d, std::size_t r)
{ d += r + 1U; }
};
template <std::size_t I, typename V>
void foo (V & v)
{
#if __cplusplus >= 201103L
static_assert(I > 0U, "!"); // c++11 or newer
#endif
bar<I>::baz(v, 0U);
}
int main()
{
std::vector<double > data1;
std::vector<std::vector<double> > data2;
std::vector<std::vector<std::vector<double> > > data3;
// some data in data1, data2 and data3
// foo<0U>(data1); // compilation error in C++11 or newer
foo<1U>(data1); // r is the first index
// foo<2U>(data1); // compilation error
// foo<0U>(data2); // compilation error in C++11 or newer
foo<1U>(data2); // r is the first index
foo<2U>(data2); // r is the second index
// foo<3U>(data2); // compilation error
// foo<0U>(data3); // compilation error in C++11 or newer
foo<1U>(data3); // r is the first index
foo<2U>(data3); // r is the second index
foo<3U>(data3); // r is the third index
// foo<4U>(data3); // compilation error
}
相关文章:
- 我可以在 C++ 中的函数体之外进行操作吗?
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 无法调用成员函数,尝试正确执行此操作仍然失败
- 不执行任何操作的函数调用C++
- 如何实现对序列对进行操作的C++元函数
- 某些 boost::asio 异步函数是否将处理程序连接到操作,以便处理程序被触发一次?
- 为表示一个或多个操作的C++函数的int参数寻找类型安全的替换
- 在函数中操作 std::array
- 避免通过操作从私有构造函数间接实例化
- 如果普通默认构造函数不执行任何操作,为什么我们不能使用 malloc 创建平凡可构造的对象?
- 如何检测函数是否执行IO操作
- 如何将操作委托给函数返回
- 在MySql中使用字符串操作函数有什么问题
- Rcpp 中"List"的操作函数 - 类似于 R 中的"Reduce function"
- 无操作函数模板
- 按位操作函数
- 当人们想操作函数中的指针时,为什么要传入双指针
- 如何在c++中操作函数以根据条件输出不同的结果
- 使用std::map时必须互斥的所有操作/函数是什么?