Simple stringstream question

W

WP

Hi!

Say I have string of the following form:
"integer stringpart". First an integer, then a space and then the
stringpart which may or may not contain additional spaces. I wanted to
separate the integer and the stringpart, so I tried:

istringstream iss(str);

int num;

iss >> num;

Ok, now num contains the int-part. But how do I get the rest into a
string variable? iss.str() returns the entire string and iss >>
astrinvar; only gets me a subpart if the stringpart contains spaces
Is a loop my only option here or have I missed something?

- WP
 
R

Rolf Magnus

Victor said:
You can always get the remaining part of the stringstream by reading its
'streambuf', I think:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
string str("123 abc");
int a;
istringstream is(str);
is >> a;

// here we extract a copy of the "remainder"
string rem(is.str().substr(is.tellg()));

Hmm, is the above line guaranteed to work correctly? I mean mixing string
positions and stream positions.
cout << "Remaining: [" << rem << "]\n";
}

Alternativeley, split the stream up before converting the number:

string str("123 Hello World");
string::size_type pos = str.find(' ');
string first = str.substr(0, pos);
string second = str.substr(pos + 1, string::npos);

Then you can use the stringstream to convert the number.
 
J

James Kanze

You can always get the remaining part of the stringstream by
reading its 'streambuf', I think:

That would be the a solution, I think, if you don't know a
character that can't be in the string. If you know that there's
no newline, however, getline will also work; if there is no
further white space, >> into a string is fine.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
    string str("123   abc");
    int a;
    istringstream is(str);
    is >> a;
    // here we extract a copy of the "remainder"
    string rem(is.str().substr(is.tellg()));

That's not what you said above, and I'm not sure what it will
give. Calling istringstream::str() seems very strange to me.
I'd prefer something like:

std::string remaining ;
is >> remaining ; // skips white space, stops at next
// following whitespace.
std::getline( is, remaining ) ;
// reads until next '\n'
std::eek:stringstream os ;
os << is.rdbuf() ; // gets absolutely everything.

Don't forget that istream::tellg() isn't guaranteed to be
convertible into an integral type, and even if it is, there is
no guarantee concerning the numeric value.
 
J

James Kanze

[...]
Alternativeley, split the stream up before converting the number:
    string str("123 Hello World");
    string::size_type pos = str.find(' ');
    string first = str.substr(0, pos);
    string second = str.substr(pos + 1, string::npos);
Then you can use the stringstream to convert the number.

The splitting is a lot easier, and a lot more robust, if you use
boost::regex to do it.
 
R

Rolf Magnus

Victor said:
Rolf said:

I don't know if stream_pos and size_type can be mixed. I think it has
to be close since the underlying buffer of the string stream *is* a
string, after all.
Alternativeley, split the stream up before converting the number:

string str("123 Hello World");
string::size_type pos = str.find(' ');
string first = str.substr(0, pos);
string second = str.substr(pos + 1, string::npos);

Then you can use the stringstream to convert the number.

Well, don't just look for spaces, mate. You need to look for any
non-digit. The string "123abc" doesn't have a space, yet the integer
you can read from it is the same as from "123 Hello World"...

Well, but that wasn't the OP's specification, according to which there is
always a space between the number and the rest. Of course, format error
handling is missing in my code, but that's the same in your code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top