C++11——整型数字与字符串相互转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liangzhao_jay/article/details/87872493

C++11提供了 to_string 、stoxxx方法, 示例代码如下:

#include <iostream>
#include <string>

using namespace std;

//数字转字符串
void numberTostr()
{
    string strInt = std::to_string(100);
    cout<<"strInt = "<<strInt<<endl;
}

//字符串转数字
void strToNum()
{
    string strInt("100");
    int num = std::stoi(strInt);
    cout<<"num = "<<num<<endl;
}

int main()
{
    numberTostr();
    strToNum();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liangzhao_jay/article/details/87872493