gpt4 book ai didi

c++ - std::istream& 运算符>>(std::istream& , ClassName& )

转载 作者:太空狗 更新时间:2023-10-29 21:10:36 27 4
gpt4 key购买 nike

我在网上查了,但没找到合适的。

怎么可能有这样的主线(原始主线):

 int main()
{
Image left;
std::ifstream ifs("left.txt");
ifs >> left;
ifs.close();
waitForKey("cout << left");
std::cout << left;
}

并尝试像这样(我的解码):

//m_pixel is a member that holds a single char
//m_H = height
//m_W = width
std::istream& operator>>(std::istream& is, ClassName& image)
{
image.m_pixel = new Pixel*[image.m_H];

for (int i = 0; i < image.m_H; i++)
image.m_pixel[i] = new Pixel[image.m_W];
}

例如,如果我不知道高度和宽度,我该如何将"is"插入图像?我怎样才能知道他们?以及如何在图像中插入"is"的字符?简而言之,如何解码这段代码?

最佳答案

显然,当您转储图像时,您需要从转储尺寸开始:

std::ostream& operator<<(std::ostream& os, ClassName& image)
{
os << image.m_H << " " << image.m_W;
// Dump the rest as before
return os;
}

然后再次阅读它们,然后阅读其余数据:

std::istream& operator>>(std::istream& is, ClassName& image)
{
is >> image.m_H >> image.m_W;
image.m_pixel = new Pixel*[image.m_H];

for (int i = 0; i < image.m_H; ++i)
{
image.m_pixel[i] = new Pixel[image.m_W];
for (int j = 0; j < image.m_W; ++j)
{
is >> image.m_pixel[i][j];
}
}
return is;
}

但正如@NathanOliver 所说,使用 std::vector<Pixel> .

关于c++ - std::istream& 运算符>>(std::istream& , ClassName& ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53483738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com