简体   繁体   English

如何终止从cin读取?

[英]How to terminate reading from cin?

I've tried a bunch of methods listed on here but none of them work.我尝试了这里列出的一堆方法,但没有一个有效。 It's always waiting for more input.它总是在等待更多的输入。

I've tried while(std::getline(std::cin, line)) and the method below, nothing seems to work:我试过while(std::getline(std::cin, line))和下面的方法,似乎没有任何效果:

#include <iostream>
#include <sstream>

using namespace std;

int main(){
  long length = 1UL<<32;
  int array[length];
  // memset(array, 0, (length-1) * sizeof(int));

  for(int i = 0; i < length; i++)
    array[i] = 0;
  string line;
  int num;
  while(!cin.eof()){
    getline(cin,line);
    stringstream ss(line);
    ss >>num;
    array[num]++;
  }
  for(int i = 0; i < length; i++)
      if(array[i]){
          cout << i << ": ";
          cout << array[i] << endl;
      }
}

First off, do not use std::cin.eof() to control your loop!首先,使用std::cin.eof()来控制你的循环! It doesn't work.它不起作用。 Also, you always need to check for successful input after the input.此外,您始终需要在输入检查是否成功输入。

That said, to terminate the input you'll need to enter the appropriate end of file character, probably at the start of the line (how it works entirely somewhat depends on the system, some settings, etc.).也就是说,要终止输入,您需要输入适当的文件结尾字符,可能在行的开头(它的工作方式完全取决于系统、某些设置等)。 On Windows you'd use Ctrl-Z, on UNIXes you'd use Ctrl-D.在 Windows 上,您将使用 Ctrl-Z,在 UNIX 上,您将使用 Ctrl-D。

First off, this part of your program attempts to allocate 4 GB of memory on the stack, which doesn't work on my machine (good luck finding any machine with 4 GB of continuous memory space):首先,程序的这一部分尝试在堆栈上分配 4 GB 内存,这在我的机器上不起作用(祝你好运找到任何具有 4 GB 连续内存空间的机器):

long length = 1UL<<32;
int array[length];

If I change that to a much more reasonable:如果我将其更改为更合理的:

long length = 32;

Then it works fine for me:然后它对我来说很好用:

$ g++ -g test.cpp -o test && ./test
2
5
# pressed control+d
2: 1
5: 2
$ 

So I'm guessing something else is wrong.所以我猜还有其他问题。


Note: Unless you actually plan to use all of those indexes, you may want to considering using anunordered_map , so you only use the space you actually need.注意:除非您真的计划使用所有这些索引,否则您可能需要考虑使用unordered_map ,因此您只使用您实际需要的空间。

The condition you are looking for can be most easily tested by evaluating "std::cin" as a bool, ie while (cin) .通过将 "std::cin" 评估为布尔值,即while (cin) ,可以最容易地测试您正在寻找的条件。 But it won't do this until you have tried to read beyond EOF, so expect an empty getline:但它不会这样做,直到您尝试阅读 EOF 之外的内容,因此请期待一个空的 getline:

#include <iostream>
#include <string>

int main() {
    std::string input;
    while (std::cin) {
        std::cout << "Type something in:\n";
        std::getline(std::cin, input);
        if(input.empty())
            continue;
        std::cout << "You typed [" << input << "]\n";
    }
    std::cout << "Our work here is done.\n";

    return 0;
}

Live demo: http://ideone.com/QR9fpM现场演示: http : //ideone.com/QR9fpM

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

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