Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you should have a matrix/grid/2darray class of some sort</p> <pre><code>//A matrix class that holds objects of type T template&lt;class T&gt; struct matrix { //a constructor telling the matrix how big it is matrix(unsigned columns, unsigned rows) :c(columns), data(columns*rows) {} //construct a matrix from a stream matrix(std::ifstream&amp; stream) { unsigned r; stream &gt;&gt; c &gt;&gt; r; data.resize(c*r); stream &gt;&gt; *this; if (!stream) throw std::runtime_error("invalid format in stream"); } //get the number of rows or columns unsigned columns() const {return c;} unsigned rows() const {return data.size()/c;} //an accessor to get the element at position (column,row) T&amp; operator()(unsigned col, unsigned row) {assert(col&lt;c &amp;&amp; row*c+col&lt;data.size()); return data[data+row*c+col];} protected: unsigned c; //number of columns std::vector&lt;T&gt; data; //This holds the actual data }; </code></pre> <p>And then you simply overload operator&lt;&lt;</p> <pre><code>template&lt;class T&gt; std::istream&amp; operator&gt;&gt;(std::istream&amp; stream, matrix&lt;T&gt;&amp; obj) { //for each row for(int r=0; r&lt;obj.rows(); ++r) { //for each element in that row for(int c=0; c&lt;obj.cols(); ++c) //read that element from the stream stream &gt;&gt; obj(c, r); //here's the magic line! } //as always, return the stream return stream; } </code></pre> <p>Fairly straightforward.</p> <pre><code>int main() { std::ifstream input("input.txt"); int r, c; while(input &gt;&gt; c &gt;&gt; r) { //if there's another line matrix&lt;char&gt; other(c, r); //make a matrix if (!(input &gt;&gt; other)) //if we fail to read in the matrix break; //stop //dostuff } //if we get here: invalid input or all done } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload