baibaichen wrote:
hi i want to output strings with indent space, the code looks like:
[ and is there a better way to do it...]
I prefer your use of std::transform over the explicit loop.
Depending on what you're doing overall, there may be better ways do to
the job. The basic question is whether the indentation is a constant
characteristic of something. If everything you print to this stream (at
least for a period of time, but more than this one operation) is
supposed to be indented, then you might well want to modify the
stream's behavior by writing a modified stream buffer class. I seem to
recall having seen a stream buffer that numbered each line as it was
printed out, which would be quite trivial to simply print out a tab
instead of a line number. Unfortunately, I don't recall where that was,
so you might have to look a little bit for it (or ask specifically
about it, and somebody can probably supply code for the job).
OTOH, if these strings will always be indented, whenever they're
printed out, then you could create a proxy class that always indents
them. In that case, printing them out indented is just a matter of
copying them to the stream.
Finally, if the indentation really is unique to this particular
situation, then you probably only want to attach the code to the
situation, roughly as you've already done it. One minor detail: though
it probably doesn't make much difference here, you're often better off
using a functor instead of an actual function. You might also want to
look into Boost::Lambda to allow you to generate the code more or less
inline instead of the function you're currently using.
--
Later,
Jerry.