0

In my main I have this:

vector<vector<u8>> matrix(size.first, vector<u8>(size.second));
for(auto i = 0; i < matrix.size(); ++i){
    for(auto j = 0; j < matrix[0].size(); ++j){
        matrix[i][j] = 5;
    }
}
print_matrix(matrix);

The function print_matrix is:

void print_matrix(vector<vector<u8>>& m){
    cout << "_______________________\n";
    for(size_t i (0) ; i < m.size(); ++i){
        auto& row = m[i];
        for(size_t j (0); j < row.size(); ++j){
            cout << row[j] << "-";
        }
        cout << endl;
    }
} 

I was expecting to get a matrix full of 5 printed on the stdout, but instead I get:

_______________________
-----
-----
-----
-----
-----

Why there are no numbers printed in the output?

NutCracker
  • 11,485
  • 4
  • 44
  • 68
ninazzo
  • 547
  • 1
  • 6
  • 17

1 Answers1

1

Try using range-based for loops like:

void print_matrix(std::vector<std::vector<u8>> const& m){
    std::cout << "_______________________\n";
    for (auto const& i : m) {
        for (auto const j : i){
            std::cout << static_cast<unsigned>(j) << "-";
            // or
            // std::cout << +j << "-";
        }
        std::cout << std::endl;
    }
} 

Remember that uint8_t is considered to be a raw character and, thus, it need to be converted to unsigned int in order to get it outputted as a numeric value.

NutCracker
  • 11,485
  • 4
  • 44
  • 68