简体   繁体   English

如何从std :: istream中读取(使用operator >>)?

[英]How can I read from an std::istream (using operator>>)?

How can I read from an std::istream using operator>> ? 如何使用operator>>std::istream读取?

I tried the following: 我尝试了以下方法:

void foo(const std::istream& in) {
  std::string tmp;
  while(in >> tmp) {
     std::cout << tmp;
  }
}

But it gives an error: 但它给出了一个错误:

error: no match for 'operator>>' in 'in >> tmp'

运算符>>修改流,所以不要通过const,只是一个引用。

Use a non-const reference: 使用非const引用:

void foo(std::istream& in) {
  std::string tmp;
  while(in >> tmp) {
     std::cout << tmp;
  }
}

You're doing that the right way. 你正是这样做的。 Are you sure you included all the headers you need? 您确定要包含所需的所有标题吗? ( <string> and <iostream> )? <string><iostream> )?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM