Skip to content

Instantly share code, notes, and snippets.

@watmough
Created December 3, 2018 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watmough/0246e4f7ab817975e2595d84c4f4b21e to your computer and use it in GitHub Desktop.
Save watmough/0246e4f7ab817975e2595d84c4f4b21e to your computer and use it in GitHub Desktop.
READER_HPP
#ifndef READER_HPP
#define READER_HPP
// Advent of Code 2018
// Handy text file reader returning vector<string>
// See: https://stackoverflow.com/a/1567703/33758
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
class line {
std::string data;
public:
friend std::istream &operator>>(std::istream &is, line &l) {
std::getline(is, l.data);
return is;
}
operator std::string() const { return data; }
};
std::vector<std::string> read_input(std::istream& ifs)
{
std::vector<std::string> lines;
std::copy(std::istream_iterator<line>(ifs),
std::istream_iterator<line>(),
std::back_inserter(lines));
return lines;
}
std::vector<std::string> read_input(const std::string& f)
{
auto ifs = std::ifstream(f,std::ifstream::in);
if (!ifs) {
std::cerr << "Unable to open file: " << f << "\n";
exit(-1);
}
return read_input(ifs);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment