struct st
{
int to, cost;
};
void fun(vector<st>&v1[10])
{
vector<st>v2[10];
v1=v2;
}
int main()
{
vector<st>arr[10];
fun(arr);
}
I want to pass a 2D vector in a function by reference and swap the vector with another vector in that function. But I am getting error. I don't want to do it with pair vector. I want to use structure here. How to do it?
One major problem: When passing an array as an argument, what is really passed is a pointer .
It can easily be solved by using std::array
instead:
void fun(std::array<std::vector<st>, 10>& v1)
{
std::array<std::vector<st>, 10> v2;
// Initialize v2...
v1 = v2;
}
int main()
{
std::array<std::vector<st>, 10> arr;
fun(arr);
}
After introducing std::array
above, I would rather recommend returning the array instead of passing by reference:
std::array<std::vector<st>, 10> fun()
{
std::array<std::vector<st>, 10> v2;
// Initialize v2...
return v2;
}
int main()
{
std::array<std::vector<st>, 10> arr = fun();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.