sách gpt4 ai đã đi

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

In lại Tác giả: Space Dog 更新时间:2023-10-29 21:10:36 28 4
mua khóa gpt4 Nike

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

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

 int chính()
{
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"的字符?简而言之,如何解码这段代码?

1 Câu trả lời

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

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 .

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

28 4 0
Bài viết được đề xuất: c++ - 在 MacOS 上构建 conan/cmake 项目时的相对 RPATH
Bài viết được đề xuất: c# - Sitecore 工作流程批准/拒绝电子邮件
Bài viết được đề xuất: python - 将序列变成列表的函数
Bài viết được đề xuất: c++ - 在CGAL中获取两个圆的交点
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com