gpt4 book ai didi

c++ - C++ 标准库中的 std::strtoul 等价物?

转载 作者:行者123 更新时间:2023-11-30 00:53:07 25 4
gpt4 key购买 nike

考虑以下示例:

#include <iostream>
#include <clocale>
#include <cstdlib>
#include <string>
int main()
{
std::setlocale(LC_ALL, "en_US.utf8");
std::string s = "03A0";
wchar_t wstr = std::strtoul(s.c_str(), nullptr, 16);
std::wcout << wstr;
}

这会输出 ΠColiru .

问题

std::strtoul , 来自 <cstdlib> .我完全可以使用它,但我想知道上面的示例是否可以仅使用 C++ 标准库(可能是 stringstreams)?

另请注意,没有前缀 0x在表示十六进制的字符串上。

最佳答案

当然,std::stoul :

wchar_t wstr = std::stoul(s, nullptr, 16);

主要区别在于它可以针对错误抛出异常。

关于c++ - C++ 标准库中的 std::strtoul 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17334663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com