相关疑难解决方法(0)

如何使用gcc打印__uint128_t数字?

PRIu128,其行为类似于PRIu64来自<inttypes.h>:

printf("%" PRIu64 "\n", some_uint64_value);
Run Code Online (Sandbox Code Playgroud)

或者手动逐位转换:

int print_uint128(uint128_t n) {
  if (n == 0)  return printf("0\n");

  char str[40] = {0}; // log10(1 << 128) + '\0'
  char *s = str + sizeof(str) - 1; // start at the end
  while (n != 0) {
    if (s == str) return -1; // never happens

    *--s = "0123456789"[n % 10]; // save last digit
    n /= 10;                     // drop it
  }
  return printf("%s\n", s);
}
Run Code Online (Sandbox Code Playgroud)

是唯一的选择吗? …

c gcc

45
推荐指数
3
解决办法
5万
查看次数

atoi()用于int128_t类型

如何argvint128_t支持下使用值?我知道atoi()暴露的函数和函数系列,<cstdlib>但不知怎的,我找不到一个int128_t固定宽度整数.这可能是因为这type不受c或c ++标准的支持,但是我有什么方法可以让这段代码工作吗?

#include <iostream>

int main(int argc, char **argv) {
    __int128_t value = atoint128_t(argv[1]);
}
Run Code Online (Sandbox Code Playgroud)

几乎所有发布的答案对我来说都足够好了,但我选择的是当前代码的解决方案,所以也要查看其他答案.

c c++ int

15
推荐指数
2
解决办法
1779
查看次数

为什么编译器无法自动匹配范围基 for 循环中“size_t”变量的类型?

我偶然发现了与range一起operator<<使用时存在不明确重载的问题。\n更具体地说,使用以下代码:std::views::enumeratesize_t

\n
#include <iostream>\n#include <ranges>\nnamespace rv = std::ranges::views;\n\nint main()\n{\n    for (const auto& [idx, value] : rv::iota(0zu, 5zu) | rv::enumerate)\n        std::cout << idx << '\\n';\n}\n
Run Code Online (Sandbox Code Playgroud)\n

gcc 13.1.1在 Linux 上使用以下命令进行编译:g++ --std=c++23 main.cpp.\n我收到错误:

\n
main.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\nmain.cpp:11:19: error: ambiguous overload for \xe2\x80\x98operator<<\xe2\x80\x99 (operand types are \xe2\x80\x98std::ostream\xe2\x80\x99 {aka \xe2\x80\x98std::basic_ostream<char>\xe2\x80\x99} and \xe2\x80\x98std::tuple_element<0, const std::tuple<__int128, long unsigned int> >::type\xe2\x80\x99 {aka \xe2\x80\x98const __int128\xe2\x80\x99})\n   11 |         std::cout << idx << '\\n';\n      |         ~~~~~~~~~ ^~ ~\n      |              | …
Run Code Online (Sandbox Code Playgroud)

c++ implicit-conversion std-ranges c++23

5
推荐指数
1
解决办法
220
查看次数

标签 统计

c ×2

c++ ×2

c++23 ×1

gcc ×1

implicit-conversion ×1

int ×1

std-ranges ×1