I have a function that essentially reads values in from a vector of doubles, appends these to a string (while ensuring a space between each and setting their precisions) and returns the end result, minus the final whitespace:
std::string MultiplePrintProperties::GetHpitchString()
{
std::string str;
vector< double >::iterator it;
for ( it = Vals.begin();
it != Vals.end();
it++ )
{
ostringstream s;
// Set precision to 3 digits after the decimal point
// and read into the string
boost::format fmt( "%.3f " );
s << fmt % *( it );
str.append( s.str() );
}
// Remove last white space and return string
return str.substr( 0, str.length() - 1 );
}
I would like to find out if this code could be simplified in any way. I have recently been investigating the use of for_each and functors in particular but have not been able to figure out how these techniques could improve this particular example.