-
November 9th, 2012, 02:16 PM
#1
giving one line command that prints lots of lines - ostream?
I'm using cout to print lots of lines. But i want to put together them in one variable etc. Then , i want to print it.
I think i can do it with ostream but I cant do it . Is there anybody to give me an example use of ostream.
I want to use it such that:
x<< "hello" << endl;
x<< "mike" << endl;
x<< "how " << endl;
x<< "are" << endl;
x<< "you" << endl;
cout << x;
will print
hello
mike
how
are
you
How can i do it?
Thanks in advance
-
November 9th, 2012, 05:07 PM
#2
Re: giving one line command that prints lots of lines - ostream?
The quickest thing would probably be the use of a ostringstream
Code:
#include <sstream>
std::ostringstream os;
os << "hello\n";
os << "mike\n";
os << "how\n";
os << "are\n";
os << "you\n";
std::cout << os.str();
Another possibility could be to define a class X that is able to hold the values you want (but that would limit you in terms of usability of that class) and write operator<< for that class
Code:
struct X
{
int i;
// ...
};
std::ostream& operator<<(std::ostream& os, const X& x)
{
os << x.i /* << other members... */ << std::endl;
return os;
}
HTH
Richard
Last edited by Richard.J; November 9th, 2012 at 05:12 PM.
Reason: removed 'std::' from first line to get rid of the f*g smilies
-
November 10th, 2012, 01:22 PM
#3
Re: giving one line command that prints lots of lines - ostream?
Originally Posted by loves_oi
I'm using cout to print lots of lines. But i want to put together them in one variable etc. Then , i want to print it.
I think i can do it with ostream but I cant do it . Is there anybody to give me an example use of ostream.
std::cout has its own internal buffer, so it won't print everything you write directly. However, every time you write std::end, the buffer is flushed. You can write the string literal "\n" instead to avoid flushing the buffer.*
* Should read: "to avoid forcing the buffer to be flushed."
Last edited by D_Drmmr; November 11th, 2012 at 08:13 AM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 10th, 2012, 01:28 PM
#4
Re: giving one line command that prints lots of lines - ostream?
-
November 11th, 2012, 04:13 AM
#5
Re: giving one line command that prints lots of lines - ostream?
Originally Posted by D_Drmmr
std::cout has its own internal buffer, so it won't print everything you write directly. However, every time you write std::end, the buffer is flushed. You can write the string literal "\n" instead to avoid flushing the buffer.
correct me if I'm wrong, but you can't systematically prevent the stream to flush unless an ad hoc stream buffer is used ( whereas, as you said, you can force the stream to flush, of course ), or am I missing something ?
actually, as far as I can tell, the OP wants a way to "store" a set of cout statements in a single "variable", as suggested by RichardJ. Another alternative wth the bonus of delayed-evaluation (see below) could be somethng like:
Code:
#include <iostream>
template < class Fn >
struct stream_t { Fn f; stream_t( Fn const& f ): f(f) {} };
template < class Fn >
stream_t<Fn> stream( Fn f ) { return stream_t<Fn>( f ); }
template < class Fn >
std::ostream& operator<<( std::ostream& os, stream_t<Fn> st ) { st.f( os ); return os; }
int main()
{
int x = 0;
auto printed_x = [&]( std::ostream& os ) { os << '(' << x << ')'; };
std::cout << stream(printed_x) << std::endl; // prints "(0)"
x = 1;
std::cout << stream(printed_x) << std::endl; // prints "(1)"
}
( note: I used a stream_t wrapper instead of type deduction to make the resulting operator<< the less intrusive as possible ... )
-
November 11th, 2012, 08:11 AM
#6
Re: giving one line command that prints lots of lines - ostream?
Originally Posted by superbonzo
correct me if I'm wrong, but you can't systematically prevent the stream to flush unless an ad hoc stream buffer is used ( whereas, as you said, you can force the stream to flush, of course ), or am I missing something ?
Yes, I meant you can use "\n" to avoid forcing a flush. You cannot prevent the stream from flushing.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|