(C ++)从文本文件中读取数字 [英] (C++) Reading digits from text file

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

问题描述

我有一个文本文件,如下所示:

I have a text file which looks like this:

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511

等共20行. 我想做的是从文本文件中读取每个数字,并将它们放入整数数组(一个元素=一位数字).我怎么只能从此文本文件中读取一位数字,而不是整行?

and etc. 20 lines in total. What I want to do is to read every digit from the text file and put them into an array of integers(one element = one digit). How can I read only one digit from this text file, not the whole line?

推荐答案

有几种方法可以完成您要寻找的内容,在本文中,我将介绍三种不同的方法.他们三个都假定您使用std::ifstream ifs ("filename.txt")打开文件,并假设您的"数组"实际上是声明为std::vector<int> v的向量.

There are several ways to accomplish what you are looking for, in this post I'll describe three different methods. All three of them assume that you open your file using an std::ifstream ifs ("filename.txt") and that your "array" is actually a vector declared as std::vector<int> v.

在这篇文章的结尾,还有一些关于如何加快插入向量的建议.

最简单的方法是使用operator>>一次读取一个char,然后从返回的值中减去'0'.

The most simple approach is to read one char at a time using operator>> and then subtract '0' from the value returned.

标准保证'0''9'是顺序的,并且由于char只是打印在不同事物上的数值,因此可以隐式转换为int.

The standard guarantee that '0' through '9' are sequential, and since a char is nothing but a numeric value printed in a different matter it can implicitly be casted to int.

char c;

while (ifs >> c)
  v.push_back (c - '0');


我爱STL,讨厌编写循环.

在很多情况下,这将被视为" c ++的实现方式",尤其是在与STL迷们交谈时,尽管它需要编写更多的代码..


I love the STL, and hate writing loops..

This will by many be treated as the "c++ way to do it", espacially if you are talking to STL-fanboys, though it requires a lot more code to write..

#include <algorithm>
#include <functional>
#include <iterator>

...

std::transform (
  std::istream_iterator<char> (ifs),
  std::istream_iterator<char> (), 
  std::back_inserter (v),
  std::bind2nd (std::minus<int> (), '0')
);


我不想编写循环,但是为什么不使用lambda?


I don't want to write loops, but why not use a lambda? c++11

#include <algorithm>
#include <functional>
#include <iterator>

...

std::transform (
  std::istream_iterator<char> (iss),
  std::istream_iterator<char> (),
  std::back_inserter (v),
  [](char c){return c - '0';}
);


我的std::vector每次插入都会重新分配存储空间吗?

是的,可能是.为了加快处理速度,您可以在开始执行任何插入操作之前在向量中保留存储空间,如下所示.


Will my std::vector reallocate storage upon each insertion?

Yes, probably. To speed things up you can reserve storage in your vector before you start doing any insertions, as in the below.

ifs.seekg (0, std::ios::end); // seek to the end of your file
v.reserve (ifs.tellg ()    ); // ifs.tellg () -> number of bytes in it
ifs.seekg (0, std::ios::beg); // seek back to the beginning

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

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