是否有一种优雅的方式来读取文本文件

C++ - Is there an elegant way to read in text files?

本文关键字:方式 读取 文件 取文本 一种 是否      更新时间:2023-10-16

我正在寻找一种优雅的方式将文本文件读取为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>()};

现场演示