技术
-
这都什么年代了,C++ 从字符串里面解析出一个数值居然还是非常复杂:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream> #include <charconv> int main() { const char* str = "1234"; int num; auto [p, ec] = std::from_chars(str, str + std::strlen(str), num); if (ec != std::errc()) { std::cerr << "Error: invalid integer string\n"; return 1; } if (p != str + std::strlen(str)) { std::cerr << "Error: invalid characters in integer string\n"; return 1; } std::cout << "Parsed integer: " << num << '\n'; return 0; }