Reputation: 73
#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?
Upvotes: 2
Views: 8459
Reputation: 15501
To pass an array of ten int
s by reference you would use:
void foo(int (&gr)[10]);
Similarly, for an array of ten std::vector
s it would be:
void foo(std::vector<int> (&gr)[10]);
That being said an array of vectors is somewhat unusual structure. Prefer std::array
to raw arrays.
Upvotes: 4
Reputation: 17483
auto function(vector<int> (&gr)[10]) { ... }
It should be the right syntax to pass an array of std::vector<int>
by reference.
Upvotes: 5
Reputation: 145204
For pass by reference:
void foo( vector<int> const (&v)[10] )
Or sans const
if foo
is going to modify the actual argument.
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 );
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 );
But this function doesn't accept a raw array as argument, only a std::array
.
Upvotes: 7