quote:Original post by OdoacerNonstandard.
whoa guys. How about itoa()?
Integer to String
Problem with itoa() is it not available on many platforms. Most platforms just assume you''re going to use sprintf() if it''s a C based platform or the newer boost::lexical_cast or std::ostringstream if you''re using the c++ method. Herb Sutter has written a very good comparison article between all the string formatters of the realm and you can find the article here
quote:Original post by Beer Hunterquote:Original post by OdoacerNonstandard.
whoa guys. How about itoa()?
Isn''t stdlib.h standard? If so, I would think it would be available on all platforms...?
If not, oh well. I learned something today - don''t trust cplusplus.com!
No, the lesson is to read the entire entry. It says specifically in the Portability section that itoa() is not ANSI.
quote:Original post by SiCranequote:Original post by glassJAw
int blah = 5;
string s;
s = blah;
This doesn''t do what you think it does. This creates a string where the first character is equal to a non-printing ascii character. s will not equal "5".
Whoops. My mistake.
I guess a stringstream is needed then.
int blah = 5;
stringstream ss;
string s;
ss << blah;
s = ss.str(); //or ''ss >> s;''
quote:Original post by SiCrane
No, the lesson is to read the entire entry. It says specifically in the Portability section that itoa() is not ANSI.
Whoops. Yeah, didn''t see that. Heh
cplusplus.com tends to have examples/information that predates the 98 C++ Standard. like for instance they use the non-templated versions of the headers (which won''t work in VC++ 2003 btw). So just be weary of it from that point of view.
You guys work too hard:
Cheers
HTH
[edited by - countermind on January 4, 2004 2:46:30 AM]
#include <stdio.h> // for sprintf()
#include <iostream.h>
int main(int argc, char**argv)
{
int my_int = 13;
float my_float = 12.3;
char buf[25];
sprintf(buf, "int = %i\n float = %f\n", my_int, my_float);
cout << buf << endl;
}
Cheers
HTH
[edited by - countermind on January 4, 2004 2:46:30 AM]
Main problem with your approach is type safety. There are a host of other problems with sprintf() that is talked about in the herb sutter article quoted above. boost::lexical_cast provides a nice solution:
I'm not taking care of the exceptions that might be raised there either. Another thing I personally prefer the sprintf() style of syntax for anything more complex than type conversion over a stringstream but obviously sprintf() with all its hosts of problem just isn't an option but another boost library boost::format() provides sprintf() like functionality without the problems.
[edited by - deepdene on January 4, 2004 12:15:46 PM]
int my_int = 13;float my_float 12.3f;std::string mystring = boost::lexical_cast<std::string>(my_int);mystring += boost::lexical_cast<std::string>(my_float);
I'm not taking care of the exceptions that might be raised there either. Another thing I personally prefer the sprintf() style of syntax for anything more complex than type conversion over a stringstream but obviously sprintf() with all its hosts of problem just isn't an option but another boost library boost::format() provides sprintf() like functionality without the problems.
quote:Original post by countermind
You guys work too hard:
#include <stdio.h> // for sprintf()
#include <iostream.h>
int main(int argc, char**argv)
{
int my_int = 13;
float my_float = 12.3;
char buf[25];
sprintf(buf, "int = %i\n float = %f\n", my_int, my_float);
cout << buf << endl;
}
Cheers
HTH
[edited by - countermind on January 4, 2004 2:46:30 AM]
[edited by - deepdene on January 4, 2004 12:15:46 PM]
Yes, you should use a C++ string stream, as many people have mentioned. However, people have only mentioned the string streams whose character types are of type
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on January 4, 2004 10:32:30 AM]
char
, which is not a very generic/reusable solution. boost::lexical_cast
, on the other hand, uses simple template metaprogramming to use the correct template arguments to instantiate std::basic_stringstream
with, which makes it the best reusable solution.
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
extern int get_score();
...
{
...
int score = get_score();
std::string out = "Your score: " + boost::lexical_cast<std::string>(score);
SomeAPIOutputFunction(..., out);
int other_score = get_score();
std::wstring new_out = L"Score: " + boost::lexical_cast<std::wstring>(other_score);
std::wcout << new_out << std::endl;
...
}
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on January 4, 2004 10:32:30 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement