C++ cin 读取 STDIN

发布于 2024-12-23 04:13:17 字数 175 浏览 5 评论 0原文

如何使用C++获取所有STDIN并解析它?

例如,我的输入是

2
1 4
3
5 6 7

我想使用 C++ 使用 cin 读取 STDIN 并将每一行存储在数组中。因此,它将是一个向量/整数数组的数组。

谢谢!

How to use C++ to get all the STDIN and parse it?

For example, my input is

2
1 4
3
5 6 7

I want to use C++ to read the STDIN using cin and store the each line in an array. So, it will be an vector/array of array of integers.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-12-30 04:13:17

由于这没有标记为作业,因此这里有一个使用 std::vectorstd::stringstream 读取 stdin 的小示例。我在最后添加了一个额外的部分,用于迭代向量并打印出值。给控制台一个 EOFctrl + d for *nix, ctrl + z 对于 Windows)以阻止其读取输入。

#include <iostream>
#include <vector>
#include <sstream>

int main(void)
{
   std::vector< std::vector<int> > vecLines;

   // read in every line of stdin   
   std::string line;
   while ( getline(std::cin, line) )
   {
      int num;
      std::vector<int> ints;
      std::istringstream ss(line); // create a stringstream from the string

      // extract all the numbers from that line
      while (ss >> num)
         ints.push_back(num);

      // add the vector of ints to the vector of vectors         
      vecLines.push_back(ints);      
   }

   std::cout << "\nValues:" << std::endl;
   // print the vectors - iterate through the vector of vectors   
   for ( std::vector< std::vector<int> >::iterator it_vecs = vecLines.begin();
         it_vecs != vecLines.end(); ++it_vecs )
   {
      // iterate through the vector of ints and print the ints
      for ( std::vector<int>::iterator it_ints = (*it_vecs).begin();
         it_ints < (*it_vecs).end(); ++it_ints )
      {
         std::cout << *it_ints << " ";
      }

      std::cout << std::endl; // new line after each vector has been printed
   }

   return 0;
}

输入/输出:

2
1 4
3
5 6 7

Values:
2 
1 4 
3 
5 6 7 

编辑:向代码添加了更多注释。另请注意,可以将 int 的空向量添加到vecLines(来自空输入行),这是故意的,以便输出与输入相同。

Since this isn't tagged as homework, here's a small example of reading from stdin using std::vectors and std::stringstreams. I added an extra part at the end also for iterating through the vectors and printing out the values. Give the console an EOF (ctrl + d for *nix, ctrl + z for Windows) to stop it from reading in input.

#include <iostream>
#include <vector>
#include <sstream>

int main(void)
{
   std::vector< std::vector<int> > vecLines;

   // read in every line of stdin   
   std::string line;
   while ( getline(std::cin, line) )
   {
      int num;
      std::vector<int> ints;
      std::istringstream ss(line); // create a stringstream from the string

      // extract all the numbers from that line
      while (ss >> num)
         ints.push_back(num);

      // add the vector of ints to the vector of vectors         
      vecLines.push_back(ints);      
   }

   std::cout << "\nValues:" << std::endl;
   // print the vectors - iterate through the vector of vectors   
   for ( std::vector< std::vector<int> >::iterator it_vecs = vecLines.begin();
         it_vecs != vecLines.end(); ++it_vecs )
   {
      // iterate through the vector of ints and print the ints
      for ( std::vector<int>::iterator it_ints = (*it_vecs).begin();
         it_ints < (*it_vecs).end(); ++it_ints )
      {
         std::cout << *it_ints << " ";
      }

      std::cout << std::endl; // new line after each vector has been printed
   }

   return 0;
}

Input/Output:

2
1 4
3
5 6 7

Values:
2 
1 4 
3 
5 6 7 

EDIT: Added a couple more comments to the code. Also note that an empty vectors of ints can be added to vecLines (from an empty line of input), that's intentional so that the output is the same as the input.

呆萌少年 2024-12-30 04:13:17
int main () 
{
    char line[100];
    while(!cin.eof()){
        cin.getline(line, 100);
        printf("%s\n", line);
    }

    return 0;
}

抱歉,我只是不确定是否还有比这更好的方法。

int main () 
{
    char line[100];
    while(!cin.eof()){
        cin.getline(line, 100);
        printf("%s\n", line);
    }

    return 0;
}

Sorry, I just wasn't sure if there's any way better than this.

你怎么敢 2024-12-30 04:13:17

这个应该符合您的要求,使用 istringstream 将行分隔成数组。

#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;

int main()
{
        string s("A B C D E F G");
        vector<string> vec;
        istringstream iss(s);

        do
        {
                string sub;
                iss >> sub;
                if ( ! sub.empty() ) 
                        vec.push_back (sub);
        } while (iss);

        vector<string>::iterator it = vec.begin();
        while ( it != vec.end() )
        {
                cout << *it << endl;
                it ++;
        }

        return 0;
}

This one should fit your requirement , use istringstream to separate the line into an array.

#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;

int main()
{
        string s("A B C D E F G");
        vector<string> vec;
        istringstream iss(s);

        do
        {
                string sub;
                iss >> sub;
                if ( ! sub.empty() ) 
                        vec.push_back (sub);
        } while (iss);

        vector<string>::iterator it = vec.begin();
        while ( it != vec.end() )
        {
                cout << *it << endl;
                it ++;
        }

        return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文