Christopher Benson-Manica wrote:
If std::stringstreams are unavailable, how would you go about adding
an integer (or unsigned integer) to a string?
#include <limits>
#include <string>
#define X *--o='0'+(x%10);x/=10;
#define O while(x){*--o=',';X if(x)X if(x)X}
template <typename Integer>
void append_int (Integer x, std::string & s)
{
char buffer [std::numeric_limits <Integer>::digits10 * 4 / 3 + 2];
char * o = buffer + sizeof buffer;
if(x<0){if((x=-(x+1))%10-9){*--o='1'+(x%10);x/=10;}
else{*--o='0';x/=10;++x;}X if(x)X O*--o='-';}
else if(x){X if(x)X if(x)X O}else{*--o='0';}
s.append (o, buffer + sizeof buffer);
}
--
Regards,
Buster.