STRING to INT
Don''t use char*. Use string.
#include <string>#include <sstream>#include <iostream>#include <algorithm>#include <functional>using namespace std;int main() { string text = "CAMERA10"; //erase everything but numbers text.erase( remove_if(text.begin(), text.end(), not1(ptr_fun(isdigit))) , text.end()); //prints out "10" cout << text << endl; //now lets convert that "10" to a number int number; stringstream ss; ss << text; ss >> number; //prints out "10" again. Now ''number'' is 10 cout << number << endl;}
the C library function atoi and other string functions, look in MSDN for it
Thats that last thing you need slowing down your program is a string class wasting memory all over the place
Whatever works for you though!
Thats that last thing you need slowing down your program is a string class wasting memory all over the place
Whatever works for you though!
No I don''t have my flying car yet! Or my robot maid or a space odessy.
I see nothing wrong with the old string functions for what I use them for. Of course if your program is based entirely on string processing I am sure a class is needed
I see nothing wrong with the old string functions for what I use them for. Of course if your program is based entirely on string processing I am sure a class is needed
quote:Original post by Anonymous PosterWhat does the 21st century have to do with being wasteful? Is being wasteful goin to be more accepted in the future? Don''t think so...
Cybertron, didn''t you notice it''s already the 21st century?
To the original poster, you need to know your data. For example, is it always going to be contiguous letters followed by a number? If so, you could do something like this:
// untested codeint StripNumber (char * s) { for (; !isdigit(*s); s++) ; return atoi(s);}
quote:Original post by RussellI call it being at a higher level. Buffer overflows are the cause of many security hazards and bugs in current systems. Most commonly these are caused by primitive string management and too short buffers. People just can''t handle the lower level with the care it requires. For example, the function you gave will make the program crash if the string doesn''t contain any numbers.
What does the 21st century have to do with being wasteful? Is being wasteful goin to be more accepted in the future? Don''t think so...
quote:Original post by Anonymous Poster
For example, the function you gave will make the program crash if the string doesn''t contain any numbers.
quote:Original post by Russell For example, is it always going to be contiguous letters followed by a number? If so, you could do something like this
Besides, it depends what you''re doing. You talk about how horrible the security issues are from buffer overflows, and if I''m writing a web server, then maybe I need to think about that, and maybe I should also think about whether I want my web server''s performance to suffer from reallocating memory and copying strings left and right. Since most of us are not writing web servers, this probably isn''t that big of a deal.
quote:Original post by Russell
Besides, it depends what you''re doing. You talk about how horrible the security issues are from buffer overflows, and if I''m writing a web server, then maybe I need to think about that, and maybe I should also think about whether I want my web server''s performance to suffer from reallocating memory and copying strings left and right. Since most of us are not writing web servers, this probably isn''t that big of a deal.
True, but you should always do error checking even when writing the simplest code. This way it becomes a habit and when you work on bigger projects, you get less bug creep.
First make it work,
then make it fast.
--Brian Kernighan
"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement