我在这里看到:http ://www.cplusplus.com/doc/tutorial/arrays/ int[N] 与std::array<int,N>
C++ 中的相同。我想使用这种表示法以避免将 N 作为函数的参数传递。
我想做这样的事情
returnedType function(array tab)
代替
returnedType function(int tab, int N)
但我不能制作类型数组,因为我必须写,array<int,N>
而且我事先不知道 N。有人有解决方案吗?
使function
a函数template
,像这样:
template <size_t N>
void function(std::array<int, N> arr)
{
// do something with arr
}
并称之为:
int main()
{
std::array<int, 3> a;
function(a);
std::array<int, 15> b;
function(b);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句