繁体   English   中英

从数组中删除所有出现的值

[英]remove all the occurrences of value from the array

    #include <stdlib.h>
    
    int main(int argc, char** argv) {
        int value;
        int array[5] = {1, 1, 1, 2, 3};
        int size = 5;
        scanf("%d", &value);
        for (int i = 0; i < size; i++) {
            if (value == array[i]) {
                for (int j = i; j < size; j++) {
                    array[j] = array[j + 1];
                    size--;
                }
            }
        }
        for (int i = 0; i < size; i++) {
            printf("%d ", array[i]);
        }
    }

示例我有数组: 1, 1, 1, 2, 3 和用户输入value = 1 ,我的代码必须删除数组中的所有数字value = 1 但是当我的代码运行这种情况时, output 是: 1, 2应该是: 2, 3

当您使用简单的“copy if”算法执行此代码时,实际上并没有太多内容:

#include <stdio.h>
#include <stdlib.h>

void removeAllOccurrences(int value, int *array, int *size) {
  int* end = &array[*size];
  int* copy = array;

  while (array < end) {
    if (*array != value && copy != array) {
      *copy = *array;
      --*size;
      ++copy;
    }

    ++array;
  }
}

请注意,我已将value切换为参数,因为这样可以更轻松地设置测试用例并避免必须弄清楚get_int()的作用:

int main(int argc, char **argv) {
  int array[] = { 1, 2, 1, 2, 3, 1, 4, 1 };
  int len = sizeof(array) / sizeof(int);

  removeAllOccurrences(1, array, &len);

  for (int i = 0; i < len; ++i) {
    printf("%d ", array[i]);
  }

  printf("\n");

  return 0;
}
void Remove_val(int array[], int* sz, int val)
{
    int* add = array;
    int newsize = 0;

    for (int index = 0; index < *sz; index++)
    {
        if (array[index] != val)
        {
            *add++ = array[index];
            newsize++;
        }
    }

    *sz = newsize;
}

内循环内部

    for (int i = 0; i < size; i++) {
        if (value == array[i]) {
            for (int j = i; j < size; j++) {
                array[j] = array[j + 1];
                size--;
            }
        }
    }

仅“删除”了一个等于 value 的元素。 它的 position 被下一个也可以等于 value 的元素占用。 但在外循环中,索引i正在递增。 因此,不会删除前i值的 position 处的下一个元素。

此外,由于此语句,尝试访问阵列之外的 memory

array[j] = array[j + 1];
                 ^^^^^

j等于size- 1

当找到数组中删除的值时,将所有元素向左移动也是低效的。

您可以编写一个单独的 function 来完成该任务。 function 可以在删除指定值后返回数组中实际元素的数量。

这是一个演示程序。

#include <stdio.h>

size_t remove_if_equal( int a[], size_t n, int value )
{
    size_t i = 0;
    
    for ( size_t j = 0; j < n; j++ )
    {
        if ( !( a[j] == value ) )
        {
            if ( i != j ) a[i] = a[j];
            i++;
        }
    }
    
    return i;
}

int main(void) 
{
    int a[] = { 1, 1, 1, 2, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    size_t n = remove_if_equal( a, N, 1 );
    
    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
    
    return 0;
}

程序 output 是

1 1 1 2 3 
2 3 
    #include <stdlib.h>
    
    int main(int argc, char** argv) {
        int value;
        int array[5] = {1, 1, 1, 2, 3};
        int size = 5;
        scanf("%d", &value);
        for (int i = 0; i < size; i++) {
            if (value == array[i]) {
                for (int j = i; j < size-1; j++) {
                    array[j] = array[j + 1];                    
                }
                  size--;
                  i--;
            }
        }
        for (int i = 0; i < size; i++) {
            printf("%d ", array[i]);
        }
    }

我解决了。 该代码的output为: 2, 3

暂无
暂无

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

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