sách gpt4 ai đã đi

c++ - istream std::cin 如何修改自定义 istream 缓冲区?

In lại 作者:行者123 更新时间:2023-12-02 10:13:27 30 4
mua khóa gpt4 Nike

Tôi đang đọc C++ Practical Programming by Example by Andrew Koenig (Author)và trong 4.1.3 Reading homework grades 章节中,有这个片段:

istream& read_hw(istream& in, vector& 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`?? */
}
我有几个问题:
  • Giá trị trả về : 为什么函数有 istream 类型作为返回值?
    为什么函数不是void?当该函数的唯一目的是填充 vector 时,它不必返回任何值。 (本题与上一题有关,关于istream TRONG是如何修改的)
  • 如果(在) : 正如评论中所说,我知道它可能有 ĐÚNG VẬYnếu như std::basic_ios::good 的值但是,根据文档,参数中的 istream 是新实例,因此在调用它之前不应该有任何错误状态,因此 if 子句是多余的。
  • hw.clear() : 再次,没有必要清除 vector 。这是一个新鲜的实例,它之前在程序中没有出现过,那么为什么要这样做呢?
  • in.clear() : 这个真的很困扰我。这在非并行过程中甚至是必不可少的吗?我可以想到清除缓冲区的一种情况,即发生 fatal error 时,否则,当缓冲区本身处理错误时,我认为没有必要清除缓冲区->传递了错误的值(即其他值然后加倍这种情况)=> 缓冲区只是忽略它,或者调用了 EOF => 缓冲区将结束读取。缓冲区负责这一切。
  • 最后一个-std::cout怎么样与 TRONG 相关联?书中的引文:

  • 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 之间的连接在哪里?和 TRONG , 只有 while(cin >> x)它修改了 cin , 但是 cin知道将这些数据复制到另一个缓冲区中 TRONG ?或者 TRONG 的目的是什么?当该函数的唯一目的是填充 vector 时?我想这与返回现在“修改”的方式有某种联系 TRONG最后,但我无法真正看到这两个缓冲区之间的联系。
    我知道这与上面的书有关,所以如果你没有读过它,你可能没有上下文,但有些问题不需要上下文。 //注释是指我认为不需要的原始代码的一部分,如果编译,它仍然可以工作,所以它确实是不需要的。 /**/解释了为什么我认为不需要
    无论如何对不起这些初学者问题,我的背景只有 c .

    1 Câu trả lời

    1. 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 值进入 tên多变的。诚然,这是一个人为的例子,不是最易读的,但它确实展示了它是如何工作的。
    1. if(in) : as in comments, I know it could have true value if std::basic_ios::good according to documentation, however, the istream in parameter is fresh instance

    不,它不是一个新的实例,它是一个已经在函数之外定义的引用(由 & 中的 & 符号 istream& in 表示)。
    1. 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 ( vector&) 的引用。 C没有这样的引用,它只有指针,所以 this answer可以帮助您更好地了解正在发生的事情。
    1. 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 flagsfailbithoặc badbit你不想留下来并导致 future 的 I/O 操作失败。
    1. The last one - how is std::cout connected with TRONG?

    对于这个我只能猜测作者犯了一个错误,打算写 while(in >> x) .否则,使用 TRONG 毫无意义。一点也不。

    关于c++ - istream std::cin 如何修改自定义 istream 缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62721248/

    30 4 0
    Bài viết được đề xuất: Android在编译二进制文件时重建静态库
    Bài viết được đề xuất: c++ - 使用 Z3 证明分辨率定理
    Bài viết được đề xuất: c++ 传递对象的私有(private)变量
    Bài viết được đề xuất: c++ - 带有 boost::multiprecision 的单元测试
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    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