C++数値タイプとstringの相互変換


1.数値タイプをstringに変換
1.1関数テンプレート+ostringstreamの使用
関数テンプレートを使用して、基本データ型(整数、文字型、実型、ブール型)をstringに変換します.
//ostringstream            ,           string  
//ostringstream   < string toString(const T& t){
    ostringstream oss;  //          
    oss<string:  14.2
cout<string:  12301
cout<string:  123456789785
cout<string:  1

1.2標準ライブラリ関数std::to_を使用するstring()
stdコマンド空間の下にC++標準ライブラリ関数std::to_string()は、数値タイプをstringに変換するために使用できます.使用にはincludeヘッダが必要です.
関数のプロトタイプは次のように明記されています.
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   
#include     //  stringstream           
using namespace std;  

//    : string              (          )  
template   
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(str) << endl;  

    system("pause");  
    return 0;  
}  

2.2 C標準ライブラリ関数の使用
具体的にはstringをchar*文字列に変換し、対応するタイプの変換関数で所望の数値タイプに変換します.標準ライブラリ関数を含める必要があります.  (1)stringからintへの変換
string love="77";
int ilove=atoi(love.c_str());

//  32      long int
int ilove=strtol(love.c_str(),NULL,10);
(2)stringをunsigned intに変換
//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をlong long intに変換
string love="77";
long long llLove=atoi(love.c_str());

(4)stringをunsigned long long intに変換
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を数値タイプに変換し、対応するライブラリ関数をヘッダファイルに明記します.
名前
プロトタイプ
説明
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*のポインタタイプで、デフォルトはNULLです.空でない場合は、変換に成功したときに最初の数値以外の文字の下付き文字を取得します.一般的には、直接charポインタが最後の非数値文字のアドレス値と先頭アドレス値を減算するので、「10」が数値10に変換に成功した場合*idxの値が2になるなど、変換に成功した文字数も表す.
base:変換基準を表し、デフォルトは10進数です.
参考文献
[1]C++ Reference  [2]strtoul.C++ Reference  [2]strtoull.C++ Reference  [3]strtould.C++ Reference
出典:http://blog.csdn.net/k346k346/article/details/50927002