3

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.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
maverick
  • 1,458
  • 5
  • 20
  • 28

8 Answers8

4

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;
ardiyu07
  • 1,790
  • 2
  • 17
  • 29
  • 1
    Great answer… until you use max_size. – Fred Nurk Feb 15 '11 at 08:11
  • I believe maverick means the number of integers the user will input, in which case you use a vector and let it reallocate, then handle potential out-of-memory and/or place an upper limit yourself. Max_size doesn't tell you anything useful. – Fred Nurk Feb 15 '11 at 08:17
  • I see.. but I thought it's better to let @maverick now about the existence .max_size() or .capacity() since this may be his first challenge with Vector :) I'm sorry if i misunderstood the question.. – ardiyu07 Feb 15 '11 at 08:24
  • Telling him about capacity might be helpful (but probably not as much as about size); max_size isn't useful. – Fred Nurk Feb 15 '11 at 09:25
  • In C , we don't need to store it in a temp variable and then copy it to array. So, I was searching for an answer without temp and which uses STL. If you like STL go with @Eugen or Martin's answer. I liked both. – Abinash Dash Aug 03 '20 at 18:41
  • How do you get the user to end putting in input? I want it to stop if the input process if the user hits enter. Your code just keeps allowing inputs until you put in a non-number like / or c – Daniel Romero Mar 10 '21 at 23:20
3

If the vector isn't initialized with an initial capacity, then use something like this:

int temp;
for (...) {
    cin >> temp;
    v.push_back(temp);
}
muddybruin
  • 785
  • 4
  • 8
  • 1
    Even if the vector has a capacity greater than its size, you can still use this. – Fred Nurk Feb 15 '11 at 08:12
  • That's true. I guess "initial capacity" was misleading. What I meant was that if you know the number of items, and consequently initialize the capacity to that amount, you don't need to push_back it in. You could still use cin >> v[i]. – muddybruin Feb 15 '11 at 08:25
  • 2
    `capacity` isn't enough, you also need to `resize` the vector. – MSalters Feb 15 '11 at 08:28
3

Slightly more compact versions:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

std::istream_iterator< int > iterBegin( std::cin ), iterEnd;

// using the range constructor
std::vector< int > vctUserInput1( iterBegin, iterEnd );

// using the copy & back_inserter
std::vector< int > vctUserInput2;
std::copy( iterBegin, iterEnd, std::back_inserter( vctUserInput2 ) );
Eugen Constantin Dinca
  • 8,994
  • 2
  • 34
  • 51
2

Try this

std::copy( std::istream_iterator<int>(std::cin),
            std::istream_iterator<int>(),
            std::back_inserter( v ) );

See here

You can simplify this to just creating the vector and initializing it with iterators:

std::vector<int>  v(std::istream_iterator<int>{std::cin},
                    std::istream_iterator<int>{});
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

The easiest solution is with an istream_iterator. You don't want std::copy, tough, because that's greedy. It will read everything. Instead, use this:

std::istream_iterator<int> iter(std::cin);
vector<int> v;
for(.....)
   v.push_back(*iter++);
MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Small program for taking input from user through vector in c++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> v1;
    int input;
    cout<<"Enter elements in vector";
      for (int i=0;i<10;i++)
       {
         cin>>input;
         v1.push_back(input);
       }

       cout<<"Elements in vector";
       for (int i=0;i<10;i++)
        {
          cout<<v1[i]<<"\n";
        }
         return 0;
 }
0

When number of inputs are not given then we to code like that

#include<iostream>
using namespace std;

#include<vector>

int main()
{
    int i ;
    vector<int> v;
    while(i != -1)
    {
        cin >> i;
        v.push_back(i);
    }
}

/In above code i am taking input till user has not entered -1.In your question also there must be some condition given instead of number of inputs and if the number of inputs are given then it's very easy to take input.The code for that is:/

for (i=0; i<n; i++)
{
    cin >> i;
    v.push_back(i);
}
Ravi Prakash
  • 31
  • 1
  • 4
  • Hey Ravi, you need to initialize `int i` before using it in `while (i ...)`. It is good to put some effort in code formatting, that pays for itself. You can mention that the delimited -1 will get pushed into the vector as the last element. – Zrin Mar 27 '20 at 09:51
0

You can also dynamically allocate an array and then use it in a vector.

Here it is what you can do,

#include<bits/stdc++.h>
int main()
{
 int n;cin>>n;
 vector<int>v(n);
 for(int i=0;i<n;i++){
 cin>>v[i];
}
for(auto x:v){
cout<<x<<" "<<endl;
}
}

Its an Easy approach you can do.

  • Please don't post answers that recommend using the `bits/stdc++.h` header! [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073) Some better indentation would also be an improvement. – Adrian Mole Jun 02 '20 at 15:00