mercredi 25 février 2015

2D vector passing by reference in C++

When i declare a vector like vector<vector<int> >v and try to pass it to a function like below it works :



int change(vector<vector<int> >&v)
{
v[0][0]=-1;
}
int main()
{
vector<vector<int> >v;
int a[]={1,2,3};
int b[]={4,5,6};
v.push_back(vector<int>(a,a+3));
v.push_back(vector<int>(b,b+3));
change(v);
cout<<v[0][0];
return 0;
}


But when i declare the vector like vector<int>v[2] and pass it to a function the same way? It's causing error :(



int change(vector<vector<int> >&v)
{
v[0][0]=-1;
}
int main()
{
vector<int>v[2];
int a[]={1,2,3};
int b[]={4,5,6};
v[0]=vector<int>(a,a+3);
v[1]=vector<int>(b,b+3);
change(v);
cout<<v[0][0];
return 0;
}


How can i do this and why this causing error?


Aucun commentaire:

Enregistrer un commentaire