string类型和数值类型之间的转换

一、C++11新特性中string类型和数值类型的转换方法
1、数值类型转string函数
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

wstring to_wstring (int val);
wstring to_wstring (long val);
wstring to_wstring (long long val);
wstring to_wstring (unsigned val);
wstring to_wstring (unsigned long val);
wstring to_wstring (unsigned long long val);
wstring to_wstring (float val);
wstring to_wstring (double val);
wstring to_wstring (long double val);

2、string转化成数值类型函数:
float stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
double stod (const string& str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
long stol (const string& str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
long double stold (const string& str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
long long stoll (const string& str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const string& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
说明:函数中参数为3个的表示该函数返回string类型的子串,idx是size_t指针用来保存str中第一个非数值字符的下标,base表示转换基数

3、简单测试程序

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    int aInt = 702;
    float bFloat = 7.2;
    string aStr = to_string(aInt);
    string bStr = to_string(bFloat);

    cout << "aStr=" << aStr << "  bStr=" << bStr << endl;

    string cStr = "1234";
    string dStr = "123.4";
    int cInt = stoi(cStr);
    float dFloat = stof(dStr);
    cout << "cInt=" << cInt << "  dFloat=" << dFloat << endl;

    string eStr= "1234,ISMILELI";
    size_t sz;
    int eInt = stoi(eStr,&sz,10); // 
    cout << "eInt=" << eInt << "  str:" << eStr.substr(sz)<<endl;

    int fInt = stoi(eStr,&sz,16);
    cout << "fInt=" << fInt << " fStr:" << eStr.substr(sz)<<endl;
    //cout << "Hello World!" << endl;
    return 0;
}

结果:
这里写图片描述

二、使用模板和流操作

#include <iostream>
#include <string>
#include <sstream>
#include <complex>

using namespace std;
//string转化成数值
template <typename T> T fromString(const string &str)
{
    istringstream is(str);
    T t;
    is >> t;
    return t;
}
// 数值转化成string
template <typename T> string toString(const T &t)
{
    ostringstream os;
    os << t;
    return os.str();
}

int main()
{
    int a = 123;
    float b = 1.23;
    complex<float> c(2.5,4.3);
    cout << "a=" << toString(a) << "\nb=" << toString(b) << "\nc=" << toString(c) << endl;
    cout << "==============test fromString==============" << endl;
    cout << "int:" << fromString<int>(string("123")) << "\nfloat:" << fromString<float>(string("4.56")) << endl;
    return 0;
}

结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/toby54king/article/details/81269686
今日推荐