Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iterator>
  4. #include <sstream>
  5. #include <stdexcept>
  6. #include <string>
  7. #include <vector>
  8.  
  9. enum Type { Wall, Empty };
  10.  
  11. auto constexpr wallChar = '1';
  12. auto constexpr emptyChar = '9';
  13.  
  14. std::istream& operator>>(std::istream& in, Type& type)
  15. {
  16.     char c;
  17.     if (in >> c) {
  18.         if (c == wallChar) type = Wall;
  19.         else if (c == emptyChar) type = Empty;
  20.         else throw std::runtime_error(std::string("unknown type ") + c);
  21.     }
  22.  
  23.     return in;
  24. }
  25.  
  26. std::ostream& operator<<(std::ostream& out, Type const& type)
  27. {
  28.     return out << (type == Empty ? emptyChar : wallChar);
  29. }
  30.  
  31. int main(int argc, const char * argv[])
  32. {
  33.     std::string file =
  34. "1111\n"
  35. "1991\n"
  36. "1111";
  37.  
  38.     std::vector<std::vector<Type>> map;
  39.  
  40.     auto ssfile = std::stringstream(file); // instead use std::ifstream
  41.  
  42.     std::string line;
  43.     while (std::getline(ssfile, line)) {
  44.         std::vector<Type> row;
  45.  
  46.         auto ss = std::stringstream(line);
  47.  
  48.         std::copy(std::istream_iterator<Type>(ss), std::istream_iterator<Type>(), std::back_inserter(row));
  49.         map.push_back(row);
  50.     }
  51.  
  52.  
  53.     std::cout << "the map has " << map.size() << " rows and " << map[0].size() << " columns" << std::endl;
  54.  
  55.     for (auto const& row : map) {
  56.         for (auto const& x : row) {
  57.             std::cout << x;
  58.         }
  59.         std::cout << std::endl;
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement