简体   繁体   English

如何创建二维矩阵 c++ 的水平镜像逆?

[英]How to create the horizontally mirror inverse of a 2d matrix c++?

For example if the input is:例如,如果输入是:

21 26 31 36
22 27 32 37
23 28 33 38
24 29 34 39
25 30 35 40

the output should be: output 应该是:

25 30 35 40
24 29 34 39
23 28 33 38
22 27 32 37
21 26 31 36

Al the rows are swapped until the middle row.所有行都交换到中间行。

The first row should be swapped with the last one, the second with the before last one row, etc...第一行应与最后一行交换,第二行应与最后一行交换,等等...

This is my code yet:这是我的代码:

int A[100][100] = { 0 };
int n, m;
std::cin >> n >> m;


for (int k = 0, j = n - 1; k < j; k++, --j)
     std::swap(A[k], A[j]);

number of rows(n), columns(m), for n, m < 100 It shows me the same matrix as before. number of rows(n), columns(m), for n, m < 100它向我展示了与以前相同的矩阵。

You can swap a two-dimensional array either using the standard algorithm std::swap_ranges or by writing "manually" a loop.您可以使用标准算法std::swap_ranges或通过“手动”编写循环来交换二维数组。

Here is a demonstrative program.这是一个演示程序。

#include <iostream>
#include <iterator>
#include <algorithm>

int main() 
{
    const size_t M = 5, N = 4;
    int a[M][N] = 
    {
        { 21, 26, 31, 36 },
        { 22, 27, 32, 37 },
        { 23, 28, 33, 38 },
        { 24, 29, 34, 39 },
        { 25, 30, 35, 40 }
    };

    for ( const auto &row : a )
    {
        for ( const auto &item : row )
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }
    std::cout << '\n';

    std::swap_ranges( std::begin( a ), std::next( std::begin( a ), M / 2 ), 
                      std::rbegin( a ) );

    for ( const auto &row : a )
    {
        for ( const auto &item : row )
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }
    std::cout << '\n';

    for ( std::size_t i = 0; i < N / 2; i++ )
    {
        std::swap( a[i], a[M - i - 1] );
    }

    for ( const auto &row : a )
    {
        for ( const auto &item : row )
        {
            std::cout << item << ' ';
        }
        std::cout << '\n';
    }
    std::cout << '\n';

    return 0;
}

Its output is它的 output 是

21 26 31 36 
22 27 32 37 
23 28 33 38 
24 29 34 39 
25 30 35 40 

25 30 35 40 
24 29 34 39 
23 28 33 38 
22 27 32 37 
21 26 31 36 

21 26 31 36 
22 27 32 37 
23 28 33 38 
24 29 34 39 
25 30 35 40 

Pay attention to that variable length arrays is not a standard C++ feature.请注意,可变长度 arrays 不是标准 C++ 功能。 It means that the sizes of an array shall be known at compile time.这意味着在编译时应该知道数组的大小。 If you want that it is the user who will enter the sizes then use the standard class template std::vector .如果您希望用户输入尺寸,请使用标准 class 模板std::vector

If you have an already defined array with some maximum values of its sizes and the user may specify its sub-array then the array should be at least zero-initialized如果您有一个已经定义的数组,它的大小有一些最大值,并且用户可以指定它的子数组,那么该数组应该至少初始化为零

You can swap the two dimensional array using std::reverse :您可以使用std::reverse交换二维数组:

#include <algorithm>
#include <iostream>

int main()
{
    int A[100][100] = { {21, 26, 31, 36},
                        {22, 27, 32, 37},
                        {23, 28, 33, 38},
                        {24, 29, 34, 39},
                        {25, 30, 35, 40} };

    std::reverse(&A[0], &A[5]);  // Starting and ending ranges are &A[0] and &A[5]

    // Output results
    for (int i = 0; i < 5; ++i)
    {
        for (int j = 0; j < 4; ++j)
            std::cout << A[i][j] << " ";
        std::cout << "\n";
    }
}

Output: Output:

25 30 35 40 
24 29 34 39 
23 28 33 38 
22 27 32 37 
21 26 31 36 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM