mthread wrote:
Hi,
I am using a string variable in which I do lot of appending. The
only difficulty I am facing as of now is appending a integer/float
value to this variable. Although I can accomplish this task using a
stringstream variable, it makes my life difficult as it involves 2
function call(one for adding the information to stringstream variable
and the other for copying that value in to string variable) with the
additional burden of resetting this stringstream variable.
Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.
I also ran into this problem a bit and I finally made a template.
template<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}
template<typena me Fstd::string StrmConvert( const F from )
{
return StrmConvert<std ::string>( from );
}
Basically this allows me to do things like:
std::string MyString;
MyString += "Some Value: " + StrmConvert( someint ) + " some float:" +
StrmConvert( somefloat );
etc.. All in one line.
Note there is no error checking, so say, for example, you wanted to pull a
number from a string:
std::string Data = "abc";
int MyInt = StrmConvert<int >( Data );
Data would contain 0 since the variable in StrmConert is default
constructed. It will not throw or even indicate there was an error other
than the default constructed value. This works for me. If it doesn't work
for you, check the status of the stringstream temp in StrmConvert and if it
is not okay throw something.
--
Jim Langston
ta*******@rocke tmail.com