Depends on what form you expect the input to take. If the expected input is a list of number, delimited by a space, on a single line:
>>>1 2 3 4 5 6
this is fairly easily solvable:
#include<vector> #include<string> #include<iostream> #include<sstream> int main(){ std::vector<int> v; //default construct int vector //read in line of input into "buffer" string variable std::string buffer; std::getline(std::cin, buffer); //stream line of input into a stringstream std::stringstream ss; ss << buffer; //push space-delimited ints into vector int n; while(ss >> n){ v.push_back(n); } //do stuff with v here (presumably) return 0; }
If however, the expected input is a list of numbers, delimited by a new line:
>>>1 2 3 4 5 6
You will have to decide on an exit condition, that will tell the program when to stop accepting inputs. This could take the form of a word, which would tell the program to stop. For example:
>>>1 2 3 4 5 6 STOP
A program that would work with such an input:
#include<vector> #include<string> #include<iostream> #include<sstream> int main(){ std::vector<int> v; //default construct int vector const std::string exitPhrase = "STOP"; //initialise exit phrase //read in input into "buffer" string variable. If most recent input // matches the exit phrase, break out of loop std::string buffer; while(std::cin >> buffer){ if(buffer == exitPhrase) break; //check if exit phrase matches //otherwise convert input into int and push into vector std::stringstream ss; ss << buffer; int n; ss >> n; v.push_back(n); } //do stuff with v here (again, presumably) return 0; }
For a more robust solution, also consider checking the input to see if it can be made into ints.
#include<bits/stdc++.h>is non-standard and pulls in a lot of stuff you don't need. You should include the correct headers for the types you are using.whileloop in a C++ program?