Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 268
maverick Avatar asked Aug 12 '26 23:08

maverick


1 Answers

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;
like image 106
ardiyu07 Avatar answered Aug 15 '26 12:08

ardiyu07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!