[英]How do I Pass an array object (Class Template c++) to a function as a parameter?
If I declare an array (using c++ Class Template) like this, 如果我这样声明一个数组(使用c ++类模板),
#include <array> //standard c++ library
int main()
{
array<int,3> myarray{10,20,30};
}
How will I be able to send this array object to a function as a parameter. 如何将这个数组对象作为参数发送给函数。
If I pass like this it works : 如果我这样通过,它会起作用:
void f_print(array<int, 3> object){}
int main()
{
array<int, 3> myarray{10,20,30};
f_print(myarray);
}
but the problem here is I am hard coding the size in the function. 但是这里的问题是我很难在函数中编码大小。 The whole point to work like this to use array as an object type so I can use array.size().
像这样工作的要点是将array用作对象类型,因此我可以使用array.size()。 So How can I pass this array type object (c++ Class Template) without hardcoding the size.
因此,如何在不对大小进行硬编码的情况下传递此数组类型对象(c ++类模板)。
Edit_1: Why can't I send the array object as only an object where I use all the properties of the object inside the function parameter. Edit_1:为什么我不能将数组对象仅作为对象使用,而在函数参数中使用了该对象的所有属性。
You can template either the array type 您可以模板化数组类型
template<typename T>
void f_print(std::array<T, 3> const& object);
Or the size 或大小
template<size_t S>
void f_print(std::array<int, S> const& object);
Or both: 或两者:
template<typename T, size_t S>
void f_print(std::array<T, S> const& object);
The compiler should be able to deduce the correct template arguments in all the cases. 在所有情况下,编译器都应能够推断出正确的模板参数。
I think you are missing the point of arrays. 我认为您缺少数组的要点。 It's in the very nature of an array that its size belongs to its type .
从本质上说,数组的大小属于其类型 。 That's the case for raw arrays and it's the case for
std::array
as well. 原始数组就是这种情况,
std::array
也是如此。 Hard-coding the size 3 of std::array<int, 3>
in the function is as unavoidable as hard-coding the int
. 在函数中硬编码
std::array<int, 3>
的大小3与硬编码int
一样是不可避免的。
In fact, in my experience, the best way to think about an array like for example int[4]
or std::array<int, 4>
is as a shorthand for int a, b, c, d
. 实际上,以我的经验, 考虑诸如
int[4]
或std::array<int, 4>
类的数组的最佳方法是int a, b, c, d
的简写。
I'd say a template only circumvents the issue in a very superficial way. 我想说模板只能以非常肤浅的方式规避问题。 If you create a template with a size parameter, then you only end up with a lot of different functions for different array types.
如果使用size参数创建模板,那么对于不同的数组类型,最终只会得到很多不同的功能。
std::array<int, 3>
, std::array<int, 4>
, std::array<int, 5>
and so on all result in different template instantiations. std::array<int, 3>
, std::array<int, 4>
, std::array<int, 5>
等都导致不同的模板实例化。 It's in the same way as you end up with different template instantiations for std::array<int, 3>
, std::array<double, 3>
and std::array<std::string, 3>
. 这与您以不同的模板实例化
std::array<int, 3>
, std::array<double, 3>
和std::array<std::string, 3>
模板实例化相同。
Mind that turning the size into a template parameter may still be the solution you are actually looking for. 请注意,将大小转换为模板参数可能仍然是您真正要寻找的解决方案。 If, however, you need a container for which the size is not known at compile time, then use
std::vector
. 但是,如果需要一个在编译时未知大小的容器,请使用
std::vector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.