ostream output - indenting

C

Christopher

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 said:
ToString(indent + 3) << std::endl;

but still don't know how to indent the entire thing.
 
J

Joe Greer

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


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

}
 
C

Christopher

(e-mail address removed):












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.
 
C

Christopher

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.
 

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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top