How to return a 2 dimensional vector?
I have a function that creates a 2D vector
void generate(int n)
{
vector< vector<int> > V (n, vector<int>(1 << n , 0 ));
.......
开发者_开发知识库}//n is used to determine the size of vector
Now, I need to return the created vector to use it in another function .If I did
return V ;
it will be wrong because V is a local variable but I can't define V outside the function because this functions defines the size of V . What should I do ?
You can return V
with no issues - it will return a copy of the local variable. Issues only arise when you return a reference or pointer to a variable with local scope; when the function ends, the local variable falls out of scope and is destroyed and the reference/pointer is no longer valid.
Alternatively, you can accept a reference to a vector as your argument, write to it and return void:
void generate(int n, std::vector< std::vector<int> >& vec) {
vec.resize(n, std::vector<int>(1 << n, 0));
}
int main() {
std::vector< std::vector<int> > v;
generate(10, v);
}
This is faster than returning a copy of the local member, which can be expensive for large objects such as multi-dimensional vectors.
vector<vector<int> > generate(int n)
{
vector<vector<int> > v(n, vector<int>(1 << n, 0));
//...
return v;
}
The return value is a copy of the local variable v
so there is no problem at all.
If you're concerned about copying the vector, maybe you could do something like this:
void generate(int n, vector<vector<int> >& v)
{
v.clear(); //not necessary if you're sure it's empty
v.resize(n, vector<int>(1 << n, 0));
//...
}
if the vector is small, just return it by value, it will be copied. if the vector is large, the caller can pass the dest vector by reference.
As alternative to other answers you can return pointer to heap allocated vector. For securing from memory leaks you can use "move constructor" idiom.
typedef auto_ptr<vector<vector<int> > > vector_ptr; vector_ptr generate(int n) { vector_ptr V(new vector<vector<int> >(n, vector<int>(1 << n , 0 ))); return V; } // ... vector_ptr V(generate(some_number));
This idiom can be used for any "heavy" object. If you whant to prolonge lifetime of returned object you can assign it to shared_ptr (as example boost::shared_ptr). But I think it is better return copy whenever it is possible, for minimizing complexity of your code.
Just like we do for other,we can do that here also.See my code for better explanation I have given my code for matrix multiplication in which i have made a function for it and returned a matrix as a 2D vector. `
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> mul(vector<vector<int>> a,vector<vector<int>> b)
{
int n=a.size();
int k=a[0].size();
int m=b[0].size();
vector<vector<int>> c(n,vector<int> (m,0));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
c[i][j]=0;
for(int m=0;m<k;m++)
{
c[i][j]+=(a[i][m]*b[m][j]);
}
}
}
return c;
}
int main() {
vector<vector<int>> l={{1,2}};
vector<vector<int>> r={{1,2,3},{4,5,6}};
vector<vector<int>> m;
m=mul(l,r);
for(int i=0;i<m.size();i++)
{
for(int j=0;j<m[i].size();j++)
{
cout<<m[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
`
精彩评论