c++ - How should I take a series of input in a vector? -


i used below given function inpvec didn't work because when used function in main function there no output using iterators...

 #include <iostream>  #include <vector>  using namespace std;   void inpvec(vector <int> a, int veclen){      int b;      for(int i=0;i<veclen;i++){          cin>>b;          a.push_back(b);      }  }   int main()  {      int n,j;      cin >>n;      vector <int> vac;      vector <int> pat;      vector <int>::iterator it;      inpvec(vac, n);      inpvec(pat, n);      for(it=vac.begin();it!=vac.end();it++){          cout<<*it<<" ";      }      for(it=pat.begin();it!=pat.end();it++){          cout<<*it<<" ";      }      return 0;  } 

your function

void inpvec(vector <int> a, int veclen) 

makes copy of first parameter a. calling code see no difference.

making reference change that

void inpvec(vector <int> & a, int veclen) //                       ^----- 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -