-
-
Save HappyCerberus/96a02ea8863a3651b38c5bcf433edc1f to your computer and use it in GitHub Desktop.
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
enum class Direction { | |
Right, | |
Left, | |
Up, | |
Down | |
}; | |
struct Order { | |
Direction direction; | |
uint32_t count; | |
friend std::istream& operator>>(std::istream& s, Order& order) { | |
char dir; | |
if (s >> dir >> order.count) switch (dir) { | |
case 'D': order.direction = Direction::Down; break; | |
case 'U': order.direction = Direction::Up; break; | |
case 'L': order.direction = Direction::Left; break; | |
case 'R': order.direction = Direction::Right; break; | |
default: throw std::runtime_error("Can't parse Order, wrong direction."); | |
} | |
return s; | |
} | |
}; | |
// Overloading the stream extraction operator enables the use | |
// std::views::istream and a simple copy. | |
std::vector<Order> data; | |
std::ranges::copy(std::views::istream<Order>(file), std::back_inserter(data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment