[C++] Using cin to insert elements into a vector in a single line (using console). via /r/learnprogramming


[C++] Using cin to insert elements into a vector in a single line (using console).

Hi,

vector<int> X; 

I'm trying to insert user inputted values as elements into the above vector, X. Note that the values are completely arbitrary and will change with each iteration of my main program (hence I can't use vector<int> X = {…} or push_back).

I know how to use for loop push_back commands but this means I have to press enter for every n element that I have. So given that I want to avoid this, is there a way I can enter a single line to the console with whitespaces for input?

In other words, entering

1 0 0 1 

automatically adds the above to vector X and becomes equivalent to

vector<int> X = {1,0,0,1} 

?

Thanks!

Submitted July 16, 2017 at 01:37AM by VonLuftwaffle
via reddit http://ift.tt/2tfbVld

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s