ostream value output question

Discussion in 'C++' started by Parity, Aug 13, 2003.

  1. Parity

    Parity Guest

    Is there some standard) way to make ostream automatically insert
    whitespace character after something is inserted into stream using
    operator <<?
    For example:

    int x=10, y=20, z=30;
    ofs << "(" << x << y << z << ")\n";

    should yield "(10 20 30)" - not "(102030)". My current way around this
    induces very messy code, especially when there are more than three
    variables written at once:

    ofs << "(" << x << " " << y << " " << z << ")\n";
     
    Parity, Aug 13, 2003
    #1
    1. Advertising

  2. "Parity" <> wrote...
    > Is there some standard) way to make ostream automatically insert
    > whitespace character after something is inserted into stream using
    > operator <<?


    Something? Like what? Like, everything?

    > For example:
    >
    > int x=10, y=20, z=30;
    > ofs << "(" << x << y << z << ")\n";
    >
    > should yield "(10 20 30)" - not "(102030)".


    If it inserts a space after "everything", it should yield

    "( 10 20 30 )\n "

    , shouldn't it? If it inserts a space only after an int,
    it will result into

    "(10 20 30 )\n"

    , won't it? Both are not what you need, I gather.

    > My current way around this
    > induces very messy code, especially when there are more than three
    > variables written at once:
    >
    > ofs << "(" << x << " " << y << " " << z << ")\n";


    THAT you call messy? At least it does what you want, not what
    it thinks you want. You could probably write a custom stream
    buffer that would append a space to itself after every output.
    But that will result in the output I quoted above, not in what
    you think it should.

    Victor
     
    Victor Bazarov, Aug 13, 2003
    #2
    1. Advertising

  3. Parity

    Stephen Howe Guest

    > Is there some standard) way to make ostream automatically insert
    > whitespace character after something is inserted into stream using
    > operator <<?
    > For example:
    >
    > int x=10, y=20, z=30;
    > ofs << "(" << x << y << z << ")\n";
    >
    > should yield "(10 20 30)" - not "(102030)". My current way around this
    > induces very messy code, especially when there are more than three
    > variables written at once:
    >
    > ofs << "(" << x << " " << y << " " << z << ")\n";


    You can do

    ofs << '(' << x << ' ' << y << ' ' << z << ")\n";

    which is more economical than quoted strings. I use single characters where
    the quoted string being output is just a single character.

    Apart from this, you can use ostream_iterator if your x, y,z are in an
    array. So

    vector <int> coords1;
    int coords2[3];
    :
    ofs << '(';
    copy(coords1.begin(), coords1.end(), ostream_iterator<int>(ofs, " "));
    ofs << ")\n";

    or (I have not tested this out to see if this compiles but I think so)

    ofs << '(';
    copy(&coords2[0], &coords2[3], ostream_iterator<int>(ofs, " "));
    ofs << ")\n";

    The " " adds a space between elements of an array

    with the appropriate headers and "using namespace std;"

    Stephen Howe
     
    Stephen Howe, Aug 13, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trevor
    Replies:
    2
    Views:
    525
  2. Zhixin Han
    Replies:
    4
    Views:
    425
    Zhixin Han
    Oct 7, 2004
  3. Rock
    Replies:
    4
    Views:
    444
    Jim Langston
    Nov 20, 2005
  4. Johannes Barop

    Formating Output of std::ostream

    Johannes Barop, Dec 26, 2005, in forum: C++
    Replies:
    10
    Views:
    628
    Dietmar Kuehl
    Dec 27, 2005
  5. Replies:
    2
    Views:
    1,930
Loading...

Share This Page