-2

可能重复:
如何在 C++ 中逐行从文件中读取整数

请任何人都可以建议我如何在 C++ 中输入整数直到换行。

假设输入流是

10 10 10 10 10 10 10 10 10 10,后跟 C++ 中的换行符。

4

2 回答 2

5
std::string the_string;
std::getline(the_stream, the_string);
std::istringstream iss(the_string);
for (int n; iss >> n; )
{
    // do something with n
}
于 2012-08-16T18:01:38.310 回答
5

可能重复:如何在 C++ 中逐行从文件中读取整数组

如果您想以每行为基础进行交易:

int main()
{
   std::string line;
   std::vector< std::vector<int> > all_integers;
   while ( getline( std::cin, line ) ) {
      std::istringstream is( line );
      all_integers.push_back( 
            std::vector<int>( std::istream_iterator<int>(is),
                              std::istream_iterator<int>() ) );
   }
}
于 2012-08-16T18:02:54.263 回答