I know there are two ways to do this in the standard lib:
https://en.cppreference.com/w/cpp/string/basic_string/stol
and
https://en.cppreference.com/w/cpp/utility/from_chars
But which one is more efficient and is the recommended way?
64 messages · Page 1 of 1 (latest)
I know there are two ways to do this in the standard lib:
https://en.cppreference.com/w/cpp/string/basic_string/stol
and
https://en.cppreference.com/w/cpp/utility/from_chars
But which one is more efficient and is the recommended way?
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Unlike other parsing functions in C++ and C libraries, std::from_chars is locale-independent, non-allocating, and non-throwing. Only a small subset of parsing policies used by other libraries (such as std::sscanf) is provided. This is intended to allow the fastest possible implementation that is useful in common high-throughput contexts such as text-based interchange (JSON or XML).
So from_chars is pretty minimal?
and it doesn't support most non-ASCII characters?
from_chars is meant as a way to make reproducable floats for text-based interchange as the quote says
its not meant to print nicely (i.e. it does not rounding or whatever)
its just meant to be consistent
if you want to print using to_chars you can then parse the same number using from_chars and itll load it correctly and quickly
and if you take a float from from_chars and print it using to_chars you'll get the same string
thats the reason from_chars/to_chars exist
to_chars also gives you the smallest represtation I think? although I might be wrong on that
if you want to pass in things yourself and you're not just passing it values from to_chars then this:
Floating-point parsers: Expects the pattern identical to the one used by std::strtod in the default ("C") locale
with some exceptions
And if I don't intend to print the output from from_chars?
So just use std::stoull?
std::stoull will be locale-dependent
which is nice I suppose
although I try to avoid all that mess when possible
also form_chars can't do hex
just realised you're doing integers not floats
it basically doesn't matter in this case
these only matter with floats for the most part
integers are effectively trivial to parse compared to floats
How would you?

but if im parsing something specific there are usually specific grammar things that need to be taken into consideration that the standard library c++ might be too lax or too strict
I'm pretty much trying to convert "423345" to ulonglong so
shouldn't be
Since we're here
I don't want to open another post
std::ranges::copy_if(string, std::back_inserter(result), ::isdigit);
If I were to specify whatever that's not a digit, how would I do it while keeping this format?
I tried
std::ranges::copy_if(string, std::back_inserter(result), ::!isdigit);
and
std::ranges::copy_if(string, std::back_inserter(result), !::isdigit);
None are correct syntax-wise
std::not_function(std::isdigit). except that you can't take the address of a std function. and especially one that's probably not actually even a function like isdigit
so it would actually be
std::ranges::copy_if(string, std::back_inserter(result), [](unsigned char c) { return !std::isdigit(c); });
what
you are not allowed to take the address of functions in the std namespace
and isdigit is probably implemented as a macro, not a function, anyway
with some exemptions
like
__exctype (isdigit);
```?
Ik this can be done but I was wondering if I could do it like that
no. you're simply not allowed to take the address of functions in the std namespace
*with some named exceptions
is there a function macro that's called like is_not_digit()
write it.
bool is_not_digit(unsigned char c) { return !std::isdigit(c); }
would that work with the :: ?
constexpr?
no it's not constexpr
:(
It's a string read from a database
Which I first remove all the non-digit chars
and then convert to a ulonglong
i see
I've not heard of this. So something like not allowed?
;compile
std::array values {43, 212, 566};
std::cout << std::accumulate(values.begin(), values.end(), 1, std::lcm<int, int>);
2579828
Or is it only for non template functions?
unspecified or possibly ill-formed
is such a wonderful state of being
Reading up on it, it seems like if it compiles, the result is compiler/library dependent.
These seems to be done to have std functions be implemented in different ways, some of which addressing would not make as much sense.
And function signatures/overloads are subject to breaking changes that could cause code to no longer compile between versions.
TIL I guess
!close