C++如何实现删除矩阵中多行多列



```c++

faulttestM = { {1,1,1,0,0,1,0,1,0,0},
              {0,0,0,1,0,0,1,1,0,1},
              {0,0,1,0,1,1,1,1,1,1},
              {0,0,0,1,0,0,1,1,0,1},
              {1,1,1,0,0,1,0,1,0,0} };

如上矩阵,需要删除相同的行和列,请问该怎么写代码,我自己写的只能删除一行T T


    vector <int> reRow;
    vector <int> reCol;
    int b = 0;
    //查找相同的行,放入reRow数组中
    for (int i = 0; i < row; i++)
    {

        for (int count1 = i + 1; count1 < row; count1++)
        {
            int a = 0;
            for (int j = 0; j < rol; j++)
            {
                if (faulttestM[i][j] == faulttestM[count1][j])
                {
                    a++;
                    continue;
                }
                else if (faulttestM[i][j] != faulttestM[count1][j])
                {
                    break;
                }
            }
            if (a == rol)
            {
                b++;
                if (count(reRow.begin(), reRow.end(), count1))
                {
                    break;
                }
                else
                {
                    reRow.push_back(count1);
                }

            }
        }
    }
    cout << "检查re中是否存在数值:" << reRow[0] << endl;
    cout << "check the size of reRow:" << reRow.size() << endl;
    //查找相同的列,放入reCol数组中
    int c = 0;
    for (int j = 0; j < rol; j++)
    {

        for (int count2 = j + 1; count2 < rol; count2++)
        {
            int a = 0;
            for (int i = 0; i < row; i++)
            {
                if (faulttestM[i][j] == faulttestM[i][count2])
                {
                    a++;
                    continue;
                }
                else if (faulttestM[i][j] != faulttestM[i][count2])
                {
                    break;
                }
            }
            if (a == row)
            {
                c++;
                if (count(reCol.begin(), reCol.end(), count2))
                {
                    break;
                }
                else
                {
                    reCol.push_back(count2);
                }
            }
        }
    }
    cout << "check the size of reCol:" << reCol.size() << endl;

    int optrow = row - reRow.size();
    int optrol = rol - reCol.size();

    cout << "row : " << row << endl;
    cout << "rol : " << rol << endl;
    cout << "optrow : " << optrow << endl;
    cout << "optrol : " << optrol << endl;



    int** optftmatrix;
    //矩阵初始化
    optftmatrix = new int* [row];
    for (int i = 0; i < row; i++)
        optftmatrix[i] = new int[rol];

    for (int i = 0; i < row; i++)
    {

        for (int j = 0; j < rol; j++)
        {
            optftmatrix[i][j] = 0;
        }

    }//初始化结束

    for (int k = 0; k < reRow.size(); k++)
    {
        cout << "reRow里的值:" << reRow[k] << endl;
    }

    for (int i = 0; i < reCol.size(); i++)
    {
        cout << "reCol里的值:" << reCol[i] << endl;
    }
    cout << "检查矩阵中的值:"<<faulttestM[0][5]<<" "<<faulttestM[1][3] << endl;

    int r0 = 0, c0 = 0;
    for (int i = 0; i < row; i++)
    {
        for (int k = 0; k < reRow.size(); k++)
        {
            if (i == reRow[k])
            {
                r0 = 1;
            }
            else
            {
                r0 = 0;
            }
            int delColCount = -1;
            for (int j = 0; j < rol; j++)
            {

                for (int p = 0; p < reCol.size(); p++)
                {

                    if (j < reCol[p])
                    {
                        c0 = 0;

                    }
                    else
                    {

                        c0 = 1;

                        if (j + 1 == rol)
                        {
                            continue;
                        }
                    }
                    /*for (int k = 1; k < reCol.size(); k++)
                    {
                        if (reCol[k - 1] < j < reCol[k])
                        {
                            delColCount += 1;
                        }
                        else if (reCol[k] < j < reCol[k + 1])
                        {
                            delColCount += 2;
                        }*/

                    //}
                    optftmatrix[i][j] = faulttestM[i + r0][j + c0];
                }
            }

        }
    }

```

给个思路:首先保证reRow和reCol正确无误
然后有伪码如下:

长=0
for i in 原数组长:
    if(i in reRow):
        continue
    宽=0
    for j in 原数组宽:
        if(j in reCol):
            continue
        新数组[长][宽++]=旧数组[i][j]
    长++
        

用链表实现可能会更好一些。你这种的话,删除无非就是元素移动,也不可能是真正的删除,要么就是重新分配内存,把不删除的元素复制到新的里面