Files
cppdraft_translate/cppdraft/string/conversions.md
2025-10-25 03:02:53 +03:00

206 lines
7.2 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[string.conversions]
# 27 Strings library [[strings]](./#strings)
## 27.4 String classes [[string.classes]](string.classes#string.conversions)
### 27.4.5 Numeric conversions [string.conversions]
[🔗](#lib:stoi)
`int stoi(const string& str, size_t* idx = nullptr, int base = 10);
long stol(const string& str, size_t* idx = nullptr, int base = 10);
unsigned long stoul(const string& str, size_t* idx = nullptr, int base = 10);
long long stoll(const string& str, size_t* idx = nullptr, int base = 10);
unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5214)
*Effects*: The first two functions call strtol(str.c_str(), ptr, base),
and the last three functions call strtoul(str.c_str(), ptr, base),strtoll(str.c_str(), ptr, base), and strtoull(str.c_str(), ptr,
base), respectively[.](#1.sentence-1)
Each function returns the converted result, if any[.](#1.sentence-2)
The
argument ptr designates a pointer to an object internal to the function
that is used to determine what to store at *idx[.](#1.sentence-3)
If the function does
not throw an exception and idx != nullptr, the function stores in *idx the index of the first unconverted element of str[.](#1.sentence-4)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5225)
*Returns*: The converted result[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5229)
*Throws*: invalid_argument if strtol, strtoul,strtoll, or strtoull reports that no conversion can be
performed[.](#3.sentence-1)
Throws out_of_range if strtol, strtoul,strtoll or strtoull sets errno to ERANGE,
or if the converted value is outside the range of representable values
for the return type[.](#3.sentence-2)
[🔗](#lib:stof)
`float stof(const string& str, size_t* idx = nullptr);
double stod(const string& str, size_t* idx = nullptr);
long double stold(const string& str, size_t* idx = nullptr);
`
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5249)
*Effects*: These functions callstrtof(str.c_str(), ptr), strtod(str.c_str(), ptr), andstrtold(str.c_str(), ptr), respectively[.](#4.sentence-1)
Each function returns
the converted result, if any[.](#4.sentence-2)
The argument ptr designates a pointer to
an object internal to the function that is used to determine what to store at*idx[.](#4.sentence-3)
If the function does not throw an exception and idx != nullptr,
the function stores in *idx the index of the first unconverted element
of str[.](#4.sentence-4)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5260)
*Returns*: The converted result[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5264)
*Throws*: invalid_argument if strtof, strtod, orstrtold reports that no conversion can be performed[.](#6.sentence-1)
Throwsout_of_range if strtof, strtod, orstrtold sets errno to ERANGE or if the converted value is outside the range of representable
values for the return type[.](#6.sentence-2)
[🔗](#lib:to_string)
`string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long 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);
`
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5288)
*Returns*: format("{}", val)[.](#7.sentence-1)
[🔗](#lib:stoi_)
`int stoi(const wstring& str, size_t* idx = nullptr, int base = 10);
long stol(const wstring& str, size_t* idx = nullptr, int base = 10);
unsigned long stoul(const wstring& str, size_t* idx = nullptr, int base = 10);
long long stoll(const wstring& str, size_t* idx = nullptr, int base = 10);
unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
`
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5307)
*Effects*: The first two functions call wcstol(str.c_str(), ptr, base),
and the last three functions call wcstoul(str.c_str(), ptr, base),wcstoll(str.c_str(), ptr, base), and wcstoull(str.c_str(), ptr,
base), respectively[.](#8.sentence-1)
Each function returns the converted result, if any[.](#8.sentence-2)
The
argument ptr designates a pointer to an object internal to the function
that is used to determine what to store at *idx[.](#8.sentence-3)
If the function does
not throw an exception and idx != nullptr, the function stores in *idx the index of the first unconverted element of str[.](#8.sentence-4)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5318)
*Returns*: The converted result[.](#9.sentence-1)
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5322)
*Throws*: invalid_argument if wcstol, wcstoul, wcstoll, orwcstoull reports that no conversion can be performed[.](#10.sentence-1)
Throwsout_of_range if the converted value is outside the range of representable values
for the return type[.](#10.sentence-2)
[🔗](#lib:stof_)
`float stof(const wstring& str, size_t* idx = nullptr);
double stod(const wstring& str, size_t* idx = nullptr);
long double stold(const wstring& str, size_t* idx = nullptr);
`
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5340)
*Effects*: These functions call wcstof(str.c_str(), ptr),wcstod(str.c_str(), ptr), and wcstold(str.c_str(), ptr),
respectively[.](#11.sentence-1)
Each function returns the converted
result, if any[.](#11.sentence-2)
The argument ptr designates a pointer to an object internal to
the function that is used to determine what to store at *idx[.](#11.sentence-3)
If the function
does not throw an exception and idx != nullptr, the function stores in *idx the index of the first unconverted element of str[.](#11.sentence-4)
[12](#12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5350)
*Returns*: The converted result[.](#12.sentence-1)
[13](#13)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5354)
*Throws*: invalid_argument if wcstof, wcstod, or wcstold reports that no
conversion can be performed[.](#13.sentence-1)
Throws out_of_range if wcstof, wcstod, orwcstold sets errno to ERANGE[.](#13.sentence-2)
[🔗](#lib:to_wstring)
`wstring to_wstring(int val);
wstring to_wstring(unsigned val);
wstring to_wstring(long val);
wstring to_wstring(unsigned long val);
wstring to_wstring(long 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);
`
[14](#14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5375)
*Returns*: format(L"{}", val)[.](#14.sentence-1)