Reputation: 4642
I'm looking for an elegant way to read in a text file into a 2x2 vector. I'm using this approach to write the data to a text file:
ofstream FILE(theFile, ios::out | ofstream::binary);
for(const auto& vt : data) {
std::copy(vt.begin(), vt.end(), std::ostream_iterator<double>(FILE, " "));
FILE << "\n\n";
}
My question is, is there a way to use the std::istream_iterator
in a similar way to be able to read in the contents of the text file?
EDIT:
Data:
std::vector<std::vector<double> > data;
Upvotes: 3
Views: 588
Reputation: 96810
You can make your own iterator that uses a vector as the underlying type. It can be used similar to 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>{};
}
Now you can initialize the vector with:
std::vector<std::vector<double>> v{line_iter<double>(FILE), line_iter<double>()};
Upvotes: 1
Reputation: 47794
Since you insist on using std::istream_iterator
, this is one way
#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( );
}
Upvotes: 5