Re: ostream output - indenting
Christopher <cpisz@austin.rr.com> wrote in news:dfc7ee09-8cdf-4e7d-b8f0-
69531c079930@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
}