Hello..
Whats the best/right way to convert c++ string to int?
Thanks for help
Hello..
Whats the best/right way to convert c++ string to int?
Thanks for help
How can you convert a string to int anyway?
Do you want the int to contain the length of the string or the ascii character code of the first character? Or something else?
I don't really understand your question...
string test = "432";
int i;
now i want to convert test to i = 432;
Code:string test="432"; int i=atoi(test.c_str());
What about using sstream? Isnt that option better?Originally Posted by maxorator
Yes. Because the way atoi works is... strange... to be nice to it and more often than not forces code to be written in order to tame it.
atoi("12 - Hello Mum!") will gladly be converted to 12.
and atoi("What a wonderful strange thing to return") will gladly return 0.
http://faq.cprogramming.com/cgi-bin/...wer=1046996179 for more control on the conversion.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Hey..
Heres my version of string convertion:
I call it withCode:template <class t> t string_convert(const string &s) { istringstream stream(s); t i; if (stream >> i) return i; return 0; }
int i = string_convert<int>(some_string);
But I get link error.. No idea why..
This can do string to int, or int to string or whatever - http://cboard.cprogramming.com/showp...32&postcount=7
Could you give an example how to use it?Originally Posted by twomers
Read the FAQ, dammit.Code:std::string s("1234321"); int i = jsw::lexical_cast(s);
Thanks.. One more thing, what would be the right way to catch exception? I've never been doing that before. Is this a smart way to do it?Originally Posted by Tonto
> One more thing, what would be the right way to catch exception?
Baseball gloves - exceptions travel at a heck of a speed, so you need something with padding
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
There is no way that's going to work because the compiler doesn't know what type to return. Try passing in a argument to your template func.Originally Posted by l2u
EDIT: nvm, I would use the stringstream option if i had to do thisCode:template <class T> void string_convert(const std::string &str, T &myVar) { .. code here myVar = str converted to T }
EDIT2: I may and probably am wrong about the compiler not knowing what type to return (that is specified by T), however I can say that returning 0 in ALL cases is probably not appropriate.
EDIT3: Bah, I had to get this working here's the code I came up with:
Code:#include <iostream> #include <string> #include <sstream> template <class T> T string_convert(const std::string &s) { std::istringstream stream(s); T i; if (stream >> i) return i; return 0; } int main() { std::string myStr("12345"); std::cout << string_convert<int>(myStr); return 0; }
Last edited by MacNilly; 09-10-2006 at 12:07 PM.