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
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
Re: giving one line command that prints lots of lines - ostream?
Quote:
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."
Re: giving one line command that prints lots of lines - ostream?
Re: giving one line command that prints lots of lines - ostream?
Quote:
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 ... )
Re: giving one line command that prints lots of lines - ostream?
Quote:
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.