将空格分隔的文件读取到动态数组C++中
Reading file seperated by white spaces into dynamic array C++
嗨,我将从文件中读取并将用空格分隔的数字放入不同的数组中。要读取的示例文件
15
10 2
20 1
30 1
这就是我所做的
while(!file.eof()){
file>>first[count++];
}
通过这样做,我逐行读取,但我想读取直到空格并将空格之后的数字放入不同的数组中。结果数组将如下所示(假设第一个和第二个是动态整数数组)
first={15,10,20,30}
second ={0,2,1,1}
你应该使用字符串流。
#include <sstream>
...
string line;
while(getline(file, line))
{
istringstream iss(line);
int firstNumOnLine, secondNumOnLine.
iss >> firstNumOnLine;
first.push_back(firstNumOnLine);
if(iss >> secondNumOnLine)
{
//...
second.push_back(secondNumOnLine)
}
else
{
//... there was no second number on the line.
second.push_back(0);
}
}
这里,first
和second
被假定为向量
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 输出没有重复元素的动态数组(收缩数组)C++
- 正在插入动态数组
- 如何在动态数组上使用搜索函数
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 静态数组的自由动态数组
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 为什么 c++ 动态数组的大小没有改变?
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 指针的 C++ 动态数组 - 何时需要使用它?
- 删除动态数组时未定义标识符
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 动态数组的 C++ 重载加运算符
- 基于数组的列表 - 动态数组创建时出错