C++ 字符串流值不正确?结果是 0?

C++ Stringstream values aren't correct? The result is 0?

本文关键字:结果是 不正确 字符串 C++      更新时间:2023-10-16

这是我无法正常工作的重载部分。

HexColour HexColour::operator+(const HexColour& other) const
{
HexColour temp;
    stringstream r,g,b;
    stringstream ro, go, bo;
    int x, y, z;
    int xo, yo, zo;
    r << hex << colour[2] << colour[3];
    g << hex << colour[4] << colour[5];
    b << hex << colour[6] << colour[7];
    r >> x;
    g >> y;
    b >> z; 
    ro << hex << other.colour[2] << other.colour[3];
    go << hex << other.colour[4] << other.colour[5];
    bo << hex << other.colour[6] << other.colour[7];
    ro >> xo;
    go >> yo;
    bo >> zo;

    if((x + xo) > 255)
        x = 255;
    else
        x = x + xo;
    if((y + yo) > 255)
        y = 255;
    else
        y += yo;    
    if((z + zo) > 255)
        z = 255;
    else
        z += xo;
    cout << x << " anwer" << endl;
    cout << y << " anwer" << endl;
    cout << z << " anwer" << endl;

到目前为止,一切都很完美,x y z 答案是正确的;这是问题部分:

    r << hex << x; // PROBLEM
    g << hex << y; // PROBLEM
    b << hex << z; // PROBLEM

r的值是0,为什么?

    cout << hex <<r << endl;
    temp.colour = "0x" + r.str() + g.str() + b.str();
    return temp;
    }

我在上面打印的部分是正确的整数值,但 r 不是新的十六进制值??

使用以下命令清除所有字符串流:

r.clear();

然后重复这些步骤->

r<<hex<<x;

希望这有帮助。

IMO,你真的有两个问题。首先,我认为如果您尽早从十六进制字符串转换为某种整数类型,数据将更容易操作,因此所有内部代码只需要操作整数,而不是十六进制字符串。

其次,你在这个功能中做了太多不同的事情,然后通过重复这些事情中的每件事三次来增加对伤害的侮辱。最好将其分解为一些功能,每个功能执行一些连贯的操作。

我将创建一个支持这些任意操作的 color(或colour,如果您愿意)类,以及与 HexColor 之间的转换,而不是支持任意操作的 HexColor 类,您仅用于输入和输出。

color类内部,操作将被分解为更易于管理的部分,而不是每个部分都是一个巨大的整体代码块,所有内容重复三次。

#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
class color;
struct HexColor {
    std::string colour;
    HexColor(std::string const &colour) : colour(colour) {}
    friend std::ostream &operator<<(std::ostream &os, HexColor const &h) {
        return os << h.colour;
    }
    HexColor(color const &);
};
class color {
    unsigned val;
    unsigned getr(unsigned in) { return (in >> 0) & 0xff; }
    unsigned getg(unsigned in) { return (in >> 8) & 0xff; }
    unsigned getb(unsigned in) { return (in >> 16) & 0xff; }
    unsigned rgb(unsigned r, unsigned g, unsigned b) {
        return r | (g << 8) | (b << 16);
    }
    unsigned min(unsigned a, unsigned b) {
        return b < a ? b : a;
    }
    unsigned saturating_add(unsigned a, unsigned b) {
        return min(a + b, 255);
    }
public:
    friend class HexColor;
    color(unsigned r, unsigned g, unsigned b) : val(rgb(r, g, b)) {}
    color(HexColor const &h) {
        std::stringstream temp(h.colour.substr(2, 6));
        temp >> std::hex >> val;
    }
    color operator+(color const &other) {
        unsigned r = saturating_add(getr(val), getr(other.val));
        unsigned g = saturating_add(getg(val), getg(other.val));
        unsigned b = saturating_add(getb(val), getb(other.val));
        return color(r, g, b);
    }
};
HexColor::HexColor(color const &c) {
    std::stringstream s;
    s << std::setw(6) << std::setfill('0') << std::setprecision(6) << std::hex << c.val;
    colour = "0x" + s.str();
}
#ifdef TEST
int main(){
    color a(HexColor("0x010101"));
    color b(HexColor("0x020202"));
    std::cout << HexColor(a + b) << "n";
    color c(HexColor("0x123456"));
    color d(HexColor("0x789abc"));
    std::cout << HexColor(c+d) << "n";
}
#endif

这就留下了一个基本问题:拥有HexColor类是否有意义,或者将这点功能滚动到color类本身(或者,也许,同一命名空间中的一些自由函数)中是否更有意义。目前,我保持原样,color主要负责操作,HexColor处理I/O,但在实际代码中,我会长时间考虑只有一个处理十六进制表示的operator>>operator<<,其他一切都只是操作颜色。