是否有一种优雅的方式来读取文本文件
C++ - Is there an elegant way to read in text files?
我正在寻找一种优雅的方式将文本文件读取为2x2矢量。我使用这种方法将数据写入文本文件:
ofstream FILE(theFile, ios::out | ofstream::binary);
for(const auto& vt : data) {
std::copy(vt.begin(), vt.end(), std::ostream_iterator<double>(FILE, " "));
FILE << "nn";
}
我的问题是,是否有一种方法可以以类似的方式使用std::istream_iterator
,以便能够读取文本文件的内容?
数据:
std::vector<std::vector<double> > data;
既然您坚持使用std::istream_iterator
,这是一种方法
#include <sstream>
//....
std::vector<double> v ;
std::string str ;
while ( std::getline(FILE, str) )
{
std::stringstream ss(str);
std::copy( std::istream_iterator<double>(ss),
std::istream_iterator<double>(),
std::back_inserter(v)
) ;
data.push_back( v ); // data is your vector of vector
v.clear( );
}
可以创建自己的迭代器,使用vector作为底层类型。它可以类似于std::istream_iterator
:
#include <vector>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
template <
class Value,
class Container = std::vector<Value>,
class charT = char,
class traits = std::char_traits<charT>
>
class line_iterator
: public std::iterator<std::input_iterator_tag, std::vector<Value>>
{
public:
using value_type = Value;
using container_type = Container;
private:
std::basic_istream<charT, traits>* is_;
std::basic_string<charT, traits> line;
std::basic_istringstream<charT, traits> str;
container_type temp;
int count = -1;
public:
line_iterator(std::basic_istream<charT, traits>& is,
int count = -1) noexcept
: is_(&is), count(count)
{ this->read(); }
constexpr line_iterator() noexcept : is_(nullptr)
{ }
constexpr line_iterator(const line_iterator& rhs) noexcept
: is_(rhs.is_), temp(rhs.temp), count(rhs.count)
{ };
constexpr container_type operator*() const noexcept
{
return std::move(temp);
}
line_iterator& operator++()
{
this->read();
return *this;
}
line_iterator operator++(int)
{
line_iterator copy(*this);
++*this;
return std::move(copy);
}
friend constexpr bool operator==(const line_iterator& lhs,
const line_iterator& rhs) noexcept
{
return lhs.is_ == rhs.is_;
}
friend constexpr bool operator!=(const line_iterator& lhs,
const line_iterator& rhs) noexcept
{
return !(lhs == rhs);
}
private:
void read()
{
temp.clear();
if (is_)
{
if (std::getline(*is_, line))
{
str.str(line);
if (count == -1)
temp.assign(
std::istream_iterator<Value, charT, traits>{str}, {});
else if (0 <= count)
std::copy_n(
std::istream_iterator<Value, charT, traits>{str},
count,
std::back_inserter(temp));
}
if (!*is_)
is_ = nullptr;
str.clear();
}
}
};
template <
class Value,
class Container = std::vector<Value>,
class charT = char,
class traits = std::char_traits<charT>
>
line_iterator<Value, Container, charT, traits>
constexpr line_iter(std::basic_istream<charT, traits>& is,
int count = -1) noexcept
{
return line_iterator<Value, Container, charT, traits>{is, count};
}
template<class Value>
constexpr line_iterator<Value> line_iter() noexcept
{
return line_iterator<Value>{};
}
现在可以用:
初始化vector对象std::vector<std::vector<double>> v{line_iter<double>(FILE), line_iter<double>()};
现场演示
相关文章:
- 如何以CV_16UC1的方式读取JPG图像并在屏幕上打印?
- 使用 ffmpeg 以编程方式读取 fMP4
- 最理想的情况是,如果一个变量在多个线程中读取,但只在一个线程中写入,那么它是否应该在写入线程中以非原子方式读取
- 如何使用 c++ 以编程方式读取地理数据
- 在十六进制编辑器中写入程序内存,并以编程方式读取
- C :如何以快速方式读取Char文件
- Windows 8:如何以编程方式读取用户文档文件夹路径(使用 c)
- 如何以与字节序无关的方式读取 UTF-16 代码点的长度
- 以非阻塞方式读取Linux中的原始输入
- 如何以这种特定的方式读取和处理文件中的文本
- C++以不同的方式读取两个输入文件
- 以较小、更频繁的读取或较大的读取方式读取输入
- c++以二进制方式读取文件.文件结束的问题
- 以编程方式读取internet explorer cookie
- 最适合的方式读取键盘输入在c++
- 关于以二进制方式读取字符的问题,您好
- 不能以任何方式读取函子类
- 图像处理-将矩阵中的16位写入文本文件,并用C++以不同的方式读取
- 以C++中的长度指示器方式读取二进制文件
- C++:如何以独立于平台的方式读取和写入多字节整数值