从文件到矢量获取每一行后,数字都会更改或破坏

将每一行从文件转换为向量后,数字都会更改或破坏! IDK他们为何或如何被摧毁

const
    reference = [{ id: 1,value: 10 },{ id: 2,{ id: 3,{ id: 4,value: 5 }],result = reference.map((sum => ({ value },i) => sum += i && value)(0));

console.log(result);

但是如果我删除first_matrix >> temp; 那没关系 顺便说一句,给我第一个[i] [j] -6.27744e + 66 和Yep IK,我可以轻松编写 我的“ first.txt”中有这样的内容

ifstream first_matrix;
first_matrix.open("first.txt");
double temp;
int d;//Dimensions
vector<double> firstv(0);
first_matrix >> d;
//counter should be (d*d) + 1
//cuz we get dimension and here is 2
//and we want get 4 numbers(d*d) and the first line is dimension so (+ 1)
for ( i = 0; i < (d*d) + 1; i++)
{
  first_matrix >> temp;
  firstv.push_back(temp);
}
double** first = new double* [d];
for (i = 0; i < d; i++)
{
    first[i] = new double[d]; 
    for (j = 0; j < d; j++)
    {

       //as you can see I want to 
       //put 4numbers of file.txt which is 10,16,102,15
       //into first[i][j]
       first_matrix >> first[i][j];
       std::cout << "Result [" 
                 << i << "] ["
                 << j << "] : "
                 << first[i][j]; 
    }


}

所以我有一个2 * 2的矩阵,给我们4个数字。 但是我的问题是从first.txt获取firt [i] [j]。 结果应该是这样

2
10
16
102
15
baili251810659 回答:从文件到矢量获取每一行后,数字都会更改或破坏

问题出在下面我注释掉的代码上。循环从文件中读取数字。因此,当执行其他循环时,已到达文件末尾,因此返回0值并将其存储到first中。

#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
ifstream first_matrix;
first_matrix.open("first.txt");
double temp;
int d;//Dimensions
vector<double> firstv(0);
first_matrix >> d;

// The problem is with the commented code below. It's reading the data from the
// from the file,which keeps the loops below from reading it.
//for ( int i = 0; i < (d*d) + 1; i++)
//  {
//    first_matrix >> temp;
//    firstv.push_back(temp);
//  }
double** first = new double* [d];
for ( int i = 0; i < d; i++)
  {
    first[i] = new double[d];
    for (int j = 0; j < d; j++)
      {

        // This was putting zeroes into first[i][j]
        first_matrix >> first[i][j];
        std::cout << "Result ["
                  << i << "] ["
                  << j << "] : "
                  << first[i][j] << std::endl;
      }


  }
}

以上代码的输出:

Result [0] [0] : 10
Result [0] [1] : 16
Result [1] [0] : 102
Result [1] [1] : 15
本文链接:https://www.f2er.com/3152377.html

大家都在问