is there better way to add indent space?

B

baibaichen

hi i want to output strings with indent space, the code looks like:

std::set<std::string> files;

std::set<std::string> iterator b = files.begin();
std::set<std::string> iterator e = files.end();

for (b; b!=e b++)
std::cout << '\t' << *b << std::endl;


the another way is :

string add_indent(const string& str)
{
return '\t' + str;
}

transform(files.begin(),files.end(),ostream_iterator<string>(cout,"\n"),add_indent);

which is better? and is there better way to add indent space?

thanks
 
M

mlimber

baibaichen said:
hi i want to output strings with indent space, the code looks like:

std::set<std::string> files;

std::set<std::string> iterator b = files.begin();
std::set<std::string> iterator e = files.end();

for (b; b!=e b++)
std::cout << '\t' << *b << std::endl;


the another way is :

string add_indent(const string& str)
{
return '\t' + str;
}

transform(files.begin(),files.end(),ostream_iterator<string>(cout,"\n"),add_indent);

which is better? and is there better way to add indent space?

thanks

You could use std::setw and the field alignment of iostreams to do
something similar, but it depends how you want to indent. Tabs might
not always line up correctly without more sophisticated logic that
takes into account the length of the strings. (Of course, the same can
be true of std::setw.)

Cheers! --M
 
E

Earl Purple

baibaichen said:
hi i want to output strings with indent space, the code looks like:

std::set<std::string> files;

std::set<std::string> iterator b = files.begin();
std::set<std::string> iterator e = files.end();

for (b; b!=e b++)
std::cout << '\t' << *b << std::endl;


the another way is :

string add_indent(const string& str)
{
return '\t' + str;
}

transform(files.begin(),files.end(),ostream_iterator<string>(cout,"\n"),add_indent);

which is better? and is there better way to add indent space?

well a better way to write your loop would be:

for ( ; b!=e; ++b )
{
std::cout << '\t' << *b << '\n';
}

using pre-increment and not flushing the buffer unnecessarily.
 
J

Jerry Coffin

baibaichen said:
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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top