Boost.LexicalCast which is defined in the Library “boost/lexical_cast.hpp” provides a cast operator, boost::lexical_cast, that can convert numbers from strings to numeric types like int or double and vice versa.
boost::lexical_cast is an alternative to functions like std::stoi(), std::stod(), and std::to_string(), which were added to the standard library in C++11.
Now let see the Implementation of this function in a program.
Conversion integer -> string string- > integer integer -> char float- > string
Associated exceptions : If a conversion fails, an exception of type bad_lexical_cast, which is derived from bad_cast, is thrown. It throws an exception because the float 52.50 and the string “GeeksforGeeks” cannot be converted to a number of type int.
23 45 1234 Exception caught : bad lexical cast: source type value could not be interpreted as target Exception caught :bad lexical cast: source type value could not be interpreted as target
Reference :- http://www.boost.org/
- Different ways to use Const with Reference to a Pointer in C++
- std::to_address in C++ with Examples
- Maximum repeated frequency of characters in a given string
- Count of even and odd set bit with array element after XOR with K
- Program to create Custom Vector Class in C++
- std::is_trivially_copy_constructible in C/C++
- Difference between Python and C++
- tgamma() method in C/C++ with Examples
- Find K distinct positive odd integers with sum N
- boost::type_traits::is_array Template in C++
- boost is_pointer template in C++
- Radio Button Vs Checkbox in HTML
- Three-State Bus Buffers
- Modulo Operator (%) in C/C++ with Examples
- fpclassify() method in C/C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.