按字符C ++读取文件 [英] Read files by character C++

查看:167
本文介绍了按字符C ++读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用数字读取一个文件
ex:

I wanted to read a file with numbers ex:

2
2 3 
2 3 4 
5 6 7

3 
2 2
1 2 
2 3 

我使用getline()函数,并将结果存储在向量(字符串)中。但是,当我访问向量中的元素时,存储了具有空格的整行。我想通过数字存储元素(数字表示一个矩阵)

I used the getline () function and I store the results in a vector (string). However, when I access the elements in the vector, the entire line with spaces were stored. I wanted to store elements by number (the numbers represent a matrix)

推荐答案

#include<fstream>
#include<iterator>
#include<vector>

int main(int, char*[])
{
    std::ifstream file("numbers.txt");
    std::vector<int> data((std::istream_iterator<int>(file)),
                          std::istream_iterator<int>());
}

会给你一个整数的向量。

will give you a vector of integers.

这篇关于按字符C ++读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆