473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ostream output - indenting

How would I go about indenenting for each level of recursion, if I am
trying to output the contents of a class which contains its own type?
where AttributeGroupMap is
typdef std::map<std::string, AttributeGroup *AttributeGroupMap;

const std::string AttributeGroup::ToString() const
{
std::stringstream ss;

ss << m_name << std::endl;

for(AttributeGroupMap::const_iterator it =
m_attributeGroups.begin();
it != m_attributeGroups.end(); ++it)
{
// need everything from here indented for each level of
recursion
ss << (it->second)->ToString() << std::endl;
}

return ss.str();
}
I thought if changing the parameters to something like
const std::string AttributeGroup::ToString(unsigned indent = 0) const

and then
ss << /** do something to indent here */ (it->second)-
>ToString(indent + 3) << std::endl;
but still don't know how to indent the entire thing.
Jul 28 '08 #1
4 3820
Christopher <cp***@austin.rr.comwrote in news:dfc7ee09-8cdf-4e7d-b8f0-
69**********@34g2000hsf.googlegroups.com:
How would I go about indenenting for each level of recursion, if I am
trying to output the contents of a class which contains its own type?
where AttributeGroupMap is
typdef std::map<std::string, AttributeGroup *AttributeGroupMap;

const std::string AttributeGroup::ToString() const
{
std::stringstream ss;

ss << m_name << std::endl;

for(AttributeGroupMap::const_iterator it =
m_attributeGroups.begin();
it != m_attributeGroups.end(); ++it)
{
// need everything from here indented for each level of
recursion
ss << (it->second)->ToString() << std::endl;
}

return ss.str();
}
I thought if changing the parameters to something like
const std::string AttributeGroup::ToString(unsigned indent = 0) const

and then
ss << /** do something to indent here */ (it->second)-
>>ToString(indent + 3) << std::endl;

but still don't know how to indent the entire thing.
Indenting is pretty simple, simply prepend std::string(indent, ' ') to
the string. If I were you though, I would separate the recursive output
from the ToString() method. A method named ToString() is too general
for such a specific use. I would start with something like:

// since we return by value, there is no need to return a const string
std::string AttributeGroup::ToString() const
{
return m_name; // assumes m_name is a string
}

std::string AttributeGroup::RecursiveAttributeDump(int lvl) const
{
std::string ret = std::string(lvl * 3, ' ') + ToString() + '\n';

for (AttributeGroupMap::const_iterator it = m_attributeGroups.begin
(),
it != m_AttributeGroups.end(), ++it)
{
RecursiveAttributeDump(lvl+1);
}
return ret;
}
This is all untried but it should work.

joe

}
Jul 28 '08 #2
On Jul 28, 1:10*pm, Joe Greer <jgr...@doubletake.comwrote:
Christopher <cp...@austin.rr.comwrote in news:dfc7ee09-8cdf-4e7d-b8f0-
69531c079...@34g2000hsf.googlegroups.com:
How would I go about indenenting for each level of recursion, if I am
trying to output the contents of a class which contains its own type?
where AttributeGroupMap is
typdef std::map<std::string, AttributeGroup *AttributeGroupMap;
const std::string AttributeGroup::ToString() const
{
* *std::stringstream ss;
* *ss << m_name << std::endl;
* *for(AttributeGroupMap::const_iterator it =
m_attributeGroups.begin();
* * * * it != m_attributeGroups.end(); ++it)
* *{
* * * // need everything from here indented for each level of
recursion
* * * ss << (it->second)->ToString() << std::endl;
* *}
* *return ss.str();
}
I thought if changing the parameters to something like
const std::string AttributeGroup::ToString(unsigned indent = 0) const
and then
ss << /** do something to indent here */ *(it->second)-
>ToString(indent + 3) << std::endl;
but still don't know how to indent the entire thing.

Indenting is pretty simple, simply prepend std::string(indent, ' ') to
the string. *If I were you though, I would separate the recursive output
from the ToString() method. *A method named ToString() is too general
for such a specific use. *I would start with something like:

// since we return by value, there is no need to return a const string
std::string AttributeGroup::ToString() const
{
* *return m_name; // assumes m_name is a string

}

std::string AttributeGroup::RecursiveAttributeDump(int lvl) const
{
* *std::string ret = std::string(lvl * 3, ' ') + ToString() + '\n';

* *for (AttributeGroupMap::const_iterator it = m_attributeGroups.begin
(),
* * * it != m_AttributeGroups.end(), ++it)
* *{
* * * RecursiveAttributeDump(lvl+1);
* *}
* *return ret;

}

This is all untried but it should work.

joe

}

mmm
works if there is one data member to output, but I failed to show in
my concept code there that there are multiple lines of output, as
there will be multiple data members in the object. Name was just one.
Sorry I left that bit out.

Jul 28 '08 #3
On Jul 28, 1:21*pm, Christopher <cp...@austin.rr.comwrote:
On Jul 28, 1:10*pm, Joe Greer <jgr...@doubletake.comwrote:
Christopher <cp...@austin.rr.comwrote in news:dfc7ee09-8cdf-4e7d-b8f0-
69531c079...@34g2000hsf.googlegroups.com:
mmm
works if there is one data member to output, but I failed to show in
my concept code there that there are multiple lines of output, as
there will be multiple data members in the object. Name was just one.
Sorry I left that bit out.
Ah, I think I got it.
Just had to modify things a little. Thanks for the help. Problem
resolved.
Jul 28 '08 #4
Christopher <cp***@austin.rr.comwrote in
news:10**********************************@c65g2000 hsa.googlegroups.com:
On Jul 28, 1:21*pm, Christopher <cp...@austin.rr.comwrote:
>On Jul 28, 1:10*pm, Joe Greer <jgr...@doubletake.comwrote:
Christopher <cp...@austin.rr.comwrote in
news:dfc7ee09-8cdf-4e7d-b8f0
-
69531c079...@34g2000hsf.googlegroups.com:
mmm
works if there is one data member to output, but I failed to show in
my concept code there that there are multiple lines of output, as
there will be multiple data members in the object. Name was just one.
Sorry I left that bit out.

Ah, I think I got it.
Just had to modify things a little. Thanks for the help. Problem
resolved.
Great. I'm glad it helped.

joe
Jul 28 '08 #5

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

Similar topics

1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
8
by: Boris | last post by:
Is it possible to manipulate the std::ostream to prepend a string when performing output, e.g. // manipute std::cout to prepend "prefix " std::cout << "hallo" << std::endl; // results in...
2
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
4
by: Alexander Stippler | last post by:
Hi, The short story: I have to redirect stdout (not cout!!) to an ostream. How can I manage this? The long story: I use the NAGC numerics library with C++ and want to use its output routines...
9
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For...
0
by: Steven T. Hatton | last post by:
I tried to create my own manipulator that would both set the width of the subsequent output field, and cast an unsigned char to unsigned in. I came up with the following rather ugly hack. Notice...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
6
by: silversurfer2025 | last post by:
Hello, I am currently trying to derive a class from ostream (which is giving output to my GUI), such that I can give my methods either std::cout or my own outputstream-class to be used as output...
1
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.