string repeat on a max key of map


I am attempting to print the keys of a map. The format I would like is
K1 |
..
..
K99 |

I need to therefore count the number of digits in the key, and then space out accordingly.
The code generates a compile error, see below
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
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#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  5 7  5 8  6 4  7 6  8 9  9 10"};
  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

  // header
	for (const auto & p : m) {
		std::cout << "  " << p.first << "";
	}

	std::cout << "\n";

  std::string ss;

  // body
  for (const auto & p : m) {    
    std::cout << p.first << ss( (dc - digit_count(p.first)), ' ') << "|";
    std::cout << "\n";
  }

  return 0;
}

compile error:
1
2
3
4
5
6
c++ -g -std=c++14 -pedantic -Wall -Wpointer-arith -Wwrite-strings -Wcast-qual -Wcast-align -Wformat-security -Wformat-nonliteral -Wmissing-format-attribute -Winline -funsigned-char -o "str_repeat" "str_repeat.cpp" (in directory: /c++/learn_c++)
str_repeat.cpp: In function ‘int main()’:
str_repeat.cpp:39:65: error: no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string<char>}) (int, char)’
     std::cout << p.first << ss( (dc - digit_count(p.first)), ' ') << "|";
                                                                 ^
Compilation failed.


Can anybody suggest how to solve this?
What is this supposed to do? ss( (dc - digit_count(p.first)), ' ')
I figured out!

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
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
#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  5 7  5 8  6 4  7 6  8 9  9 10"};
  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

  // header
	for (const auto & p : m) {
		std::cout << "  " << p.first << "";
	}

	std::cout << "\n";

  std::string ss = "";

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

  return 0;

}
Topic archived. No new replies allowed.