understanding istringstream in c++
Hi,
I am new to c++ and in process fo understading and learning string class.
I am working on istringstream
Code:
std::istringstream iss;
std::string value="32 40 50 80 902";
iss.str (value); //what does this do???
for(int i=0;i<5;i++){
cout<< " " <<i<<"\n";
int val;
iss >> val;
Quote:
I get output as 32 40 50 80 902
That is; string has been split up by space.
Code:
std::string value="32,40,50,80,902";
If I replace space by comma, the output is 32 and all 0 there after.
I cannot understand how it actually works.
PS: Apolologies, if I have posted question in wrong forum. Kindly provide me so not repeat mistake in future.
Re: understanding istringstream in c++
You might get a better idea from this
Code:
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
istringstream iss;
string value = "32,40,50,80,902";
iss.str (value); //what does this do???
for (int i = 0; i < 5; i++) {
string val;
iss >> val;
cout << i << endl << val <<endl;
}
return 0;
}
which outputs
Code:
0
32,40,50,80,902
1
2
3
4
As you can see, for index 0 it outputs the whole string rather than just the first part. This is because iss.str(value) creates a string stream called iss consisting of the string value. iss >> val extracts the next element from the iss stream and puts it into val. Elements in a character stream are considered to be separated by 'white space' which is basically space, tab and newline characters (see http://msdn.microsoft.com/en-us/libr.../e9a023cx.aspx). So when the elements are separated by a ',' this is not a 'white-space' character so the whole string is read. When the elements are separated by a ' ' this is a white space character so the next element obtained by iss >> val is the next number in the string.
So
Code:
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
istringstream iss;
string value = "32 40 50 80 902";
iss.str (value); //what does this do???
for (int i = 0; i < 5; i++) {
string val;
iss >> val;
cout << i << endl << val <<endl;
}
return 0;
}
produces
Code:
0
32
1
40
2
50
3
80
4
902
In your original code val was of type int so iss >> val wa trying to read an integer from the stream. When the values were separated by a ' ' this worked as each loop iteration produced the next number. However when separated by a ',' the first extration produced the whole string which is not a number as it contains ','s. So trying to extract an integer produces the first valid number from the stream (32) and then the stream is set to 'bad' because it tries to read a number and found ',' which is not valid. So further attempts to read from the stream fail until the stream state is reset to good.
Have a look at
http://www.cplusplus.com/reference/s.../stringstream/
You might also find these of interest
http://www.cplusplus.com/doc/
http://www.learncpp.com/
Re: understanding istringstream in c++
Thank you for such a great explanation and helpful URLs.
Quote:
Originally Posted by
2kaud