ostream value output question

P

Parity

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";
 
V

Victor Bazarov

Parity said:
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
 
S

Stephen Howe

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top