<tfoot id='PJhxV'></tfoot>

    1. <legend id='PJhxV'><style id='PJhxV'><dir id='PJhxV'><q id='PJhxV'></q></dir></style></legend>
    2. <i id='PJhxV'><tr id='PJhxV'><dt id='PJhxV'><q id='PJhxV'><span id='PJhxV'><b id='PJhxV'><form id='PJhxV'><ins id='PJhxV'></ins><ul id='PJhxV'></ul><sub id='PJhxV'></sub></form><legend id='PJhxV'></legend><bdo id='PJhxV'><pre id='PJhxV'><center id='PJhxV'></center></pre></bdo></b><th id='PJhxV'></th></span></q></dt></tr></i><div id='PJhxV'><tfoot id='PJhxV'></tfoot><dl id='PJhxV'><fieldset id='PJhxV'></fieldset></dl></div>

    3. <small id='PJhxV'></small><noframes id='PJhxV'>

        <bdo id='PJhxV'></bdo><ul id='PJhxV'></ul>
    4. 如何在 C++ 中逐行读取文件中的整数组

      时间:2023-10-07
    5. <small id='GHQ2r'></small><noframes id='GHQ2r'>

      <i id='GHQ2r'><tr id='GHQ2r'><dt id='GHQ2r'><q id='GHQ2r'><span id='GHQ2r'><b id='GHQ2r'><form id='GHQ2r'><ins id='GHQ2r'></ins><ul id='GHQ2r'></ul><sub id='GHQ2r'></sub></form><legend id='GHQ2r'></legend><bdo id='GHQ2r'><pre id='GHQ2r'><center id='GHQ2r'></center></pre></bdo></b><th id='GHQ2r'></th></span></q></dt></tr></i><div id='GHQ2r'><tfoot id='GHQ2r'></tfoot><dl id='GHQ2r'><fieldset id='GHQ2r'></fieldset></dl></div>
          <tbody id='GHQ2r'></tbody>

        1. <tfoot id='GHQ2r'></tfoot>
            • <bdo id='GHQ2r'></bdo><ul id='GHQ2r'></ul>
                <legend id='GHQ2r'><style id='GHQ2r'><dir id='GHQ2r'><q id='GHQ2r'></q></dir></style></legend>

                本文介绍了如何在 C++ 中逐行读取文件中的整数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个文本文件,每行一个或多个整数,用空格分隔.我怎样才能用 C++ 以优雅的方式阅读这个?如果我不关心行,我可以使用 cin >>,但重要的是在哪一行整数上.

                I have a text file with on every line one or more integers, seperated by a space. How can I in an elegant way read this with C++? If I would not care about the lines I could use cin >>, but it matters on which line integers are.

                示例输入:

                1213 153 15 155
                84 866 89 48
                12
                12 12 58
                12
                

                推荐答案

                这取决于您是要逐行还是完整地进行.将整个文件转化为一个整数向量:

                It depends on whether you want to do it in a line by line basis or as a full set. For the whole file into a vector of integers:

                int main() {
                   std::vector<int> v( std::istream_iterator<int>(std::cin), 
                                       std::istream_iterator<int>() );
                }
                

                如果您想在一行一行的基础上进行交易:

                If you want to deal in a line per line basis:

                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>() ) );
                   }
                }
                

                这篇关于如何在 C++ 中逐行读取文件中的整数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:站点 coderbyte 上的“gets(stdin)"是怎么回事? 下一篇:一根线上有多个输入

                相关文章

                最新文章

                  • <bdo id='GADoz'></bdo><ul id='GADoz'></ul>

                  <tfoot id='GADoz'></tfoot>
                  <i id='GADoz'><tr id='GADoz'><dt id='GADoz'><q id='GADoz'><span id='GADoz'><b id='GADoz'><form id='GADoz'><ins id='GADoz'></ins><ul id='GADoz'></ul><sub id='GADoz'></sub></form><legend id='GADoz'></legend><bdo id='GADoz'><pre id='GADoz'><center id='GADoz'></center></pre></bdo></b><th id='GADoz'></th></span></q></dt></tr></i><div id='GADoz'><tfoot id='GADoz'></tfoot><dl id='GADoz'><fieldset id='GADoz'></fieldset></dl></div>

                  <small id='GADoz'></small><noframes id='GADoz'>

                    <legend id='GADoz'><style id='GADoz'><dir id='GADoz'><q id='GADoz'></q></dir></style></legend>