Archived

This topic is now archived and is closed to further replies.

atoi and std::string

This topic is 5175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

to use atoi:
std::string snumber = "42";
int i = atoi(snumber.c_str());

I almost always use atoi, but there are better ways I''m told.

I think stlsoft has converters...

you could use a stringstream and inserters/extracters:
stringstring sstrm(snumber);
sstrm >> i;

There are more. I''m sure somebody will enlighten us as to the "best" way

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
atoi isnt standard c++

Use stringstreams, its just as easy


template<typename RT, typename T, typename Traits,typename alloc>
RT better_atoi(const basic_string<T,Traits, alloc>& your_string)
{
RT i;
std::stringstream tempstream = your_string;
tempstream >> i;
return i;
}


Not tested that.. correct me if its wrong

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
atoi isnt standard c++

Of course it is. It''s in C89, so it''s in C++. You''re thinking of itoa.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
atoi isnt standard c++

Use stringstreams, its just as easy


template<typename RT, typename T, typename Traits,typename alloc>
RT better_atoi(const basic_string<T,Traits, alloc>& your_string)
{
RT i;
std::stringstream tempstream = your_string;
tempstream >> i;
return i;
}


Not tested that.. correct me if its wrong

It's not so much that it's wrong, it's just it could be more generic. See boost::lexical_cast. Also note that initialisation of the basic_stringstream (hint) object, using its constructor, would be more favourable.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 11, 2003 4:57:51 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

template<typename RT, typename T, typename Traits, typename Alloc>
RT even_better_atoi(const basic_string<T,Traits,Alloc>& my_string)
{
RT temp_1
basic_istringstream<T,Traits,Alloc> temp_string = my_string;
tempstring >> temp_1;
return temp_1;
}


La?

"Also note that initialisation of the basic_stringstream (hint) object, using its constructor, would be more favourable. "

Thats what the code does.. temp_string(my_string) is the same as temp_string = my_string ... Both call the constructor with the my_string parameter

Share this post


Link to post
Share on other sites
Yes, correct. It's "syntactic sugar". Fleh, switching between languages.

You're sort of nearly there. You need to choose to instantiate the string stream template with the right character type based on both the target and source types. Also it can be improved further (exceptions, limits/precision, etc.). As I've already said, boost::lexical_cast.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 12, 2003 5:55:34 AM]

Share this post


Link to post
Share on other sites
"atoi isnt standard c++"

Small point, but all the old C stuff that''s bundled in C++ *is* standard C++, meaning that a standards conforming compiler *must* support it. printf, strcpy, memcpy, etc. etc. etc. are all C++, but they are also C.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

Share this post


Link to post
Share on other sites