Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iterator>
- #include <sstream>
- #include <stdexcept>
- #include <string>
- #include <vector>
- enum Type { Wall, Empty };
- auto constexpr wallChar = '1';
- auto constexpr emptyChar = '9';
- std::istream& operator>>(std::istream& in, Type& type)
- {
- char c;
- if (in >> c) {
- if (c == wallChar) type = Wall;
- else if (c == emptyChar) type = Empty;
- else throw std::runtime_error(std::string("unknown type ") + c);
- }
- return in;
- }
- std::ostream& operator<<(std::ostream& out, Type const& type)
- {
- return out << (type == Empty ? emptyChar : wallChar);
- }
- int main(int argc, const char * argv[])
- {
- std::string file =
- "1111\n"
- "1991\n"
- "1111";
- std::vector<std::vector<Type>> map;
- auto ssfile = std::stringstream(file); // instead use std::ifstream
- std::string line;
- while (std::getline(ssfile, line)) {
- std::vector<Type> row;
- auto ss = std::stringstream(line);
- std::copy(std::istream_iterator<Type>(ss), std::istream_iterator<Type>(), std::back_inserter(row));
- map.push_back(row);
- }
- std::cout << "the map has " << map.size() << " rows and " << map[0].size() << " columns" << std::endl;
- for (auto const& row : map) {
- for (auto const& x : row) {
- std::cout << x;
- }
- std::cout << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement