473,471 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is there better way to add indent space?

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_iterat or<string>(cout,"\n"),add_indent);

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

thanks

Jan 9 '06 #1
3 8462
baibaichen wrote:
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_iterat or<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

Jan 9 '06 #2

baibaichen wrote:
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_iterat or<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.

Jan 9 '06 #3
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.

Jan 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Mark Hahn | last post by:
We're proud to say that Prothon has grown out of the indent space/tab flame war stage and has moved on to more serious discussions. We've decided to allow space-based indents and tab-based...
1
by: Vincent | last post by:
I noticed an irritating behaviour of the text-indent property and I wonder if I'm the only one to feel this way about it: it seems that text-indent applies even to floated elements. Although this...
6
by: Harlan Messinger | last post by:
A publication style guide indicates that for a table heading like the following, Table 3. Wheat and rye harvest in European countries in years that end in 3 or 7 or when a new prime minister...
8
by: tintagel | last post by:
Hi everyone, I've just joined your group! I'm pretty new to HTML etc. Here's my problem: I'm attached to the idea of keeping content separate from formatting information, so I like putting...
5
by: M.N.Smadi | last post by:
Hi there; i have a script that is not indented properly. Is there a way that i can have it auto indented. thanks moe smadi
1
by: prileep | last post by:
I have two tables Users and UserLogin. Here i will use two methods of table design. and which query will return me the result more fast. The table size will be large that it may contain records in...
21
by: Bogdan | last post by:
Can anyone recommend a program for indentation of C preprocessor directives. My file looks like this: #ifdef a #define b #else #define c #endif int main() {
20
by: Eric Wertman | last post by:
I was considering putting together a proposal for an alternate block syntax for python, and I figured I'd post it here and see what the general reactions are. I did some searching, and while I...
14
by: Jeff | last post by:
Lets say we have a paragraph that wraps several lines. I'd like to indent the first-line a bit. The first-line pseudo class sprang to mind, but you can't set a left padding there. Perhaps...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.