Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cin values into a vector

People also ask

Can I use CIN with vector?

Can i use cin directly inside push_back() in vector in c++ cin always requires a temporary variable to read into, which is ugly: I don't usually use cin directly - I always wrap it in a helper called read . cin >> X returns the istream object, so, no.

How do you add elements to a vector in C++?

To add elements to vector, you can use push_back() function. push_back() function adds the element at the end of this vector. Thus, it increases the size of vector by one.


As is, you're only reading in a single integer and pushing it into your vector. Since you probably want to store several integers, you need a loop. E.g., replace

cin >> input;
V.push_back(input);

with

while (cin >> input)
    V.push_back(input);

What this does is continually pull in ints from cin for as long as there is input to grab; the loop continues until cin finds EOF or tries to input a non-integer value. The alternative is to use a sentinel value, though this prevents you from actually inputting that value. Ex:

while ((cin >> input) && input != 9999)
    V.push_back(input);

will read until you try to input 9999 (or any of the other states that render cin invalid), at which point the loop will terminate.


Other answers would have you disallow a particular number, or tell the user to enter something non-numeric in order to terminate input. Perhaps a better solution is to use std::getline() to read a line of input, then use std::istringstream to read all of the numbers from that line into the vector.

#include <iostream>
#include <sstream>
#include <vector>

int main(int argc, char** argv) {

    std::string line;
    int number;
    std::vector<int> numbers;

    std::cout << "Enter numbers separated by spaces: ";
    std::getline(std::cin, line);
    std::istringstream stream(line);
    while (stream >> number)
        numbers.push_back(number);

    write_vector(numbers);

}

Also, your write_vector() implementation can be replaced with a more idiomatic call to the std::copy() algorithm to copy the elements to an std::ostream_iterator to std::cout:

#include <algorithm>
#include <iterator>

template<class T>
void write_vector(const std::vector<T>& vector) {
    std::cout << "Numbers you entered: ";
    std::copy(vector.begin(), vector.end(),
        std::ostream_iterator<T>(std::cout, " "));
    std::cout << '\n';
}

You can also use std::copy() and a couple of handy iterators to get the values into the vector without an explicit loop:

std::copy(std::istream_iterator<int>(stream),
    std::istream_iterator<int>(),
    std::back_inserter(numbers));

But that’s probably overkill.


You need a loop for that. So do this:

while (cin >> input) //enter any non-integer to end the loop!
{
   V.push_back(input);
}

Or use this idiomatic version:

#include <iterator> //for std::istream_iterator 

std::istream_iterator<int> begin(std::cin), end;
std::vector<int> v(begin, end);
write_vector(v);

You could also improve your write_vector as:

 #include <algorithm> //for std::copy

template <typename T>
void write_vector(const vector<T>& v)
{
   cout << "The numbers in the vector are: " << endl;
   std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
}

you have 2 options:

If you know the size of vector will be (in your case/example it's seems you know it):

vector<int> V(size)
for(int i =0;i<size;i++){
    cin>>V[i];
 }

if you don't and you can't get it in you'r program flow then:

int helper;
while(cin>>helper){
    V.push_back(helper);
}

If you know the size of the vector you can do it like this:

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

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto &it : v) {
        cin >> it;
    }
}