Why does the example output have more than one row? Are '1' and ' ' the only possible values in the input? Can same column have '1' in more than one row? If so, what should the output have then?
The numbers in the input can be '1' to '4' or ' '. The '2' in the input says that the information must be printed on the second line, and the linenumber where the '2' stands must be printed on this line (exactly on the column where it stands in the input). This is the first challenge. When you have this you can work further on it so it works as follows:
Ok, you have K columns. On row n of output, in column k, you do list (comma-separated) the rows of input that have value n in column k.
You obviously have to store the information in useful way.
Something crude would do for "exactly 3 columns and values 1-4". For example: std::vector<int> data[5][3];
(That does have a surplus row just to avoid some index calculation.)
On row n, column k you would store a value x into data with data[x][k].push_back(n);