Search code examples
c++vectoruser-input

writing integer input to vector container in C++


likewise we do in array

for (.....)
  cin>>a[i];

how we can do this using vectors. i declared a vector of integers

vector<int> v;

now i need to take inputs from console and add append those in vector.i am using vector because i donot know the limit.


Solution

  • To insert integer to a vector from console and print everything out:

    int input;
    vector<int> v;
    while(cin >> input){
     v.push_back(input);
    }
    
    for(int i = 0; i<v.size(); i++){
     cout<< v[i] <<endl;
    }
    

    And vector also provides you to print out the max size with:

    cout << v.max_size()<<endl;