【发布时间】:2016-01-19 00:20:01
【问题描述】:
在一次大学练习中,我被要求编写一个模板函数“print();”,它有两个参数,1:一个泛型类型的数组,2:一个指定数组大小的 int。然后该函数应将数组中的每个项目打印到控制台。我在函数参数方面遇到了一些问题。我目前拥有的代码是:
template <typename Type>
Type print (Type a, Type b)
{
Type items;
Type array;
a = array;
b = items;
for (int i = 0; i < items; i++) {
std::cout << std::endl << "The element of the index " << i << " is " << array << std::endl;
std::cout << std::endl;
}
在 main() 中:
print(Array[], 10);
显然,将 Array 作为参数并没有返回值,所以我不确定还能做什么。有什么想法吗?
【问题讨论】:
-
是否可以传递指针(在数组到指针衰减之后)还是要通过引用获取数组?
-
(在第一种情况下你也可以传递非静态数组)
-
抱歉,我不确定,我在这个问题中只写了教科书问我的内容。
标签: c++ arrays function template-function