如何消除二维向量中的格言c ++

时间:2017-02-06 22:14:11

标签: c++

嗨我需要从2d向量中消除x这是一个例子,C ++

1 2 3
4 5 6
7 8 9

 x=9

COUT

1 2
4 5

这是我试过的,每当我放

时它都不起作用
1 1 2
3 4 5
6 7 8

它停止了,我找不到问题

#include<iostream.h>
int main()
{
  int v[100][100],i,j,x,m,n;
  int nv[100][100];  
  cout<<"linii:";
  cin>>n;
  cout<<endl;
  cout<<"coloane:";
  cin>>m;

  cout<<endl;
  cout<<"Introduceti matricea"<<endl;
  for(i=1;i<=n;i++){
      for(j=1;j<=m;j++){
          cin>>v[i][j];
        }
    }
   cout<<endl;
   cout<<"Matricea este"<<endl;
   for(i=1;i<=n;i++){
      for(j=1;j<=m;j++){
          cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
    int k=0,l=0,q=0,t=0,p;
    cout<<"Introduceti X:";
    cin>>x;
   while(t == 0){
       p=0;
     for(i=1;i<=n;i++){
      for(j=1;j<=m;j++){
           if(v[i][j] == x){
                k=i;
                l=j;
                p++;
                q++;
                if(v[i][j]==v[i+1][j])
                {
                    k++;
                }
                    if(v[i][j]==v[i][j+1])
                    {
                        l++;
                    }

             }
        }
     }

    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(i==k || j==l){}
            else if(i<k && j<l){
                nv[i][j]=v[i][j];
                }
            else if(i<k && j>l){
                nv[i][j-1]=v[i][j];
                }
            else if(i>k && j<l){
                nv[i-1][j]=v[i][j];
                }
            else {nv[i-1][j-1]=v[i][j];}
        }

    }
     for(i=1;i<=n-q;i++){
        for(j=1;j<=m-q;j++){
            v[i][j]=nv[i][j];

        }
    }
    if(p==1){
        t=1;
    }
}

    cout<<"matricea finala"<<endl;
    for(i=1;i<=n-q;i++){
        for(j=1;j<=m-q;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }

}

1 个答案:

答案 0 :(得分:0)

答案是

int main()
{
    int n, m, maxI = 0, maxJ = 0;
    cout << "linii:";
    cin >> n;
    vector<vector<int>> numbers(n);
    cout << "coloane:";
    cin >> m;
    cout << "Introduceti matricea" << endl;
    for (int i = 0; i < n;i++) {
        numbers[i].resize(m);
        for (int j = 0; j < m; j++)
        {
            cin >> numbers[i][j];
            if (numbers[i][j] > numbers[maxI][maxJ])
            {
                maxI = i;
                maxJ = j;
            }
        }
    }

    numbers.erase(maxI + numbers.begin());
    cout << "matricea finala" << endl;
    for (auto& itr : numbers) {
        itr.erase(maxJ + itr.begin());
        for (const auto& itr2 : itr)
            cout << itr2 << " ";
        cout << endl;
    }
    return 0;
}

我使用 std :: vector 来删除max元素。