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;
}
原文地址:https://www.cnblogs.com/Forgenvueory/p/7725116.html