从2部分中正确打印出整数

| 我有一个
uint128_t
类,将其值存储为
uint64_t UPPER, LOWER;
,并且我不确定如何重载
operator<<
,以便当我传入
std::cout
时,该值将正确打印为十进制。目前,我正在做
std::ostream & operator<<(std::ostream & stream, uint128_t const & rhs){
    if (rhs.upper())                  // if the upper value has a non-zero digit
        stream << rhs.upper();

    // i need some way to pad this so that the number of 0s between 
    // upper and lower is correct

    stream << rhs.lower();
    return stream;
我该怎么办? 编辑: 例: 如果uint128_t变量具有
UPPER = 1
LOWER = 1
,我希望流包含十进制值
(1 << 64) + 1
    
已邀请:
假设您已正确实现除法运算符和模运算符,则可以执行以下操作:
std::ostream & operator<<(std::ostream & stream, uint128_t const & rhs){

    if(rhs.upper() == 0)
        return stream << rhs.lower();

    char buffer[50];
    char *cp = buffer + 49;
    *cp = 0;
    while(rhs > 0)
    {
        --cp;
        *cp = (rhs % 10) + \'0\';
        rhs /= 10;
    }

    return stream << cp;
}
    

要回复问题请先登录注册