本文介绍了如何通过引用传递向量数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <vector>
using std::vector;
int main(void)
{
vector<int> gr[10];
function(gr);
}
我该如何定义通过引用而不是按值调用的函数?
How should I define that function calling by reference rather than by value?
推荐答案
以供参考:
void foo( vector<int> const (&v)[10] )
如果 foo
要修改实际参数,则没有 const
.
Or sans const
if foo
is going to modify the actual argument.
要避免使用由内而外的原始C语法进行声明的问题,可以执行以下操作:
To avoid the problems of the inside-out original C syntax for declarations you can do this:
template< size_t n, class Item >
using raw_array_of_ = Item[n];
void bar( raw_array_of_<10, vector<int>> const& v );
但是,如果您在 main
函数中使用了 std :: array
,则可以执行以下操作:
However, if you had used std::array
in your main
function, you could do just this:
void better( std::array<vector<int>, 10> const& v );
但是此函数不接受原始数组作为参数,仅接受 std :: array
.
But this function doesn't accept a raw array as argument, only a std::array
.
这篇关于如何通过引用传递向量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!