dimanche 7 juillet 2019

How to pass static vector of vectors by reference?

I want to pass v vector such that it does not get copied every time when I call the function one(..). But I am not able to do that.

Can anyone help me to get out from this?

int n; // global variable
void one(vector <int >(&v)[n]) 
{
    v[0][0] = 1;
}

int main()
{
    cin >> n;//n=1
    vector <int > v[n];
    v[0].push_back(9);
    one(v);
    cout << v[0][0];
}

The error message:

prog.cpp:5:32: error: variable or field ‘one’ declared void
  void one(vector <int > (&v)[n]){
                                ^
prog.cpp:5:27: error: ‘v’ was not declared in this scope
  void one(vector <int > (&v)[n]){
                           ^
prog.cpp: In function ‘int main()’:
prog.cpp:17:6: error: ‘one’ was not declared in this scope
 one(v);
      ^

Aucun commentaire:

Enregistrer un commentaire