为什么将整数从文件读取到2D向量导致所有值均为零
Why reading integers from file into a 2d vector is resulting all values to be zero?
我正在尝试将文本文件的整数矩阵读取到2D矢量中。
输入数据:
4 5
0 -0.5 -3 -1 -4
40 1 1 1 1
10 -2 -1 1 1
10 0 1 0 -1
我的代码:
ifstream InFile("Simplex_EX1.txt");
if (!InFile.is_open())
cout << "File could not be opened correctly";
vector<vector<int>> MyData;
int Rows, Columns;
InFile >> Rows >> Columns; // read first line - working
MyData.resize(Rows);
for (int i = 0; i < Rows; i++)
MyData[i].resize(Columns);
for (int i = 0; i < Rows; i++)
for (int j = 0; j < Columns; j++)
InFile >> MyData[i][j]; // read the rest - not working
InFile.close();
因此,我成功阅读了第一行,因此我正确调整了向量大小。但是,最终向量只是零。有人可以告诉我我在做什么错吗?请感谢
您正在尝试将浮点数 -0.5
读取为失败的整数。一旦流失败,在清除错误之前,它将不会读取其他内容。
相关文章:
- C++:如何循环通过向量中的整数元素
- 添加存储在向量中的大整数的函数出现问题
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- C++ 如何比较n个排序的整数向量以找到互元素?
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 如何将整数字符串转换为整数的二维向量?
- 如何将整数向量值插入字符向量
- 如何使用 STL 算法将整数向量转换为字符串向量?
- 使用192/256位整数求和无符号64位整数向量的点积的最快方法
- 查找多个向量之间的公共元素(无整数元素)
- 如何将整数向量插入到键中,标准::映射的值
- 整数对的向量转化为整数向量的向量
- 如何组合两个整数向量
- C++中整数向量的序列化/反序列化
- 如何从C++中的值向量构造整数<bool>值
- 使用整数初始化列表初始化长双精度的向量
- 将向量整数拆分为这些数字的整数
- 如何访问映射的向量(整数和字符串的向量)
- 将带有时间戳的向量整数连接为C++中的字符串