views:

303

answers:

5

Where can I find an overview of type conversion, e.g. string to integer, etc.?

Because of the comments so far, I'll clarify: I'm looking for a list /table that says: To convert a string to an int, use: ... And same for other data types (where possible): double to int, char to string, ...

A: 

Ummmm, http://google.com, perhaps?

Larry
don't suggest the obvious. if people are too lazy to try google, suggesting it to them here makes YOU the bad guy.
KevinDTimm
Indeed. _Searching for the exact title of this question on google yields great results_... http://www.google.ca/search?q=C%2B%2B+type+conversion
Cam
Searching Google I found this thread. This suggests me to search Google...
UncleBens
+11  A: 

If it's string to/from other types then stringstream or boost::lexical_cast.

For other types it will depend on the types, but maybe look up the standard cast templates? static_cast and dynamic_cast should do most things you need, or there is const_cast and reinterpret_cast which tend to only be useful for dealing with legacy systems.

jk
I don't mean to self promote, but for reference this answer shows how to use `lexical_cast`, and how to make a simple no-boost replacement with streams as well. http://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/1243435#1243435
GMan
@jk; @GMan - thx for your responses.
Mark Robinson
A: 

There are functions in the standard library to do string<->int conversions. You should grab any reference book on C++, or search Google.

I'm more familiar with C, but I believe the C++ functions are the same: atoi, strtoi, etc.

CWF
See jk's answer for the standard C++ approach.
Bill
+1  A: 

Streams are essentially C++'s string conversion operators.

You also have available C's conversion method of sprintf, but that is massively error-prone and unsafe.

T.E.D.
A: 

I would recommend to read the reference about the string class and then something about casting to different data types generally.

stupid_idiot