优雅的方式来初始化非静态数组成员 [英] Elegant way to initialize non-static array member
问题描述
考虑以下(*)。有没有办法重写它以便
保持方便(当数组v被修改时N被重新计算)
*和*编译:)
谢谢,
-Mathieu
(*)
模板< typename T,unsigned int N>
struct Functor
{
T值[N];
};
int main()
{
const double v [] = {0,1,4,9,16,25,36};
const unsigned int N = sizeof(v)/ sizeof(v [0]);
Functor< double,Nf = v; //将无法编译
// Functor< double,7f = {{0,1,4,9,16,25,36}}; //需要
手动计算7 ...
返回0;
}
>
推荐答案
11月6日下午3:32,mathieu< mathieu.malate ... @ gmail.comwrote:
On Nov 6, 3:32 pm, mathieu <mathieu.malate...@gmail.comwrote:
考虑以下(*)。有没有办法重写它以便
保持方便(当数组v被修改时N被重新计算)
*和*编译:)
谢谢,
-Mathieu
(*)
模板< typename T,unsigned int N>
struct Functor
{
T值[N];
};
int main()
{
const double v [] = {0,1,4,9,16,25 ,36};
const unsigned int N = sizeof(v)/ sizeof(v [0]);
Functor< double,Nf = v; //将无法编译
// Functor< double,7f = {{0,1,4,9,16,25,36}}; //需要
手动计算7 ...
返回0;
}
Hi,
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)
Thanks,
-Mathieu
(*)
template <typename T, unsigned int N>
struct Functor
{
T values[N];
};
int main()
{
const double v[] = {0, 1, 4, 9, 16, 25, 36 };
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = v; // will not compile
//Functor<double,7f = { {0, 1, 4, 9, 16, 25, 36 } }; // need to
compute 7 by hand...
return 0;
}
超级丑陋的解决方案:
#define V {0,1,4,9,16,25,36}
int main()
{
const double v [] = V;
const unsigned int N = sizeof( v)/ sizeof(v [0]);
Functor< double,Nf = {V};
....
}
Super-ugly solution:
#define V {0, 1, 4, 9, 16, 25, 36 }
int main()
{
const double v[] = V;
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = { V };
....
}
mathieu写道:
mathieu wrote:
考虑以下(*)。有没有办法重写它以便
保持方便(当数组v被修改时N被重新计算)
*和*编译:)
谢谢,
-Mathieu
(*)
模板< typename T,unsigned int N>
struct Functor
{
T值[N];
};
Hi,
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)
Thanks,
-Mathieu
(*)
template <typename T, unsigned int N>
struct Functor
{
T values[N];
};
添加此函数:
模板< class T,unsigned NFunctor< T,NmakeFunctor(T(& a)) [N])
{
Functor< T,Nf;
std :: copy(a,a + N,f.values) ;
返回f;
}
Add this function:
template<class T, unsigned NFunctor<T,NmakeFunctor(T (&a)[N])
{
Functor<T,Nf;
std::copy(a, a+N, f.values);
return f;
}
>
int main( )
{
const double v [] = {0,1,4,9,16,25,36};
const unsigned int N = sizeof(v)/ sizeof(v [0]);
Functor< double,Nf = v; //将无法编译
// Functor< double,7f = {{0,1,4,9,16,25,36}}; //需要
手动计算7 ...
>
int main()
{
const double v[] = {0, 1, 4, 9, 16, 25, 36 };
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = v; // will not compile
//Functor<double,7f = { {0, 1, 4, 9, 16, 25, 36 } }; // need to
compute 7 by hand...
Functor< double,Nf = makeFunctor(v);
Functor<double,Nf = makeFunctor(v);
>
返回0;
}
>
return 0;
}
最终代码:
模板< typename T,unsigned int N>
struct Functor
{
T值[N];
};
#include< algorithm>
template< typename T, unsigned int N>
Functor< T,NmakeFunctor(T const(& a)[N])
{
Functor< T, Nf;
std :: copy(a,a + N,f.values);
返回f;
}
int main()
{
const double v [] = {0,1,4,9,16,25,36} ;
const unsigned int N = sizeof(v)/ sizeof(v [0]);
Functor< double,Nf = makeFunctor(v);
返回0;
}
V
-
请删除资金''A'通过电子邮件回复
我没有回复最热门的回复,请不要问
Final code:
template <typename T, unsigned int N>
struct Functor
{
T values[N];
};
#include <algorithm>
template <typename T, unsigned int N>
Functor<T,NmakeFunctor(T const (&a)[N])
{
Functor<T,Nf;
std::copy(a, a + N, f.values);
return f;
}
int main()
{
const double v[] = {0, 1, 4, 9, 16, 25, 36 };
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = makeFunctor(v);
return 0;
}
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
11月6日,3:晚上51点,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Nov 6, 3:51 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu写道:
mathieu wrote:
Hi,
考虑以下(*)。有没有办法重写它以便
保持方便(当数组v被修改时N被重新计算)
*和* compiles :)
Consider the following (*). Is there a way to rewrite it so that it
remains convenient (N is being recomputed when array v is modified)
*and* compiles :)
....
....
最终代码:
template< typename T,unsigned int N>
struct Functor
{
T values [N];
};
#include< algorithm>
模板< typename T,unsigned int N>
Functor< T ,NmakeFunctor(T const(& a)[N])
{
Functor< T,Nf;
std :: copy( a,a + N,f.values);
返回f;
}
int main()
{
const double v [] = {0,1,4,9,16,25,36};
const unsigned int N = sizeof (v)/ sizeof(v [0]);
Functor< double,Nf = makeFunctor(v);
返回0;
}
Final code:
template <typename T, unsigned int N>
struct Functor
{
T values[N];
};
#include <algorithm>
template <typename T, unsigned int N>
Functor<T,NmakeFunctor(T const (&a)[N])
{
Functor<T,Nf;
std::copy(a, a + N, f.values);
return f;
}
int main()
{
const double v[] = {0, 1, 4, 9, 16, 25, 36 };
const unsigned int N = sizeof(v) / sizeof(v[0]);
Functor<double,Nf = makeFunctor(v);
return 0;
}
美观简单!
谢谢,
-Mathieu
nice and simple !
Thanks,
-Mathieu
这篇关于优雅的方式来初始化非静态数组成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!