简体   繁体   English

置换2D数组中的元素

[英]Permute an element in 2d-array

I'm attempting to shift zeros of a matrix to get minors of it by using in my program 2d-array. 我试图通过在我的程序2d-array中使用,将矩阵的零移位以获得其次要元素。 How correctly delete (shift) elements of 2d-array? 如何正确删除(移位)二维数组元素?

I know how to handle this problem with 1d-array by permuting its element 我知道如何通过排列其元素来处理1d数组的问题

for (int i = DEL; i < (SIZE - 1); i++)
    array[i] = array[i + 1];

Where DEL - index of element, which we want delete, SIZE - size of array. 其中DEL-我们要删除的元素索引,SIZE-数组大小。 But I've got not the same result with multidimensional-array: 但是多维数组的结果却不一样:

for (int i = DEL; i < (SIZE - 1); i++)
    for (int j = DEL; j < (SIZE - 1); j++)
        array[i][j] = array[i+1][j+1];

Where is a mistake? 哪里有错?

#include <iostream>

using namespace std;

int main()
{
    int array[3][3] = {
        1, 2, 3,
        4, 5, 6, 
        7, 8, 9};

    // Setting to zero 2nd row and 3rd column
    int m = 1; // 2
    int n = 2; // 3


    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            array[m][j] = 0;
            array[i][n] = 0;
            cout << array[i][j];
            // Trying to shift every element, which equals zero
            if (array[i][j] == 0)
                for (int k = i; k < 2; k++)
                    for (int l = j; l < 2; l++)
                        array[k][l] = array[k+1][l+1];          

        }
        cout << endl;
    }



    cout << endl;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            cout << array[i][j];
        cout << endl;
    }
}

I get: 我得到:

120
000
780

120
000
780

But actually I want the last output be like that: 但是实际上我希望最后的输出是这样的:

120
780
000

I think the term "permuting" does not fit here. 我认为“排列”一词在这里不合适。 You want to delete rows and columns from a 2 dimensional array. 您要从二维数组中删除行和列。

However you made some semantic errors. 但是,您犯了一些语义错误。 I fixed this for you and show you the corrected code below. 我已为您修复此问题,并在下面显示了更正的代码。

One big advice. 一个大建议。 Try to shrink a big problem into several smaller problems. 尝试将一个大问题缩小为几个小问题。 Your trying too much in multi-nested for loops. 您在多嵌套循环中尝试过多。 Do one after the other. 一个接一个地做。

Please see: 请参见:

#include <iostream>

constexpr size_t MaxArraySize = 3;

int main()
{
    int array[MaxArraySize][MaxArraySize] = {
        1, 2, 3,
        4, 5, 6,
        7, 8, 9 };

    // Setting to zero 2nd row and 3rd column
    int rowToDelete = 1; // 2
    int colToDelete = 2; // 3

    // Set cell to delete to 0
    for (int row = 0; row < MaxArraySize; ++row)
    {
        for (int col = 0; col < MaxArraySize; ++col)
        {
            array[rowToDelete][col] = 0;
            array[row][colToDelete] = 0;
            std::cout << array[row][col];
        }
        std::cout << '\n';
    }

    // First shift all rows
    for (int row = rowToDelete; row < MaxArraySize - 1; ++row)
        for (int col = 0; col < MaxArraySize; ++col)
            array[row][col] = array[row+1][col];

    // Then shift all cols
    for (int col = colToDelete; col < MaxArraySize-1; ++col)
        for (int row = 0; row < MaxArraySize; ++row)
            array[row][col] = array[row][col+1];

    // Set the cells that were shifted away to 0
    for (int row = 0; row < MaxArraySize; ++row)
        array[row][MaxArraySize - 1] = 0;
    for (int col = 0; col < MaxArraySize; ++col)
        array[MaxArraySize - 1][col] = 0;

    // Show result
    std::cout << "\n\nResult\n\n";
    for (int row = 0; row < MaxArraySize; ++row)
    {
        for (int col = 0; col < MaxArraySize; ++col)
            std::cout << array[row][col];
        std::cout << '\n';
    }
}

2nd and even more important. 第二甚至更重要。 You are using plain C code with plain C-Style arrays. 您正在使用带有纯C样式数组的纯C代码。 The only C++ in your code is using iostreams. 您代码中唯一的C ++使用的是iostream。 That is not so good. 那不是很好。

Try to use C++. 尝试使用C ++。 Never ever use plain C-Style array. 永远不要使用纯C样式数组。 Try to use algorithms and containers from the STL. 尝试使用STL中的算法和容器。 Use better names for variables. 为变量使用更好的名称。

The resulting piece of code will be ultra simple. 产生的代码片段将非常简单。 And here we are really deleting rows and columns. 在这里,我们实际上是删除行和列。 Please see: 请参见:

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

int main()
{
    // Lambda for printing the matric to std::cout
    auto print = [](std::vector<std::vector<int>> & m) {
        std::for_each(m.begin(), m.end(), [](std::vector<int> & vi) { std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; });
    };
    // You can add whatever values. 
    std::vector<std::vector<int>> matrix {
        {11, 12, 13, 14},
        {15, 16, 17, 18},
        {19, 20, 21, 22},
        {23, 24, 25, 26}
    };
    // Show initial data
    std::cout << "\n\nInitial matrix:\n";
    print(matrix);

    constexpr size_t RowToDelete = 1; // Row 2
    constexpr size_t ColToDelete = 2; // Column3

    // Erase row
    matrix.erase(matrix.begin() + RowToDelete);
    // Erase column in each row
    std::for_each(matrix.begin(),matrix.end(), [ColToDelete](std::vector<int> & vi) { vi.erase(vi.begin() + ColToDelete); });

    // Show result
    std::cout << "\n\nResulting matrix with deleted row " << RowToDelete+1 << " and deleted column " << ColToDelete+1 << '\n';
    print(matrix);

    return 0;
}

Hope this helps . 希望这可以帮助 。 . .

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

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