Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ type conversion

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, ...

like image 710
Reinstate Monica - Goodbye SE Avatar asked Dec 23 '22 05:12

Reinstate Monica - Goodbye SE


2 Answers

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.

like image 100
jk. Avatar answered Jan 07 '23 10:01

jk.


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.

like image 35
T.E.D. Avatar answered Jan 07 '23 11:01

T.E.D.