在获取从文件到矢量的每一行之后,数字将被更改或销毁
After Getting Every line from File to Vector, Digits are Changed or Destroyed
获取从文件到矢量的每一行后,数字将被更改或销毁!!IDK为什么或如何销毁
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];
}
}
但是如果我删除first_matrix>>temp;那没关系顺便说一句,第一次[i][j]给我-6.27744e+66是的IK我可以很容易地写作我在我的"first.txt"中有这样的东西
2
10
16
102
15
所以我有一个矩阵,它是2*2,给我们4个数字。但我的问题是从first.txt中获取firt[i][j]。结果应该像这个
Result[0][0] = 10
Result[0][1] = 16
Result[1][0] = 102
Result[1][1] = 15
问题出在我下面评论的代码上。那里的循环正在从文件中读取数字。因此,当其他循环执行时,已到达文件末尾,因此返回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
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 计算十进制 c++ 之后的数字
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何在向量中某个数字之后重新计算进程
- 预期输出之后的所有这些数字是多少
- 在获取从文件到矢量的每一行之后,数字将被更改或销毁
- C++ 在地板函数之后以十六进制打印数字
- 在每个偶数特定数字之后,插入向量中
- 如何在C ++中打印特定位数的位数?例如,总共打印8位数字(小数点之前+之后的组合)
- C :Bool方法在COUT语句之后返回意外数字
- 小数点之后,如何指定有多少个数字
- C 如何在文件中的特定字符串之后获取数字
- std::queue::size() 可以在 size() == 0 的 pop() 之后返回一个巨大的数字
- 如何从 9 和 90 中提取 2.9 和 2.09,即获取数字等于最大编号的小数部分.二分位数之后的数字
- 默认值仅适用于 8 和 9.之后,它使用第一个数字并将其视为 1,2,3
- 在waitpid()之后来自WTERMSIG宏的异常信号数字
- 如何在第 "." 点之后将数字放入数组中
- 仅读取文本文件中的数字并存储在数组中.之后,读取第一行来存储在数组中
- 在c++中截断数字(点之后)
- 如何在读取文本文件时跳到每行最后一个数字之后的下一行