int x;
while(cin>>x)
{
store the number one by one
}
//process
Simply do it this way. Store the numbers in the array.
Or you can do it this way-
string s;
getline(cin,s);
std::stringstream myss;
myss<<s;
std::string t;
int x;
std::vector<int> v;
while(std::getline(myss,t,' '))
{
if(std::stringstream(t)>>x)
{
// store x in an vector.
v.push_back(x);
}
Let’s elaborate the idea-
What is stream? Informally, a string is a collection of characters, a stream is a tool to manipulate moving data around.
What is stringstream? A class that operate on strings.Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a
string
object, using memberstr
.How the first code works?
cin
is an object of classistream
that represents the standard input stream.(from where inputs are fetched) It returns a reference to the same stream for which it is overloaded hereistream
.cin
is used for formatted string extraction.It’s possible to make tests like
if (cin)
//then do something.
which will betrue
ifcin
is ok andfalse
if an end-of-file or has encountered an error. It can be used easily in case you are reading from a file.How the second code works? Simply put it this way. Upto the ‘ ‘ you are putting a whole line in a
string
. Then you are using thestringstream
class to get the desired value (here int x). Now when you are extracting from it, you are only extracting a int value. After that it again goes to getline to fetch the next number. As space seperates the numbers each getline is getting a string containing that number. The getline works as follows- getline(cin,str,delim)
extracts characters from cin and stores them into str until the delimitation character delim is found (or the newline character, ‘\n’ where the delim is not there [meansgetline(cin,str)
]),The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.
Each extracted character is appended to the string.Now this is how the second code works. Check reference manuals for more deatils.
Now let’s see a little bit about vector
s.
vector is a container type in c++. The next question would be probably what is conatiner?
container: containers are holder objects that store collection of other objects. Simply put, you can keep anything in this containers, be it a int or a object you defined or structure type, anything. This is flexibilty is acieved using template. The template as the name implies simply give us a idea about it’s work-yes it is a template of any type you can use in place of it.
vector: There are other containers like list,deque etc but vector is quite different from other containers. Vector is quite effiecient where it has to insert or delete element from ends( back end) and access elements randomly- vector provides random access easily like
v[i]
etc. This is the basic of vector. It is better than arrays because it increases dynamically as you push elements. To know how to use it check the reference manual.
14
solved How to input integer numbers with space in between