【问题标题】:Multi-line stdin - C++多行标准输入 - C++
【发布时间】:2012-10-13 18:17:39
【问题描述】:

所以,如果我有一个采用标准输入的程序,例如

1
5
2
4

我如何准确地遍历每一行并说出打印该值,这就是我的想法:

#include <iostream>
using namespace std;

int main()
{

  while ( // input has ended// ) {
  cout << //current line//

  //increment to next line//

 }     

  return 0;
}

有没有这样的方法?

【问题讨论】:

    标签: c++ stdin


    【解决方案1】:

    我喜欢的模式是:

    while (!cin.eof())
    {
        string line;
        getline(cin, line);
    
        if (cin.fail())
        {
            //error
            break;
        }
    
        cout << line << endl;
    }
    

    与其他答案一样,您可以键入 CTRL+ZEOF 发送到 STDIN。如果STDIN 是一个管道,那么EOF 将在流没有更多数据时发送。

    保存到向量中:

    vector<int> numbers;
    
    while (!cin.eof())
    {
        string line;
        getline(cin, line);
    
        if (cin.fail())
        {
            //error
            break;
        }
    
        cout << line << endl;
    
        istringstream iss(line);
        int num;
        iss >> num;
        numbers.push_back(num);
    }
    

    如果你想要一个 C 风格的数组(虽然我会推荐 std::vector:

    size_t START_SIZE = 100;
    
    size_t current_size = START_SIZE;
    size_t current_index = 0;
    
    int* numbers = new int[current_size];
    
    while (!cin.eof())
    {
        string line;
        getline(cin, line);
    
        if (cin.fail())
        {
            //error
            break;
        }
    
        cout << line << endl;
    
        if (current_index == current_size)
        {
            current_size += START_SIZE;
            int* tmp_arr = new int[current_size];
    
            for (size_t count = 0; count < current_index; count++)
            {
                tmp_arr[count] = numbers[count];
            }
    
            delete [] numbers;
            numbers = tmp_arr;
        }
    
        istringstream iss(line);
        int num;
        iss >> num;
    
        numbers[current_index] = num;
        current_index++;
    }
    
    delete [] numbers;
    

    【讨论】:

    • 如果我想将所有输入保存到一个数组中,该怎么做?
    • @Beginnernato,它们都是一种类型吗?如果是这样,std::vector&lt;Type&gt; inputs(std::istream_iterator&lt;Type&gt;(std::cin), std::istream_iterator&lt;Type&gt;());
    • 哦,整数?好的……一秒钟。
    【解决方案2】:

    最简单的方法是这样做(建议类似,但通常不应该那样使用 std::istream 的 .fail() 和 .eof() 函数):

    int main (int argc, char* argv[])
    {
       std::string line;
       while (std::getline (std::cin, line))
       {
         std::cout << line << std::endl; // std::getline skips the newline
       }
    
       std::cout << "No more lines" << std::endl;
       return 0;
    };
    

    【讨论】:

      【解决方案3】:

      如果你从控制台读取,你可以添加一个像 0 这样的结束条件。如果用户输入 0,它将结束输入。

      #include <iostream>
      using namespace std;
      
      int main()
      {
        int n;
        cin>>n;
        while(n!=0) {
      
        cout << //current line//
      
        //increment to next line//
        cin>>n;
       }   
      
        return 0;
      }
      

      【讨论】:

        【解决方案4】:

        std::cin &gt;&gt; std::cout.rdbuf(); 怎么样?当您通过 CTRL-Z 或类似方式输入 EOF 时,它将结束。这里是a sample

        #include <iostream>
        
        int main() {
            std::cin >> std::cout.rdbuf();
        }
        

        输入:

        1
        5
        4
        2
        (EOF)

        输出:

        1
        5
        4
        2

        将它们保存到数组中并在以后打印它们也同样有效:

        std::vector<int> inputs(std::istream_iterator<int>(std::cin), std::istream_iterator<int>());
        std::copy(std::begin(inputs), std::end(inputs), std::ostream_iterator<int>(std::cout, "\n"));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-25
          • 2011-04-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多