Skip to content

Instantly share code, notes, and snippets.

@jason810496
Created May 20, 2024 16:00
Show Gist options
  • Select an option

  • Save jason810496/9372ffca8934896da5c6f32710cfaa59 to your computer and use it in GitHub Desktop.

Select an option

Save jason810496/9372ffca8934896da5c6f32710cfaa59 to your computer and use it in GitHub Desktop.
+----+-------+-------+-------+
| -1 | 24000 | 14000 | 26000 |
+----+-------+-------+-------+
| -1 | -1 | 6000 | 12000 |
+----+-------+-------+-------+
| -1 | -1 | -1 | 9000 |
+----+-------+-------+-------+
| -1 | -1 | -1 | -1 |
+----+-------+-------+-------+
void print_table(vector<vector<int> > &vec){
const char separator = ' ';
const char separator_v = '|';
const char separator_h = '-';
const char corner = '+';
const int padding = 1;
int n = vec.size();
int m = vec[0].size();
vector<int> max_col(m,0);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
max_col[i] = max(max_col[i],(int)to_string(vec[j][i]).size());
}
}
cout << corner;
for(int i=0; i<m; i++){
for(int j=0; j<max_col[i]+2*padding; j++) cout << separator_h;
cout << corner;
}
cout << "\n";
for(int i=0; i<n; i++){
cout << separator_v << " ";
for(int j=0; j<m; j++){
int gap = max_col[j] - (int)to_string(vec[i][j]).size();
int l_gap = gap - gap/2;
int r_gap = gap/2;
while(l_gap--) cout << separator;
cout << vec[i][j];
while(r_gap--) cout << separator;
cout << " " << separator_v << " ";
}
cout << "\n";
cout << corner;
for(int i=0; i<m; i++){
for(int j=0; j<max_col[i]+2*padding; j++) cout << separator_h;
cout << corner;
}
cout << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment