Search code examples
c++boostboost-multi-array

How to change type of elements in C++ boost multi array?


I receive a matrix with elements of type unsigned char from another function and I am trying to find its max value.

boost::multi_array<unsigned char, 2> matrix;

All elements are integers and so I was hoping to recast matrix as type <int, 2> then perform std::max_element() operation, but unsure how to recast type of a boost multi array.


Solution

  • You don't need that to use max_element. char is an integral type just like int:

    Live On Compiler Explorer

    #include <boost/multi_array.hpp>
    #include <fmt/ranges.h>
    #include <algorithm>
    
    int main() {
        using boost::extents;
        boost::multi_array<unsigned char, 2> matrix(extents[10][5]);
    
        std::iota(         //
            matrix.data(), //
            matrix.data() + matrix.num_elements(), '\x30');
    
        fmt::print("matrix: {}\n", matrix);
    
        auto [a, b] = std::minmax_element(matrix.data(),
                                        matrix.data() + matrix.num_elements());
    
        // as integers
        fmt::print("min: {}, max {}\n", *a, *b);
        // as characters
        fmt::print("min: '{:c}', max '{:c}'\n", *a, *b);
    }
    

    Program stdout

    matrix: {{48, 49, 50, 51, 52}, {53, 54, 55, 56, 57}, {58, 59, 60, 61, 62}, {63, 64, 65, 66, 67}, {68, 69, 70, 71, 72}, {73, 74, 75, 76, 77}, {78, 79, 80, 81, 82}, {83, 84, 85, 86, 87}, {88, 89, 90, 91, 92}, {93, 94, 95, 96, 97}}
    min: 48, max 97
    min: '0', max 'a'
    

    Reinterpreting View

    If you must (for other reasons thatn using max_element) you can use a multi_array_ref:

    // reinterpreting view:
    boost::multi_array_ref<const char, 2> view(
        reinterpret_cast<const char*>(matrix.data()),
        std::vector(matrix.shape(), matrix.shape() + 2));
    
    fmt::print("view: {}\n", view);
    

    Which prints Live On Compiler Explorer

    view: {{'0', '1', '2', '3', '4'}, {'5', '6', '7', '8', '9'}, {':', ';', '<', '=', '>'}, {'?', '@', 'A', 'B', 'C'}, {'D', 'E', 'F', 'G', 'H'}, {'I', 'J', 'K', 'L', 'M'}, {'N', 'O', 'P', 'Q', 'R'}, {'S', 'T', 'U', 'V', 'W'}, {'X', 'Y', 'Z', '[', '\'}, {']', '^', '_', '`', 'a'}}
    

    You can also reshape it:

    view.reshape(std::vector{25, 2});
    fmt::print("reshaped: {}\n", view);
    

    Printing

    reshaped: {{'0', '1'}, {'2', '3'}, {'4', '5'}, {'6', '7'}, {'8', '9'}, {':', ';'}, {'<', '='}, {'>', '?'}, {'@', 'A'}, {'B', 'C'}, {'D', 'E'}, {'F', 'G'}, {'H', 'I'}, {'J', 'K'}, {'L', 'M'}, {'N', 'O'}, {'P', 'Q'}, {'R', 'S'}, {'T', 'U'}, {'V', 'W'}, {'X', 'Y'}, {'Z', '['}, {'\', ']'}, {'^', '_'}, {'`', 'a'}}