cin to stringstream

C

cherico

I'd like to read stings from cin and put them
in stringstream.

I use a string object as an intermediate "container"
to store data from cin and then put them in stringstream.

stringstream ss ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)
 
J

John Harrison

cherico said:
I'd like to read stings from cin and put them
in stringstream.

I use a string object as an intermediate "container"
to store data from cin and then put them in stringstream.

stringstream ss ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)

Does this count?

void copy_string(istream& in, ostream& out)
{
string str;
in >> str;
out << str;
}

stringstream ss;
copy_string(cin, ss);

Its still uses a intermediate string but its hidden from the rest of your
code.

Other than that I don't think there is a way. Why is it an issue? There
maybe another way to solve whatever problem you are having.

john
 
X

Xiaobin Yang

Overload operator >>, it solves.[B

istream& operator >> (istream& in, stringstream& ss);
int main (int argc, char* argv[])
{
stringstream ss; cin >> ss; cout << ss.str() << endl; return 0;
}
istream& operator >> (istream& in, stringstream& ss){
string s; in >> s; ss << s; return in;
}

I'd like to read stings from cin and put them
in stringstream.

I use a string object as an intermediate "container"
to store data from cin and then put them in stringstream.

stringstream ss ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)

-- xiaobin
 
T

tom_usenet

I'd like to read stings from cin and put them
in stringstream.

I use a string object as an intermediate "container"
to store data from cin and then put them in stringstream.

stringstream ss ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)

If you don't mind putting all of the data into the stringstream, you
can do:

ss << cin.rdbuf();

Alternatively, there are plenty of ways of copying data from one to
the other using algorithms. e.g.

std::istreambuf_iterator<char> begin(cin), end;
std::eek:streambuf_iterator<char> out(ss);
std::copy(begin, end, out);
//or use a different algo, like copy_n.

Tom
 
D

Dietmar Kuehl

I'd like to read stings from cin and put them
in stringstream.

If you read strings and put them into a stringstream you are implicitly
removing whitespace: is this by design or by accident?
stringstream ss ;
cin >> ss ;

Here are a few alternatives which do not remove whitespace:

std::stringstream ss;
ss << std::cin.rdbuf();

std::copy(std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>(),
std::eek:streambuf_iterator<char>(ss));

.... and a few alternatives which do remove whitespace:

std::copy(std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
std::eek:stream_iterator<std::string>(ss));

bool my_isspace(char c) { return std::isspace(c); } // in namespace scope
std::remove_copy_if(std::istreambuf_iterator<char>(std::cin),
std::istreambuf_iterator<char>(),
std::eek:streambuf_iterator<char>(ss), my_isspace);
 
T

tom_usenet

bool my_isspace(char c) { return std::isspace(c); } // in namespace scope

There's a missing conversion in that isspace call:

bool my_isspace(char c) {
return std::isspace(std::char_traits<char>::to_int_type(c));
}

otherwise you get UB for negative values of c (other than EOF).

Tom
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top