`std::from_chars` Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <charconv> // from_char, to_char
#include <string>
#include <iostream>
int main()
{
const std::string str { "12345678901234" };
int value = 0;
const auto res = std::from_chars(str.data(),
str.data() + str.size(),
value);
if (res.ec == std::errc())
{
std::cout << "value: " << value
<< ", distance: " << res.ptr - str.data() << '\n';
}
else if (res.ec == std::errc::invalid_argument)
{
std::cout << "invalid argument!\n";
}
else if (res.ec == std::errc::result_out_of_range)
{
std::cout << "out of range! res.ptr distance: "
<< res.ptr - str.data() << '\n';
}
}
Enter to Rename, Shift+Enter to Preview