August 2017
Intermediate to advanced
438 pages
14h 19m
English
Let's convert integer 100 to std::string using boost::lexical_cast:
#include <cassert>#include <boost/lexical_cast.hpp> void lexical_cast_example() { const std::string s = boost::lexical_cast<std::string>(100); assert(s == "100");}
Compare it against the traditional C++ conversion method:
#include <cassert>#include <sstream> void cpp_convert_example() { std::stringstream ss; // Slow/heavy default constructor. ss << 100; std::string s; ss >> s; // Variable 'ss' will dangle all the way, till the end // of scope. Multiple virtual methods and heavy // operations were called during the conversion. assert(s == "100");}
And against the C conversion method:
#include <cassert>#include <cstdlib> void c_convert_example() { ...Read now
Unlock full access