boost常用库(一):boost数值转换

在STL中有一些字符转换函数,例如atoi,itoa等,在boost里面只需用一个函数lexical_cast进行转换,lexical_cast是模板方法,使用时需要传入类型。只能是数值类型转字符串。字符串转数值类型。

使用方法:

1.包含头文件#include

2.命名空间 using namespace boost 或者在使用时boost::lexical_cast.

#include 
#include 
#include <string>
using namespace std;
using namespace boost;
int main()
{
    //这里将字符串转为整型
    int a = lexical_cast<int>("1234");
    int b = lexical_cast<int>("435");
    cout << a + b << endl;
    //整型转字符串
    string c = lexical_cast<string>(123);
    string d = lexical_cast<string>(456);
    cout << c + d << endl;
return 0; }

 

输出

 

你可能感兴趣的:(boost常用库(一):boost数值转换)