converting integer to a string(without the help of stringstreamvariable)

M

mthread

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.
 
M

Martin York

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.

Check out the library provided by boost.org

There is a lexical_cast<>() function.

It uses stringstream underneath but provides a nice interface to it.
 
J

Juha Nieminen

mthread said:
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.

AFAIK the only alternative *standard* way to do that would be to use
std::sprintf(), but that would probably not be any simpler than using
stringstreams. On the contrary, it would probably be more complicated
because you would have to guess how many characters std::sprintf() will
write, and make sure you allocate enough space for it. Then you have to
resize the string to fit the actual amount of characters written.
 
M

Michael DOUBEZ

mthread a écrit :
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.

If you do a lot of appending, using a stringstream from the very
beginning seems logical. And with it, you could control the formating of
your numerical values (which is not the case with boost::lexical_cast<>).


Michael
 
J

Jim Langston

mthread said:
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<typename T, typename F > T StrmConvert( const F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

template<typename F> std::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.
 
R

Richard Herring

In message
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.

As you hint, reusing a stringstream and remembering to clear both its
content and its flags can bes error-prone. If you do go down this route,
it's probably better to construct a separate stringstream for each
conversion and then discard it after use.
Is there anyway I can accomplish this task in a much simpler
way. Thanx in advance.

If you're doing a lot of appending, use a single stringstream instead of
the string as your text accumulator, and only convert to string when
it's complete.
 
D

Default User

mthread said:
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.

I fail to see how it's much of a burden. It sounds like your usage is
pretty repititious, so write it once and put it in a function.





Brian
 
J

Juha Nieminen

Default said:
I fail to see how it's much of a burden. It sounds like your usage is
pretty repititious, so write it once and put it in a function.

Something like this could work:

template<typename T>
const std::string& operator<<=(std::string& s, T value)
{
std::eek:stringstream os;
os << s << value;
s = os.str();
return s;
}

Then you can use it like:

std::string str = "The value is ";
str <<= 10;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top