Reputation: 175
I'm new to c++ and trying to find some example code to extract integers from comma delimited strings. I come across this code:
std::string str = "1,2,3,4,5,6";
std::vector<int> vect;
std::stringstream ss(str);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == ',')
ss.ignore();
}
I have trouble understanding the while loop conditional statement: ss >> i
. From my understanding, istream::>>
returns the istream operated on. Error bits may be set by the operation. There doesn't seem to be a boolean variable involved. How can ss >> i
serve as a conditional statement?
Additionally, does >>
extract one or multiple characters? For example, if I have a string "13, 14". Does this operation return integers 1, 3, 1, 4 or integers 13, 14?
Thanks a lot, M
Upvotes: 0
Views: 4877
Reputation: 91
The operator >> in stringstream is inherited from istream. The documentation of istream says the return value is istream object [1]. I did a quick test and the return value is a void* (possibly stream object). I also see when the stream is exhausted (at the end) the return value is NULL (This is from my test, and I could not find it in the doc). So this possibly explains the behavior of the while loop, as void* and NULL can be converted to bool . Just change the loop to while(void* x = (ss >> i)) {} and you can get the return value yourself.
Your second question's answer is in the link below which says "Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type, which is stored as the value of val." So in this case it will extract as many characters to convert it to an integer.
[1] http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
Upvotes: 0
Reputation: 12058
1) Conditional statement.
std::stringstream derives from std::ios, which defines:
Description: A null pointer if at least one of failbit or badbit is set. Some other value otherwise.
Description: true if none of failbit or badbit is set. false otherwise.
That's why you can use this expression as condition for a loop - operator>> returns reference to stringstream object, which then is converted to either void* pointer or bool, depending on supported C++ version.
More info about this: std::ios::operator bool
2) operator>> applied for numbers extracts as many characters as it can:
int main()
{
std::string str = "111,12,1063,134,10005,1226";
std::vector<int> vect;
std::stringstream ss(str);
int i;
while (ss >> i)
{
vect.push_back(i);
if (ss.peek() == ',')
ss.ignore();
}
return 0;
}
Content of vector: [111, 12, 1063, 134, 10005, 1226].
Again, more info: std::istream::operator>>
Upvotes: 2
Reputation: 41301
How can
ss >> i
serve as a conditional statement?
Class std::basic_ios
(which is the base class for all standard streams) has explicit operator bool()
which returns !fail()
. The point of it is to indicate that the stream is in valid state and can be used further.
Additionally, does
>>
extract one or multiple characters?
Depends on the type of the object you read. For numbers, it basically extracts as many characters as it can. You may read the rules in detail here.
Upvotes: 2