Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   cin to stringstream (http://www.velocityreviews.com/forums/t283583-cin-to-stringstream.html)

cherico 06-08-2004 06:11 AM

cin to stringstream
 
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...)

John Harrison 06-08-2004 06:27 AM

Re: cin to stringstream
 

"cherico" <cherico@bonbon.net> wrote in message
news:afc115ec.0406072211.42c73fce@posting.google.c om...
> 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



Xiaobin Yang 06-08-2004 09:22 AM

Re: cin to stringstream
 
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;
}

On Tue, 7 Jun 2004, cherico wrote:

> 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

tom_usenet 06-08-2004 09:50 AM

Re: cin to stringstream
 
On 7 Jun 2004 23:11:47 -0700, cherico@bonbon.net (cherico) wrote:

>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::ostreambuf_iterator<char> out(ss);
std::copy(begin, end, out);
//or use a different algo, like copy_n.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Dietmar Kuehl 06-08-2004 01:06 PM

Re: cin to stringstream
 
cherico@bonbon.net (cherico) wrote:
> 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::ostreambuf_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::ostream_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::ostreambuf_iterator<char>(ss), my_isspace);
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

tom_usenet 06-08-2004 02:18 PM

Re: cin to stringstream
 
On 8 Jun 2004 06:06:20 -0700, dietmar_kuehl@yahoo.com (Dietmar Kuehl)
wrote:

> 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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html


All times are GMT. The time now is 04:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.