如何删除二维数组中的元素

how to delete an element in 2d array

本文关键字:二维数组 元素 删除 何删除      更新时间:2023-10-16

美好的一天,我试图在线搜索有关如何删除 2D 数组中元素的示例,但无济于事。 如果我的问题看起来不清楚和业余,请原谅我。

删除 2D 数组中特定元素的简单代码是什么?我正在使用静态数组,还没有了解动态和向量。

#include<iostream>
#include<string>
std::string array[3][6] = {{"a","b","c","d","e","f",},
                            {"g","h","i","j","k","l",},
                            {" "," "," "," "," "," ",}}; //i made some allowance so that i can insert/delete
void display();
using namespace std;
int main(){
    char xdelete;
    char ydelete;
    display();
    cout<<"nDelete character" <<endl;
    cout<<"enter position x: "; 
    cin>>xdelete; 
    cout<<"enter position y: ";
    cin>>ydelete;
    for(int x = xdelete; x<2; x++){
        for(int y = ydelete ;y<6;y++){
            array[x][y] = array[x][y+1]; // yup, this is wrong >.<
        }
    }
    display();
}
//display
void display(){
    cout<<endl;
    for(int z = 0; z<6; z++){
        cout<<"  "<<z;
    }
    cout<<endl;
    for(int x = 0; x<2; x++){
    cout<<x <<"|";
        for(int y = 0; y<6; y++){
            cout<<" " <<array[x][y] <<" "; 
        }
    cout<<endl;
    }
}

这是给定的:输入位置 x:0 输入位置 Y:1

 0  1  2  3  4  5
0| a  b  c  d  e  f
1| g  h  i  j  k  l

这是我想要的输出

 0  1  2  3  4  5
0| a  c  d  e  f  g
1| h  i  j  k  l  

无法从数组中删除元素。数组T[N]在其整个生存期内恰好包含N数量的 T 类型元素。

另一个类似于删除的操作是为所有连续元素的左侧分配一个索引,以便"已删除"索引中的元素将被覆盖。这将使最后一个索引处的元素成为倒数第二个索引的副本。

你不需要外部for循环,你可以使用 xdelete 来访问该行。

y循环中,您必须在 y < 5 处停止,否则您将在行外访问。循环后,您应该将以下元素替换为空白,否则您将拥有该行中最后一项的副本。

int y;
for (y = ydelete; y < 5; y++) {
    array[xdelete][y] = array[xdelete][y+1];
}
array[xdelete][y] = " ";

您应该保留数组中元素的实际数量,当对数组执行某些操作(如删除元素(时,将数组解释为一维数组会更简单。

这是一个演示程序

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
std::ostream & display( const std::string *a, size_t n, size_t cols, std::ostream &os = std::cout )
{
    for ( size_t i = 0; i < n; i++ )
    {
        os << a[i] << ( ( i + 1 ) %  cols == 0 ? 'n' : ' ' ); 
    }
    return os;
}
int main() 
{
    const size_t M = 3, N = 6;
    std::string array[M][N] = 
    {
        { "a", "b", "c", "d", "e", "f", },
        { "g", "h", "i", "j", "k", "l", },
    };
    auto it = std::find( *array, *array + M * N, std::string() );
    size_t n = std::distance( *array, it );
    display( *array, n, N ) << std::endl;
    size_t row, col;
    std::cout<<"Delete charactern";
    std::cout << "enter row: "; 
    std::cin >> row; 
    std::cout << "enter column: ";
    std::cin >> col;
    if ( N * row + col < n )
    {
        size_t pos = N * row + col;
        std::move( *array + pos + 1, *array + n, *array + pos );
        --n;
    }
    std::cout << 'n';
    display( *array, n, N ) << std::endl;
    return 0;
}

它的输出可能看起来像

a b c d e f
g h i j k l
Delete character
enter row: 0
enter column: 1
a c d e f g
h i j k l 

在这里重复一点,有额外的建议

正如其他人所说,你不能从数组中删除元素,你可以移动它们。关于你的代码:它确实会移动(只需更改名称array,并将xdeleteydelete的类型更改为int s.,但是,随着[y + 1]你最终会越界,这会导致问题。

为了更接近静态二维数组中的删除,在移动初始数组后,创建一个大小 = (originalsize - 1( 的新数组,并将所有内容从第一个复制到第二个(最后一个元素除外(;

(比如(我有一个整数数组 - A[3][4]如果您不确定如何将下一行的第一项移动到当前行最后一项的位置,那么这可能是一个可能的解决方案:

//Deleting an item from the array
int Row,Column;
cout<<endl<<"Enter the index from where you wish to delete the item: ";
cin>>Row>>Column;
int Deleted;
Deleted=A[Row][Column];
for(int i=Row; i<3;i++){
    int n=0;
    for(int j=Column; j<4; j++){
        n++;
        if(n==3)
            A[i][j]= A[i+1][(j+1)%4];
        else
            A[i][j]= A[i][(j+1)%4];
    }
}
cout<<endl<<"Item deleted"<< "="<<Deleted<<endl;
//Displaying Array after deleting the item
for(int i=0; i<3; i++) {
    for (int j = 0; j < 4; j++) {
     cout<<" "<< A[i][j];
    }
}