直接读取C 的结构
Reading directly to struct in C++
假设我有一些结构,例如 Rectangle
:
struct Rectangle
{
int x0, x1, y0, y1;
};
是否可以以一种能够致电的方式创建矩形结构:
Rectangle rec;
cin >> rec;
?我认为应该以某种方式做到这一点,但我的经验还不够。
免责声明
我不是在寻找:
cin >> rec.x0 >> rec.x1 >> rec.y0 >> rec.y1;
您可以使用:
Rectangle rec;
cin >> rec;
如果您定义了适当的operator>>
函数。
std::istream& operator>>(std::istream& in, Rectangle& rec)
{
return (in >> rec.x0 >> rec.x1 >> rec.y0 >> rec.y1);
}
如果不允许您定义这样的功能,则不能使用要使用的语法。
是的,最好的解决方案是 Rectangle
的 operator>>
:
struct Rectangle
{
int x0, x1, y0, y1;
};
istream& operator>>(istream& s, Rectangle& r)
{
s >> r.x0 >> r.x1 >> r.y0 >> r.y1;
return s;
}
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 将内部带有矢量的结构保存/读取到二进制文件中
- 从 XML 中读取未指定结构的每个数据成员
- 如何读取没有数组或结构的 3 列文件
- 将结构向量保存到文件中,并从C++文件中读取结构向量
- 嵌套结构,从由空行分隔的文件中读取数据
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 从只读内存中读取结构
- 为什么不读取带有指针的结构?
- 如何读取文件并将该文件分类为不同类型的数据,这些数据都存储在结构中的特定数组中
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 将文本文件读取到数组结构中
- 如何读取/查询文件系统和文件结构
- 从包含 IP 标头片段的二进制文件中读取结构的最佳方法是什么
- 使用结构读取和写入二进制文件
- 使用文件和结构读取数据并输出数据
- 使用结构 c++ 读取压缩文件 Gzread
- 结构读取C++
- 在 c++ 中使用消息结构读取/填充数据缓冲区的正确方法是什么?
- 将包含字符串属性的结构读取/写入二进制文件