Created
December 3, 2018 03:00
-
-
Save watmough/0246e4f7ab817975e2595d84c4f4b21e to your computer and use it in GitHub Desktop.
READER_HPP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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