1.數值類型轉換為string
1.1使用函數模板+ostringstream
使用函數模板將基本數據類型(整型、字符型、實型、布爾型)轉換成string。
//ostringstream對象用來進行格式化的輸出,常用於將各種類型轉換為string類型
//ostringstream只支持<<操作符
template<typename T> string toString(const T& t){
ostringstream oss; //創建一個格式化輸出流
oss<<t; //把值傳遞如流中
return oss.str();
}
cout<<toString(14.2)<<endl; //實型->string:輸出14.2
cout<<toString(12301)<<endl; //整型->string:輸出12301
cout<<toString(123456789785)<<endl;//長整型->string:輸出123456789785
cout<<toString(true)<<endl; //布爾型->string:輸出1
1.2使用標准庫函數std::to_string()
std命令空間下有一個C++標准庫函數std::to_string(),可用於將數值類型轉換為string。使用時需要include頭文件<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);
2.string轉換為數值類型
2.1使用函數模板+ istringstream
stringstream在int或float類型轉換為string類型的方法中已經介紹過, 這里也能用作將string類型轉換為常用的數值類型。
#include <iostream>
#include <sstream> //使用stringstream需要引入這個頭文件
using namespace std;
//模板函數:將string類型變量轉換為常用的數值類型(此方法具有普遍適用性)
template <class Type>
Type stringToNum(const string& str){
istringstream iss(str);
Type num;
iss >> num;
return num;
}
int main(int argc, char* argv[]) {
string str("00801");
cout << stringToNum<int>(str) << endl;
system("pause");
return 0;
}
2.2使用C標准庫函數
具體做法是先將string轉換為char*字符串,再通過相應的類型轉換函數轉換為想要的數值類型。需要包含標准庫函數<stdlib.h>。
(1)string轉換為int32_t
string love="77"; int ilove=atoi(love.c_str()); //或者16位平台轉換為long int int ilove=strtol(love.c_str(),NULL,10);
(2)string轉換為uint32_t
//str:待轉換字符串 //endptr:指向str中數字后第一個非數字字符 //base:轉換基數(進制),范圍從2至36 unsigned long int strtoul (const char* str, char** endptr, int base); #示例 string love="77"; unsigned long ul; ul = strtoul(love.c_str(), NULL, 10);
(3)string轉換為uint64_t
string love="77"; long long llLove=atoll(love.c_str());
(4)string轉換為uint64_t
unsigned long long int strtoull (const char* str, char** endptr, int base); #示例 string love="77"; unsigned long long ull; ull = strtoull (love.c_str(), NULL, 0);
(5)string轉換為float或double
string love="77.77"; float fLove=atof(love.c_str()); double dLove=atof(love.c_str());
(6)string轉換為long double
long double strtold (const char* str, char** endptr);
2.3使用C++標准庫函數
使用C++11引入的C++庫函數將string轉換為數值類型,相應的庫函數申明於頭文件<string>中。
| 名稱 | 原型 | 說明 |
|---|---|---|
| stoi | int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to integer (function template ) |
| stol | long stol (const string& str, size_t* idx = 0, int base = 10); long stol (const wstring& str, size_t* idx = 0, int base = 10); |
Convert string to long int (function template) |
| stoul | 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); |
Convert string to unsigned integer (function template) |
| stoll | 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); |
Convert string to long long (function template) |
| stoull | 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); |
Convert string to unsigned long long (function template) |
| stof | float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); |
Convert string to float (function template ) |
| stod | double stod (const string& str, size_t* idx = 0); double stod (const wstring& str, size_t* idx = 0); |
Convert string to double (function template ) |
| stold | long double stold (const string& str, size_t* idx = 0); long double stold (const wstring& str, size_t* idx = 0); |
Convert string to long double (function template) |
形參說明:
str:重載了string和wstring版本,表示被轉換的字符串。
idx:表示一個size_t*的指針類型,默認為空值。不為空時,轉換成功時獲取第一個非數值字符的下標。一般情況下,因為它是直接char型指針把最后非數值字符的地址值和起始地址值相減,所以也表示成功轉換的字符數量,如”10”轉成功為數值10時,*idx的值為2。
base:表示轉換基准,默認是10進制。
參考文獻
[1]C++ Reference
[2]strtoul.C++ Reference
[2]strtoull.C++ Reference
[3]strtold.C++ Reference
