iostream重载__int128

Normal (Naive)写法,用 string(char* )

std::ostream& operator <<(std::ostream&out,const  __int128 b) {
  std::string s;  __int128 t = b;int sig = 1;
  if(t < 0) sig = -1,t = -t;
  for(;t;t/=10) s += '0' + t % 10;
  if(sig == -1) s += '-';
  reverse(s.begin(), s.end());
  if(s.length() == 0) s += '0';
  out << s ;
  return out;
}
/********* istrream 类似读入挂 O(∩_∩)O *************/

我突然有个大胆的想法系列

std::ostream& operator <<(std::ostream&out, __int128 x) {
  if(x < 0) {out << "-"; out << -x; return out;}
  if(x == 0) {out << "0"; return out;}
  if(x > 10) out << x / 10;
  out << "0123456789"[x % 10];
  return out;
}

std::istream& operator >>(std::istream&in, __int128 &x) {
  char c;
  while(c = in.get(), c != '-' && !isdigit(c));
  if(c == '-') {x = '0' - (c = in.get()); while(isdigit(c = getchar()))x = x * 10 + '0' - c;}
  else {x = c - '0'; while(isdigit(c = in.get()))x = x * 10 - '0' + c;};
  return in;
}
posted @   菁芜  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
阅读排行:
· 基于Docker+DeepSeek+Dify :搭建企业级本地私有化知识库超详细教程
· 由 MCP 官方推出的 C# SDK,使 .NET 应用程序、服务和库能够快速实现与 MCP 客户端
· 电商平台中订单未支付过期如何实现自动关单?
· 上周热点回顾(3.31-4.6)
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
点击右上角即可分享
微信分享提示