Cómo cambiar el tipo de elementos en C++ impulso multi matriz?

0

Pregunta

Recibo una matriz con elementos de tipo unsigned char de otra función y estoy tratando de encontrar a su máximo valor.

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

Todos los elementos son enteros y así tenía la esperanza de proceder a la refundición de la matriz como de tipo <int, 2>, a continuación, realizar std::max_element (de operación), pero seguro de cómo proceder a la refundición de tipo de impulsar el multi de la matriz.

boost c++
2021-11-21 09:26:22
1

Mejor respuesta

1

Usted no necesita utilizar max_element. char es un tipo integral como int:

Vivo En El Compilador 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);
}

Programa 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'

La Reinterpretación De Vista

Si usted debe (por otras razones thatn el uso de max_element) usted puede utilizar un 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);

Que se imprime en Vivo En el Compilador 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'}}

También puede cambiar:

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

Impresión

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'}}
2021-11-21 13:34:12

Agrega un literal de la respuesta a la pregunta que pidió para la buena medida
sehe

Y aquí está todo sin usar libfmt, usando sólo iostream para la salida: coliru.stacked-crooked.com/a/d36a83decb67d92b (bastante horrible, a la derecha :))
sehe

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................