我不知道一般的解决方案,但您可以使用参考解决您的特定问题(使用第一个、第二个或第三个索引或......)。
在 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 开始的:如果要截取第一个索引 (ex ),则使用模板值1
,x
如果2
要截取第二个索引 (ex y
),等等。
所以你打电话
foo<1U>(data1); // r is the first index
对于一维向量,
foo<1U>(data2); // r is the first index
foo<2U>(data2); // r is the second index
对于二维向量等。
如果你打电话
foo<I>(data)
whereI
大于 的维度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
}