2d Matrix pretty print


I have a 2d map structure that is output as follows:
1
2
3
4
5
6
1|2 
2|3 
3|1 
4|1 5 
5|3 6 
6|4 


But I would ideally like prefer it to look as follows:
1
2
3
4
5
6
1|010000
2|001000
3|100000
4|100010
5|001001
6|000100

Basically a 1 appears for each number listed in the previous list, otherwise zero is shown.
Could anybody assist with attempting the transformation?
My code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <map>
#include <sstream>
#include <iomanip>

int digit_count(int number) {
  int digits = 0;
  if (number < 0) digits = 1; // remove this line if '-' counts as a digit
  while (number) {
      number /= 10;
      digits++;
  }
  return digits;
}

int main () {
  unsigned short v1,v2;
  std::istringstream  stm {"1 2  2 3  3 1  4 1  4 5  5 3  5 6  6 4 "};
  std::map<unsigned short, std::map <unsigned short, unsigned short> > m;
  while (stm >> v1 >> v2) {
    m[v1]; m[v2]; // create the key(s), v2 required as well in case there are no outbounds
    m[v1][v2] = 1; // set the value
  }

  int dc = digit_count(m.rbegin()->first); // equals 10
  std::string ss = "";

  for (const auto & p : m) {
    std::cout << p.first << ss.append(" ",  (dc - digit_count(p.first))) << "|";
    for (const auto & x : p.second)
      std::cout << x.first << " ";
    ss = "";
    std::cout << "\n";
  }

  return 0;
}

You could replace
1
2
    for (const auto & x : p.second)
      std::cout << x.first << " ";

by
1
2
3
    std::string outmask = "000000";
    for (const auto & x : p.second) outmask[x.first - 1] = '1';
    std::cout << outmask;


I would recommend using header
#include <string>
really clever. thanks!!
Topic archived. No new replies allowed.