c++将行getline放入数组中,用新行分隔
c++ getline into array, separated by new line
Basicly我有一个文本文件,其中的数字用新行分隔。我想将每个数字输入到一个数组中,当新行带有一个新数字时,该新数字应该插入到数组的下一个插槽中
因此该文件类似于:
10
20
36
在这样的东西中阅读这篇文章将起作用:
std::ifstream file {"file_name"};
int t;
std::vector<int> nums;
while(file >> t)
numes.push_back(t);
或者,如果您对std-lib:感到满意
std::ifstream file {"file_name"};
std::vector<int> nums {
std::istream_iterator<int> { file },
std::istream_iterator<int> { }
};
之后:
for(int n : nums)
std::cout << n ", ";
将打印
10, 20, 36,
到stdout。
相关文章:
- 使用新行和不使用新行读取文件
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 嵌套结构,从由空行分隔的文件中读取数据
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 在C++中打印用新行分隔的6,5,4,3,2个字符
- c++将行getline放入数组中,用新行分隔
- 如何在c++中有效地读取新行分隔的整数
- c++从文件中读取用空格和新行分隔的单词