to

name

to - do the opposite of fast_io::concate(). Convert string to integer / floating point / etc.

synopsis

#include<fast_io.h>
constexpr T to<T>(args ...)

description

The function to() take one or more strings and returns the conversion result truncated from the first unexpected character (depending on the specified template parameter T).

return_value

The function returns the conversion result with specified type T. Return value type can be specified by specifying the first template parameter T while calling to().

example

convert string to integer:

#include<fast_io.h>

int main() {
    std::string answer{"42"};
    auto result{fast_io::to<uint32_t>(answer)};
    // tell to() what to do by specifying T
    println("the answer is ",result);
}

convert multiple strings to one integer:

#include<fast_io.h>
int main() {
    std::string part1{"123"};
    std::string part2{"456"};
    std::string part3{"789"};

    auto result = fast_io::to<int32_t>(part1,part2,part3);
    println("the result is: ",result);
}

anything after the first unexpected character will be ignored, even if they are expected ones:

#include<fast_io.h>

int main() {
    std::string with_point{"123456789"};
    std::string with_char{"123456A789"};
    std::string with_star{"123456*789"};
    // if the first character is illegal, 0 will be returned.
    std::string with_etc{"~!@#$%^&*()_+{}|:\"<>?`-=[]\\;',./'`"};

    println("with point: ",with_point," -> ",fast_io::to<int32_t>(with_point));
    println("with char: ",with_char," -> ",fast_io::to<int32_t>(with_char));
    println("with star: ",with_star," -> ",fast_io::to<int32_t>(with_star));
    println("with other: ",with_etc," -> ",fast_io::to<int32_t>(with_etc));
}

see also

print

concat