以下代码都是string头文件里,使用例子
int num;
std::string strNum;
strNum = std::to_string(1234);
num = std::stoi(strNum)
数值类型转string,wstring
// to_string NARROW CONVERSIONS
inline string to_string(int _Val)
{ // convert int to string
return (_Integral_to_string<char>(_Val));
}
inline string to_string(unsigned int _Val)
{ // convert unsigned int to string
return (_Integral_to_string<char>(_Val));
}
inline string to_string(long _Val)
{ // convert long to string
return (_Integral_to_string<char>(_Val));
}
inline string to_string(unsigned long _Val)
{ // convert unsigned long to string
return (_Integral_to_string<char>(_Val));
}
inline string to_string(long long _Val)
{ // convert long long to string
return (_Integral_to_string<char>(_Val));
}
inline string to_string(unsigned long long _Val)
{ // convert unsigned long long to string
return (_Integral_to_string<char>(_Val));
}
inline string to_string(float _Val)
{ // convert float to string
return (_Floating_to_string("%f", _Val));
}
inline string to_string(double _Val)
{ // convert double to string
return (_Floating_to_string("%f", _Val));
}
inline string to_string(long double _Val)
{ // convert long double to string
return (_Floating_to_string("%Lf", _Val));
}
// to_wstring WIDE CONVERSIONS
inline wstring to_wstring(int _Val)
{ // convert int to wstring
return (_Integral_to_string<wchar_t>(_Val));
}
inline wstring to_wstring(unsigned int _Val)
{ // convert unsigned int to wstring
return (_Integral_to_string<wchar_t>(_Val));
}
inline wstring to_wstring(long _Val)
{ // convert long to wstring
return (_Integral_to_string<wchar_t>(_Val));
}
inline wstring to_wstring(unsigned long _Val)
{ // convert unsigned long to wstring
return (_Integral_to_string<wchar_t>(_Val));
}
inline wstring to_wstring(long long _Val)
{ // convert long long to wstring
return (_Integral_to_string<wchar_t>(_Val));
}
inline wstring to_wstring(unsigned long long _Val)
{ // convert unsigned long long to wstring
return (_Integral_to_string<wchar_t>(_Val));
}
inline wstring to_wstring(float _Val)
{ // convert float to wstring
return (_Floating_to_wstring(L"%f", _Val));
}
inline wstring to_wstring(double _Val)
{ // convert double to wstring
return (_Floating_to_wstring(L"%f", _Val));
}
inline wstring to_wstring(long double _Val)
{ // convert long double to wstring
return (_Floating_to_wstring(L"%Lf", _Val));
}
string,wstring转int等数值类型
// sto* NARROW CONVERSIONS
inline int stoi(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to int
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
long _Ans = _CSTD strtol(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoi argument");
if (errno == ERANGE || _Ans < INT_MIN || INT_MAX < _Ans)
_Xout_of_range("stoi argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return ((int)_Ans);
}
inline long stol(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to long
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
long _Ans = _CSTD strtol(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stol argument");
if (errno == ERANGE)
_Xout_of_range("stol argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline unsigned long stoul(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to unsigned long
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
unsigned long _Ans = _CSTD strtoul(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoul argument");
if (errno == ERANGE)
_Xout_of_range("stoul argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline long long stoll(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to long long
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
long long _Ans = _CSTD strtoll(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoll argument");
if (errno == ERANGE)
_Xout_of_range("stoll argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline unsigned long long stoull(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to unsigned long long
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
unsigned long long _Ans = _CSTD strtoull(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoull argument");
if (errno == ERANGE)
_Xout_of_range("stoull argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline float stof(const string& _Str, size_t *_Idx = 0)
{ // convert string to float
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
float _Ans = _CSTD strtof(_Ptr, &_Eptr);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stof argument");
if (errno == ERANGE)
_Xout_of_range("stof argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline double stod(const string& _Str, size_t *_Idx = 0)
{ // convert string to double
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
double _Ans = _CSTD strtod(_Ptr, &_Eptr);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stod argument");
if (errno == ERANGE)
_Xout_of_range("stod argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline long double stold(const string& _Str, size_t *_Idx = 0)
{ // convert string to long double
const char *_Ptr = _Str.c_str();
char *_Eptr;
errno = 0;
long double _Ans = _CSTD strtold(_Ptr, &_Eptr);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stold argument");
if (errno == ERANGE)
_Xout_of_range("stold argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
// sto* WIDE CONVERSIONS
inline int stoi(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to int
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
long _Ans = _CSTD wcstol(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoi argument");
if (errno == ERANGE || _Ans < INT_MIN || INT_MAX < _Ans)
_Xout_of_range("stoi argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return ((int)_Ans);
}
inline long stol(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to long
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
long _Ans = _CSTD wcstol(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stol argument");
if (errno == ERANGE)
_Xout_of_range("stol argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline unsigned long stoul(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to unsigned long
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
unsigned long _Ans = _CSTD wcstoul(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoul argument");
if (errno == ERANGE)
_Xout_of_range("stoul argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline long long stoll(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to long long
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
long long _Ans = _CSTD wcstoll(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoll argument");
if (errno == ERANGE)
_Xout_of_range("stoll argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline unsigned long long stoull(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to unsigned long long
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
unsigned long long _Ans = _CSTD wcstoull(_Ptr, &_Eptr, _Base);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoull argument");
if (errno == ERANGE)
_Xout_of_range("stoull argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline float stof(const wstring& _Str, size_t *_Idx = 0)
{ // convert wstring to float
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
float _Ans = _CSTD wcstof(_Ptr, &_Eptr);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stof argument");
if (errno == ERANGE)
_Xout_of_range("stof argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline double stod(const wstring& _Str, size_t *_Idx = 0)
{ // convert wstring to double
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
double _Ans = _CSTD wcstod(_Ptr, &_Eptr);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stod argument");
if (errno == ERANGE)
_Xout_of_range("stod argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline long double stold(const wstring& _Str, size_t *_Idx = 0)
{ // convert wstring to long double
const wchar_t *_Ptr = _Str.c_str();
wchar_t *_Eptr;
errno = 0;
long double _Ans = _CSTD wcstold(_Ptr, &_Eptr);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stold argument");
if (errno == ERANGE)
_Xout_of_range("stold argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}