While it shouldn't be too difficult to insert the text in
question, once you've gotten the correct size (using a.size()
and b.size()
), I question whether this is the best solution.
I'd probably append the necessary padding to out
before
appending s2
. Something like:
int padding = 70 - (asInt( out.size() ) + asInt( s2.size() ) );
if ( padding > 0 ) {
out.append( padding, ' ' );
}
out.append( s2 );
The extra tests are necessary because std::string
uses an
unsigned type (size_t
) for the size, and unsigned types in C++
tend to lead to some surprizing results. (The asInt
may or
may not be necessary, depending on where the strings come from.
They're basically:
int
asInt( size_t original )
{
if ( original > std::numeric_limits<int>::max() ) {
throw std::range_error( "size too large for int" );
return static_cast<int>( original );
}
In many cases, you will know that the strings cannot be too long
before you get to this point in the code, and so you don't need
them.
Note that you must convert the sizes to int
(or some other
signed type) before calculating the padding. Otherwise, the
calculation will give the wrong results.