giving one line command that prints lots of lines - ostream?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: giving one line command that prints lots of lines - ostream?

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    41

    Lightbulb 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

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,108

    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 04:12 PM. Reason: removed 'std::' from first line to get rid of the f*g smilies

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: giving one line command that prints lots of lines - ostream?

    Quote Originally Posted by loves_oi View Post
    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 07: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

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,108

    Re: giving one line command that prints lots of lines - ostream?

    must be "std::endl"

  5. #5
    Join Date
    Oct 2008
    Posts
    1,088

    Re: giving one line command that prints lots of lines - ostream?

    Quote Originally Posted by D_Drmmr View Post
    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 ... )

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: giving one line command that prints lots of lines - ostream?

    Quote Originally Posted by superbonzo View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center