简体   繁体   English

如何将数组对象(Class Template c ++)作为参数传递给函数?

[英]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. 在所有情况下,编译器都应能够推断出正确的模板参数。

Make the function a template to avoid hard coding it for the size: 使函数成为模板,以避免对其进行硬编码以适应大小:

template<size_t S>
void f_print(array<int, S> object){}

See a Live Demo . 观看现场演示

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.

相关问题 如何在C ++中将std :: array作为具有可变数量元素的模板参数传递? - How do I pass std::array as a template parameter with a varying number of elements in C++? 如何将函数指针作为类模板参数传递? - How do I pass a function pointer as a class template parameter? 如何在C ++中将类(不是对象)作为参数传递 - How to pass a class (not an object) as a parameter in C++ 如何将c ++对象作为参数传递给函数? - How can I pass a c++ object as a parameter to function? C ++模板类函数作为模板参数 - C++ Template class function as template parameter 如何将二维数组传递给 function (C++)? - How do I pass a 2D array into a function (C++)? 如何将右值引用参数传递给 C++ 中的模板运算符() function? - How to pass a rvalue reference parameter to a template operator() function in C++? 将函数模板作为参数传递给C ++ - Pass a function template as a parameter in C++ 我可以通过模板c ++将对象传递给类吗 - can I pass an object to a class via template c++ 如何在创建对象时传递自定义比较器函数以及如何在 C++ 的自定义父类中使用它? - How can I pass a custom comparator function while object creation and how do I use it in the custom parent class in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM