如何传递以 "size determined at run time" 作为引用的动态分配数组?
How to pass a dynamically allocated array with "size determined at run time" as a reference?
我知道如何将一个常量大小的数组作为引用传递,但我想知道如何将一个可变大小的数组作为对另一个函数的引用传递。任何帮助将不胜感激。谢谢
例如,我有以下代码片段:
void y(int (&arr)[n]) //Gives error
{}
void x(Node * tree, int n)
{
int arr[n];
y(arr);
}
我听说我们可以模板化函数并使大小成为模板参数,但我无法这样做。
很简单:不要。请改用std::array
或std::vector
:
int get_max(std::vector<int> & vec) {//Could use const& instead, if it doesn't need to be modified
int max = std::numeric_limits<int>::min();
for(int & val : vec) {if(max < val) max = val;
return max;
}
int get_max(std::array<int, 20> & arr) {//Could use const& instead
int max = std::numeric_limits<int>::min();
for(int & val : arr) {if(max < val) max = val;
return max;
}
如果您希望这适用于任何std::array
或任何std::vector
,您可以像这样模板化它们:
template<typename T>
T get_max(std::vector<T> const& vec) {
if(vec.size() == 0) throw std::runtime_error("Vector is empty!");
T const* max = &vec[0];
for(T const& val : vec) if(*max < val) max = &val;
return *max;
}
template<typename T, size_t N>
T get_max(std::array<T, N> const& arr) {
static_assert(N > 0, "Array is empty!");
T * max = &arr[0];
for(T & val : arr) if(*max < val) max = &val;
return *max;
}
您的代码现在应如下所示以补偿:
void y(std::vector<int> & arr) //Can be const& if you don't need to modify it.
{}
void x(Node * tree, int n)
{
std::vector<int> arr(n); //Will initialize n elements to all be 0.
y(arr);
}
这个答案是为了说明在将VLA作为函数参数传递时,如何在C++中使用VLA。
在 c99 中,语法允许您将数组的大小作为参数传递给函数,并使用函数参数声明 VLA 的大小:
void y (int n, int (*arr)[n])
{}
void x (int n)
{
int arr[n];
y(n, &arr);
}
C++使用"函数名称重整"作为一种技术,将函数接受的参数类型编码到函数名称中,以支持函数重载。但是,在 GCC 中,由于 VLA 不是C++支持的功能,因此没有重整约定。有人可能会说这是一个 G++ 错误(或对 VLA 扩展的不完整支持(,但它就是这样。若要模拟按引用传递,请接受衰减的指针作为参数,并将其强制转换为对 VLA 的引用。
void y(int n, int *x)
{
int (&arr)[n] = reinterpret_cast<int (&)[n]>(*x);
}
void x(int n)
{
int arr[n];
y(n, arr);
}
我已经验证了这适用于 GCC 4.8。
Xirema已经提到了如何使用std::vector
/std::array
解决此问题。
我不知道你的确切情况,所以只会描述另一种选择,尽管事实上,std::vector
/std::array
是最好的。
指针选项
在这里,你们相信,y
的arr
和n
论点是一致的。并手动处理arr
尺寸。
void y(int * arr, const int n) {}
void x(Node * tree, int n) {
int arr[n];
y(arr, n);
}
模板选项
这将起作用,它将在每个新N
值上实例化 2 个模板。
template <size_t N>
void y(int (&arr)[N]) {}
template <size_t N>
void x(Node * tree) {
int arr[N];
y<N>(arr);
}
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 在c++中使用动态分配的问题
- 使用递归模板动态分配的多维数组
- 对具有动态分配的内存和析构函数的类对象的引用
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 访问动态分配列表中的元素
- 为什么 std::equal_to会导致动态分配?
- 调用析构函数以释放动态分配的内存
- 通过引用和动态分配传递向量
- 在动态分配的结构中初始化引用
- 对动态分配的变量的引用与指向相同动态分配变量的指针
- 通过引用返回/传递动态分配的对象
- 动态分配的数组通过引用传输到函数
- 引用变量可以访问动态分配的内存
- 如何在c++中引用动态分配的字符数组
- 返回对动态分配对象的引用
- c++从动态分配转换为引用
- 对动态分配对象的引用 ( "at run-time" )
- 从动态分配的解引用指针默认初始化非const引用函数参数是否会造成内存泄漏?