|
|
How to convert std::string to std::istream?
I have a function that use std::istream as parameter. Right now I need
to pass a std::string into it, how can I convert std::string to
std::istream type?
Thanks
Water Lin
--
Water Lin's notes and pencils: http://en.waterlin.org
Email: WaterLin@ymail.com
|
|
0
|
|
|
|
Reply
|
WaterLin (12)
|
11/13/2009 3:20:29 AM |
|
Water Lin wrote:
> I have a function that use std::istream as parameter. Right now I need
> to pass a std::string into it, how can I convert std::string to
> std::istream type?
You can't directly, but you can use a stringstream:
#include <iostream>
#include <sstream>
void f( std::istream& in )
{
std::string s;
in >> s;
std::cout << s << std::endl;
}
int main()
{
std::string s("hello");
std::istringstream ss( s );
f( ss );
}
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
ian-news (9881)
|
11/13/2009 4:05:44 AM
|
|
Ian Collins <ian-news@hotmail.com> writes:
> Water Lin wrote:
>> I have a function that use std::istream as parameter. Right now I need
>> to pass a std::string into it, how can I convert std::string to
>> std::istream type?
>
> You can't directly, but you can use a stringstream:
>
> #include <iostream>
> #include <sstream>
>
> void f( std::istream& in )
> {
> std::string s;
> in >> s;
> std::cout << s << std::endl;
> }
>
> int main()
> {
> std::string s("hello");
> std::istringstream ss( s );
>
> f( ss );
> }
Thanks, I got it.
Water Lin
--
Water Lin's notes and pencils: http://en.waterlin.org
Email: WaterLin@ymail.com
|
|
0
|
|
|
|
Reply
|
WaterLin (12)
|
11/13/2009 4:47:01 AM
|
|
Ian Collins wrote:
> Water Lin wrote:
>> I have a function that use std::istream as parameter. Right now I need
>> to pass a std::string into it, how can I convert std::string to
>> std::istream type?
>
> You can't directly, but you can use a stringstream:
Maybe I'm being hugely pedantic here, but what do you mean "you can't
directly"? How is giving the string to a strinstream *not* converting
the string to a std::istream? To me it sounds like being exactly that.
|
|
0
|
|
|
|
Reply
|
nospam270 (2853)
|
11/13/2009 8:21:37 PM
|
|
|
3 Replies
453 Views
(page loaded in 0.057 seconds)
|
|
|
|
|
|
|
|
|