📄 string
字号:
basic_string<_Elem, _Traits, _Alloc>& _Str,
const _Elem _Delim)
{ // get characters into string, discard delimiter
typedef basic_istream<_Elem, _Traits> _Myis;
ios_base::iostate _State = ios_base::goodbit;
bool _Changed = false;
const typename _Myis::sentry _Ok(_Istr, true);
if (_Ok)
{ // state okay, extract characters
_TRY_IO_BEGIN
_Str.erase();
const typename _Traits::int_type _Metadelim =
_Traits::to_int_type(_Delim);
typename _Traits::int_type _Meta = _Istr.rdbuf()->sgetc();
for (; ; _Meta = _Istr.rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (_Traits::eq_int_type(_Meta, _Metadelim))
{ // got a delimiter, discard it and quit
_Changed = true;
_Istr.rdbuf()->sbumpc();
break;
}
else if (_Str.max_size() <= _Str.size())
{ // string too large, quit
_State |= ios_base::failbit;
break;
}
else
{ // got a character, add it to string
_Str += _Traits::to_char_type(_Meta);
_Changed = true;
}
_CATCH_IO_(_Istr)
}
if (!_Changed)
_State |= ios_base::failbit;
_Istr.setstate(_State);
return (_Istr);
}
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_istream<_Elem, _Traits>& getline(
basic_istream<_Elem, _Traits> && _Istr,
basic_string<_Elem, _Traits, _Alloc>& _Str)
{ // get characters into string, discard newline
return (getline(_Istr, _Str, _Istr.widen('\n')));
}
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr,
basic_string<_Elem, _Traits, _Alloc>& _Str)
{ // extract a string
return (_STD move(_Istr) >> _Str);
}
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_istream<_Elem, _Traits>& getline(
basic_istream<_Elem, _Traits>& _Istr,
basic_string<_Elem, _Traits, _Alloc>& _Str,
const _Elem _Delim)
{ // get characters into string, discard delimiter
return (getline(_STD move(_Istr), _Str, _Delim));
}
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_istream<_Elem, _Traits>& getline(
basic_istream<_Elem, _Traits>& _Istr,
basic_string<_Elem, _Traits, _Alloc>& _Str)
{ // get characters into string, discard newline
return (getline(_STD move(_Istr), _Str, _Istr.widen('\n')));
}
template<class _Elem,
class _Traits,
class _Alloc> inline
basic_ostream<_Elem, _Traits>& operator<<(
basic_ostream<_Elem, _Traits>& _Ostr,
const basic_string<_Elem, _Traits, _Alloc>& _Str)
{ // insert a string
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef basic_string<_Elem, _Traits, _Alloc> _Mystr;
typedef typename _Mystr::size_type _Mysizt;
ios_base::iostate _State = ios_base::goodbit;
_Mysizt _Size = _Str.size();
_Mysizt _Pad = _Ostr.width() <= 0 || (_Mysizt)_Ostr.width() <= _Size
? 0 : (_Mysizt)_Ostr.width() - _Size;
const typename _Myos::sentry _Ok(_Ostr);
if (!_Ok)
_State |= ios_base::badbit;
else
{ // state okay, insert characters
_TRY_IO_BEGIN
if ((_Ostr.flags() & ios_base::adjustfield) != ios_base::left)
for (; 0 < _Pad; --_Pad) // pad on left
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
if (_State == ios_base::goodbit
&& _Ostr.rdbuf()->sputn(_Str.c_str(), _Size) != (streamsize)_Size)
_State |= ios_base::badbit;
else
for (; 0 < _Pad; --_Pad) // pad on right
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
_Ostr.width(0);
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
#if _HAS_CPP0X
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;
int _Errno = 0;
long _Ans = _CSTD _Stolx(_Ptr, &_Eptr, _Base, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoi argument");
if (_Errno || _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;
int _Errno = 0;
long _Ans = _CSTD _Stoulx(_Ptr, &_Eptr, _Base, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stol argument");
if (_Errno)
_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;
int _Errno = 0;
unsigned long _Ans = _CSTD _Stoulx(_Ptr, &_Eptr, _Base, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoul argument");
if (_Errno)
_Xout_of_range("stoul argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline _Longlong stoll(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to long long
const char *_Ptr = _Str.c_str();
char *_Eptr;
int _Errno = 0;
_Longlong _Ans = _CSTD _Stollx(_Ptr, &_Eptr, _Base, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoll argument");
if (_Errno)
_Xout_of_range("stoll argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
inline _ULonglong stoull(const string& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert string to unsigned long long
const char *_Ptr = _Str.c_str();
int _Errno = 0;
char *_Eptr;
_ULonglong _Ans = _CSTD _Stoullx(_Ptr, &_Eptr, _Base, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stoull argument");
if (_Errno)
_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();
int _Errno = 0;
char *_Eptr;
float _Ans = _CSTD _Stofx(_Ptr, &_Eptr, 0, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stof argument");
if (_Errno)
_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();
int _Errno = 0;
char *_Eptr;
double _Ans = _CSTD _Stodx(_Ptr, &_Eptr, 0, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stod argument");
if (_Errno)
_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();
int _Errno = 0;
char *_Eptr;
long double _Ans = _CSTD _Stoldx(_Ptr, &_Eptr, 0, &_Errno);
if (_Ptr == _Eptr)
_Xinvalid_argument("invalid stold argument");
if (_Errno)
_Xout_of_range("stold argument out of range");
if (_Idx != 0)
*_Idx = (size_t)(_Eptr - _Ptr);
return (_Ans);
}
#define _LLFMT "%I64"
inline string to_string(_Longlong _Val)
{ // convert long long to string
char _Buf[2 * _MAX_INT_DIG];
_CSTD sprintf_s(_Buf, sizeof (_Buf), _LLFMT "d", _Val);
return (string(_Buf));
}
inline string to_string(_ULonglong _Val)
{ // convert unsigned long long to string
char _Buf[2 * _MAX_INT_DIG];
_CSTD sprintf_s(_Buf, sizeof (_Buf), _LLFMT "u", _Val);
return (string(_Buf));
}
inline string to_string(long double _Val)
{ // convert long double to string
char _Buf[_MAX_EXP_DIG + _MAX_SIG_DIG + 64];
_CSTD sprintf_s(_Buf, sizeof (_Buf), "%Lg", _Val);
return (string(_Buf));
}
// WIDE STRING VERSIONS
inline string _Narrow_str(wstring _Str)
{ // convert wchar_t string to char string
string _Ans;
for (const wchar_t *_Ptr = _Str.c_str(); *_Ptr != 0; ++_Ptr)
_Ans.push_back((char)_CSTD wctob(*_Ptr));
return (_Ans);
}
inline int stoi(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to int
return (stoi(_Narrow_str(_Str), _Idx, _Base));
}
inline long stol(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to long
return (stol(_Narrow_str(_Str), _Idx, _Base));
}
inline unsigned long stoul(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to unsigned long
return (stoul(_Narrow_str(_Str), _Idx, _Base));
}
inline _Longlong stoll(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to long long
return (stoll(_Narrow_str(_Str), _Idx, _Base));
}
inline _ULonglong stoull(const wstring& _Str, size_t *_Idx = 0,
int _Base = 10)
{ // convert wstring to unsigned long long
return (stoull(_Narrow_str(_Str), _Idx, _Base));
}
inline float stof(const wstring& _Str, size_t *_Idx = 0)
{ // convert wstring to float
return (stof(_Narrow_str(_Str), _Idx));
}
inline double stod(const wstring& _Str, size_t *_Idx = 0)
{ // convert wstring to double
return (stod(_Narrow_str(_Str), _Idx));
}
inline long double stold(const wstring& _Str, size_t *_Idx = 0)
{ // convert wstring to long double
return (stold(_Narrow_str(_Str), _Idx));
}
#define _WLLFMT L"%I64"
inline wstring to_wstring(_Longlong _Val)
{ // convert long long to wstring
wchar_t _Buf[2 * _MAX_INT_DIG];
_CSTD swprintf(_Buf, sizeof (_Buf) / sizeof (_Buf[0]),
_WLLFMT L"d", _Val);
return (wstring(_Buf));
}
inline wstring to_wstring(_ULonglong _Val)
{ // convert unsigned long long to wstring
wchar_t _Buf[2 * _MAX_INT_DIG];
_CSTD swprintf(_Buf, sizeof (_Buf) / sizeof (_Buf[0]),
_WLLFMT L"u", _Val);
return (wstring(_Buf));
}
inline wstring to_wstring(long double _Val)
{ // convert long double to wstring
wchar_t _Buf[_MAX_EXP_DIG + _MAX_SIG_DIG + 64];
_CSTD swprintf(_Buf,sizeof (_Buf) / sizeof (_Buf[0]),
L"%Lg", _Val);
return (wstring(_Buf));
}
#endif /* _HAS_CPP0X */
_STD_END
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _STRING */
/*
* Copyright (c) 1992-2009 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.20:0009 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -