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

      <bdo id='wxGet'></bdo><ul id='wxGet'></ul>

    1. <small id='wxGet'></small><noframes id='wxGet'>

      <tfoot id='wxGet'></tfoot>
      <legend id='wxGet'><style id='wxGet'><dir id='wxGet'><q id='wxGet'></q></dir></style></legend>

      1. 如何在C++中将文件读入向量?

        时间:2023-09-15
          <tbody id='I8FpV'></tbody>
        • <bdo id='I8FpV'></bdo><ul id='I8FpV'></ul>
            <legend id='I8FpV'><style id='I8FpV'><dir id='I8FpV'><q id='I8FpV'></q></dir></style></legend>

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

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

                <tfoot id='I8FpV'></tfoot>

                1. 本文介绍了如何在C++中将文件读入向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要从 .data.txt 文件中读取每行包含一个新的 float 数字到一个向量中.

                  I need to read from a .data or .txt file containing a new float number on each line into a vector.

                  我已经广泛搜索并应用了许多不同的方法,但每次我得到相同的结果时,Main.size()0 和错误说明向量下标超出范围",所以很明显向量只是没有将任何内容读入文件.

                  I have searched far and wide and applied numerous different methods but every time I get the same result, of a Main.size() of 0 and an error saying "Vector Subscript out of Range", so evidently the vector is just not reading anything into the file.

                  注意:该文件既在文件夹中,也包含在 VS 项目中.

                  Note: the file is both in the folder and also included in the VS project.

                  无论如何,这是我的代码:

                  Anyway, here's my code:

                  #include <iostream>
                  #include <fstream>
                  #include <sstream>
                  #include <vector>
                  #include <string>
                  
                  using namespace std;
                  
                  int main() {
                  
                      vector<double> Main;
                      int count;
                      string lineData;
                      double tmp;
                  
                      ifstream myfile ("test.data", ios::in);
                  
                      double number;  
                  
                      myfile >> count;
                      for(int i = 0; i < count; i++) {
                          myfile >> tmp;
                          Main.push_back(tmp);
                          cout << count;
                      }
                  
                      cout << "Numbers:
                  ";
                      cout << Main.size();
                      for (int i=0; i=((Main.size())-1); i++) {
                          cout << Main[i] << '
                  ';
                      }
                  
                      cin.get(); 
                      return 0;
                  }
                  

                  我得到的结果总是很简单:

                  The result I get is always simply:

                  Numbers:
                  0
                  

                  推荐答案

                  你的循环错了:

                  for (int i=0; i=((Main.size())-1); i++) {
                  

                  试试这个:

                  for (int i=0; i < Main.size(); i++) {
                  

                  此外,将数字读入向量并将它们写入标准输出的更惯用的方法是:

                  Also, a more idiomatic way of reading numbers into a vector and writing them to stdout is something along these lines:

                  #include <iostream>
                  #include <iterator>
                  #include <fstream>
                  #include <vector>
                  #include <algorithm> // for std::copy
                  
                  int main()
                  {
                    std::ifstream is("numbers.txt");
                    std::istream_iterator<double> start(is), end;
                    std::vector<double> numbers(start, end);
                    std::cout << "Read " << numbers.size() << " numbers" << std::endl;
                  
                    // print the numbers to stdout
                    std::cout << "numbers read in:
                  ";
                    std::copy(numbers.begin(), numbers.end(), 
                              std::ostream_iterator<double>(std::cout, " "));
                    std::cout << std::endl;
                  
                  }
                  

                  尽管您应该检查 ifstream 的状态是否有读取错误.

                  although you should check the status of the ifstream for read errors.

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

                  上一篇:std::引用向量 下一篇:为什么 std::vector 的元素不需要默认构造函数?

                  相关文章

                  最新文章

                    <bdo id='SNI8o'></bdo><ul id='SNI8o'></ul>

                2. <tfoot id='SNI8o'></tfoot>

                  1. <small id='SNI8o'></small><noframes id='SNI8o'>

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