C++ 字符串流值不正确?结果是 0?
C++ Stringstream values aren't correct? The result is 0?
这是我无法正常工作的重载部分。
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<<
,其他一切都只是操作颜色。
相关文章:
- 密码登录程序将永远循环并显示不正确的结果
- 特征 LLT 模块给出不正确的结果?
- 计算幂级数的数学结果不正确
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 实例化具有不完整类型的类模板格式不正确(如果该类型是在之后定义的)
- 为什么要 boost::p roperty_tree::write_json() 将整数值转换为字符串?这是不正确的
- 我的程序中的值计算不正确,即使支持数学是正确的
- C++:读取字符是不正确的
- wcslen() 在使用编译指示包时返回不正确的结果
- rpmbuild check-rpath 报告错误,指出路径不是绝对的,不正确的
- 使用动态分配的数组进行矩阵乘法;结果不正确
- CMake 查找特征不正确的结果
- "this" Lambda 捕获的是不正确的。海湾合作委员会编译器错误?
- 在运行libtorch的模型时获得的结果是不正确的,该模型经过训练并从Pytorch出口
- 为什么第一个原型是正确的,而第二个原型是不正确的
- 为什么 vector.reserve(2) 后跟 v[0] = 2 被认为是不正确的
- 将 const 传递给接受 const 指针的函数是不正确的
- C++ 字符串流值不正确?结果是 0?
- 后缀否定是不正确的,而且是交流性的
- 像这样在作用域外使用delete是不正确的吗