#Converting a std::string to unsigned long long

64 messages · Page 1 of 1 (latest)

pliant nimbus
trim shellBOT
#

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.

solid current
#

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).

pliant nimbus
#

So from_chars is pretty minimal?

#

and it doesn't support most non-ASCII characters?

dawn chasm
#

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

dawn chasm
# pliant nimbus So from_chars is pretty minimal?

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

pliant nimbus
#

And if I don't intend to print the output from from_chars?

#

So just use std::stoull?

dawn chasm
#

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

pliant nimbus
dawn chasm
#

by not using them pogchamp

#

I would use from_chars probably if I was using something standard

pliant nimbus
dawn chasm
#

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

pliant nimbus
#

I'm pretty much trying to convert "423345" to ulonglong so

dawn chasm
#

I mean yes just use stoull

#

if its not a big deal

pliant nimbus
#

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

solid current
#

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); });
solid current
#

and isdigit is probably implemented as a macro, not a function, anyway

pliant nimbus
pliant nimbus
solid current
#

no. you're simply not allowed to take the address of functions in the std namespace

#

*with some named exceptions

pliant nimbus
#

is there a function macro that's called like is_not_digit()

solid current
#

write it.
bool is_not_digit(unsigned char c) { return !std::isdigit(c); }

pliant nimbus
#

would that work with the :: ?

pliant nimbus
burnt field
#

:(

pliant nimbus
#

It's a string read from a database

#

Which I first remove all the non-digit chars

#

and then convert to a ulonglong

burnt field
#

i see

coral hornet
#

;compile

std::array values {43, 212, 566};
std::cout << std::accumulate(values.begin(), values.end(), 1, std::lcm<int, int>);
safe gateBOT
#
Program Output
2579828
coral hornet
#

Or is it only for non template functions?

solid current
coral hornet
#

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

pliant nimbus
#

!close