ยังไงเพื่อเปลี่ยนประเภทของส่วนประกอบใน C++เพิ่มหลายอาเรย์?

0

คำถาม

ฉันได้รับเมตริกซ์กับส่วนประกอบของประเภท unsigned char จากคนอื่นฟังก์ชันและผมก็กำลังพยายามหาแม็กซ์ของมันมูลค่าอยู่

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

ทุกส่วนประกอบเป็น integers และฉันหวังว่าฉันจะแคร์แม้ต้องไปรับบทบาทอื่เมตริกซ์ว่างที่ประเภท<น int ได้,2>งั้นแสดเชื้อโรคที่ติดต่ดทางเพศ::max_element()สรรคต่อปฏิบัติการณ์แต่ไม่มั่นใจยังไงต้องแคร์แม้ต้องไปรับบทบาทอื่ประเภทของเค้าหลายอาเรย์.

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

คำตอบที่ดีที่สุด

1

คุณไม่ต้องการที่จะใช้ max_element. char เป็น integral ประเภทเหมือน int:

อยู่บนคอมไพเลอร์เอ็กซ์พลอเรอร์

#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);
}

โปรแกรม 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 องมุมมอง

ถ้าคุณต้อง(สำหรับอื่นๆด้วยเหตุผล thatn ใช้ max_element)คุณ สามารถ ใช้ 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);

ซึ่งรอยนิ้วมือ อยู่บนคอมไพเลอร์เอ็กซ์พลอเรอร์

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

คุณยังสามารถ reshape มัน:

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

การพิมพ์

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

เพิ่มการยึดตามตัวอักษรแบบนั้องตอบคำถามที่ถามแค่ดีสำหรับเลือกแบบเส้นต่อเนื่อง
sehe

และนี่เป็นทุกอย่างโดยไม่มีการใช้ libfmt แค่ใช้ iostream สำหรับแสดงผล: coliru.stacked-crooked.com/a/d36a83decb67d92b (ค่อนข้างน่ากลัวใช่มั้ย:))
sehe

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่