- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 C++ Practical Programming by Example by Andrew Koenig (Author)并在 4.1.3 Reading homework grades
章节中,有这个片段:
istream& read_hw(istream& in, vector<double>& hw){
//if(in){ /* HOW can be istream be used as bool in condition? -> commented out, this is not needed */
//hw.clear(); /*WHY to clear vector, when every instance is used only once? -> commented out, not needed*/
double x;
while(cin >> x)
hw.push_back(x);
//in.clear(); /* why to clear istream from error state, when if wrong value is passed, it is ignored anyway -> commented out*/
//} /* this is connected with the if-block */
return in; /*how is now in(istream) modified? where is binding between `std::cout` and `in`?? */
}
我有几个问题:
in
是如何修改的)true
如果 std::basic_ios::good
的值但是,根据文档,参数中的 istream 是新实例,因此在调用它之前不应该有任何错误状态,因此 if 子句是多余的。std::cout
怎么样与 in
相关联?书中的引文:We don't know the details of how cin works, but presumably the librarydefines it as a data structure that stores everything the library needs to know about the state of ourinput file. Reading input from the standard input file changes the state of the file, so it should logically change the value of cin as well.
std::cin
缓冲区确实是从键盘单词的输入修改的(由空格分隔),但是
std::cout
之间的连接在哪里?和
in
, 只有
while(cin >> x)
它修改了
cin
, 但是
cin
知道将这些数据复制到另一个缓冲区中
in
?或者
in
的目的是什么?当该函数的唯一目的是填充 vector 时?我想这与返回现在“修改”的方式有某种联系
in
最后,但我无法真正看到这两个缓冲区之间的联系。
//
注释是指我认为不需要的原始代码的一部分,如果编译,它仍然可以工作,所以它确实是不需要的。
/**/
解释了为什么我认为不需要
c
.
最佳答案
- return value : Why does the function have
istream
type as return value? Why is not the function void?
istream&
允许链接,如下所示:
std::string name;
read_hw(cin, gradevec) >> name;
这将放置从
cin
收到的第一个非 double 值进入
name
多变的。诚然,这是一个人为的例子,不是最易读的,但它确实展示了它是如何工作的。
if(in)
: as in comments, I know it could have true value ifstd::basic_ios::good
according to documentation, however, the istream in parameter is fresh instance
&
中的 & 符号
istream& in
表示)。
hw.clear()
: again, there is no need to clear the vector. It is a fresh instance, it was nowhere populated before in the program, so why to do this?
vector<double>&
) 的引用。 C没有这样的引用,它只有指针,所以
this answer可以帮助您更好地了解正在发生的事情。
in.clear()
: this one is really bother me. Is this even essential to do in non-parallel process? I can think of one case when to clear buffer and that is when fatal error occur.
in.clear()
有点令人困惑的是不会清除整个缓冲区,它只是
clears the various state flags像
failbit
或
badbit
你不想留下来并导致 future 的 I/O 操作失败。
- The last one - how is
std::cout
connected within
?
while(in >> x)
.否则,使用
in
毫无意义。一点也不。
关于c++ - istream std::cin 如何修改自定义 istream 缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62721248/
我正在尝试弄清楚如何让一个成员变量代表传入的 istream 或类自己创建的成员变量。 我认为,如果我动态分配类创建的 istream,那么使用指向 istream 的指针可能会起作用;然而,问题在于
我正在尝试理解这段代码 istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.b
我编写了一个实现 IStream(COM 接口(interface)而不是 C++ 标准库类)的过滤器。这一切都很好,但我的问题是我不确定何时(如果有的话)我可以确定不会发送进一步的 IStream
我正在测试 C++PL 书中的一段代码并找到了下一段代码(我不想感觉我是在将它从书中复制粘贴到我的 IDE,所以我至少改变了变量名): istream& operator>> (istream& is
我正在阅读 C++ Practical Programming by Example by Andrew Koenig (Author)并在 4.1.3 Reading homework grades
我正在尝试使用 istream& getline 而不是使用 istream& operator 从文件中读取行。 结果cpp #include "Result.h" Result::Result()
我试图通过套接字连接发送图像,但我遇到了以下代码的问题: //stream to char array STATSTG myStreamStats; ULONG bytesSaved; myStrea
这里有两段代码,我起初认为它们应该是等价的: { std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::bin
我在网上查了,但没找到合适的。 怎么可能有这样的主线(原始主线): int main() { Image left; std::ifstream ifs("l
假设我有以下代码: std::ifstream file(name, flags); file.seekg(0, std::ios::beg); // Check for error file.rea
在 Windows 上的 C++ 中,是否有任何简单的方法可以为现有 std::stream 对象创建 (COM) IStream 接口(interface)? 一个示例是使用 IWICStream:
考虑以下简单程序,它将输入拆分为空白并每行打印出一个非空白标记: #include int main(int argc, char* argv[]) { while (std::ci
istream& getline (istream& is, string& str); 此处 str 在换行符后终止。但是,如果我想处理 str cotents 2-3 行的情况,那么还有什么选择呢
这个问题已经有答案了: Java multiple file transfer over socket (3 个回答) 已关闭 5 年前。 我正在编写一个程序在两台计算机之间传输文件,但我没有关闭套接
void transferData(ifstream& input_file, ofstream& output_file) { char ch; while (input_file
我正在读取包含以下行的文本文件: deltaT 0.005; 在字符串中找到正确的行后,我想使用如下代码读出该值: double deltaT;
我正在查看 istream 类,但没有看到可以完全清除缓冲区并将输入设置为为下一个“干净”输入做好准备的方法。 我为我的类定义了提取运算符,在我的主程序中我要求用户输入,如下所示: while (tr
我正在编写一个解析器,之前我在尝试解析标识符(任何对 C++ 变量名有效的东西)和未闭合的字符串文字(任何以 " 开头的东西)时遇到了麻烦,但是在我的输入末尾缺少结束符 ")。我认为这是因为词法分析器
我得到了一个具有以下签名的函数。我无法改变它,我必须接受它。 void parse(std::istream & in); 我应该测试这个函数,所以基本上用预定义的内容调用它并检查值是否被正确解析。因
我的运算符(operator)有问题>> istream& operator>> (istream& is, Matrix& M) { char first; is>>first;
我是一名优秀的程序员,十分优秀!