我想要實作的是回圈遍歷陣列一次,同時洗掉我的函式為其回傳 false 的元素。但是我寫的代碼不起作用。任何想法都將不勝感激,因為我真的被卡住了,我不明白我做錯了什么
#include <stdio.h>
bool isitright(int i) {
if (i/2 == 0) {
return false;
} else {
return true;
}
}
int main() {
int arr[4] = {0, 1, 2, 3};
int i = 0;
while (i < 4) {
if(!isitright(i)) {
arr[i] = arr[i 1];
}
i ;
}
}
uj5u.com熱心網友回復:
如何從 C 中的陣列中“洗掉一個元素”。您也可以在 C 中將此稱為“過濾陣列”。
您不能在 C 中調整靜態分配的陣列的大小。相反,您只需跟蹤大小變數并相應地調整它,然后只列印您需要的內容。
1.方法一【更好的方法】:將一個陣列過濾成另一個陣列
對于n陣列中的元素:
時間復雜度:O(n)
空間復雜度:O(2n) = O(n)
但是,如果您嘗試從陣列中過濾某些值,最簡單的方法是復制到新陣列。所以,這是一個演示。在這個演示中,我從一個包含 的陣列開始[-3, -2, -1, 0, 1, 2, 3],然后我用你的小演算法過濾掉所有的-1、0和 1值,當我完成后在新陣列中結束。is_it_right()[-3, -2, 2, 3]
要在同一陣列中就地執行此操作將需要更高級的演算法,該演算法幾乎肯定會在運行速度較慢的情況下進行權衡,但不會使陣列空間增加一倍。下面的方法將運行得非常快,但我將陣列空間加倍,因為我是從一個陣列復制到另一個陣列,而不是就地過濾陣列。
不管怎樣,仔細研究一下:
在這里在 OnlineGDB 上在線運行它。
或者在我的eRCaGuy_hello_world 存盤庫中下載下面兩種方法的完整、可運行示例:array_filter_and_remove_element.c
#include <stdbool.h>
#include <stdio.h>
/// Get the number of elements in an array
#define ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))
/// Return false for -1, 0, or 1, and return true otherwise.
bool is_it_right(int i)
{
if (i/2 == 0)
{
return false;
}
return true;
}
void print_array(int arr[], size_t len)
{
printf("[");
for (size_t i = 0; i < len; i )
{
printf("%i", arr[i]);
if (i < len - 1)
{
printf(", ");
}
}
printf("]\n");
}
int main()
{
int arr[] = {-3, -2, -1, 0, 1, 2, 3};
int arr_filtered[ARRAY_LEN(arr)];
size_t arr_filtered_len;
// Remove values -1, 0, and 1 from the array
int j = 0;
for (size_t i = 0; i < ARRAY_LEN(arr); i )
{
if (is_it_right(arr[i]))
{
arr_filtered[j] = arr[i];
j ;
}
}
arr_filtered_len = j;
print_array(arr, ARRAY_LEN(arr));
print_array(arr_filtered, arr_filtered_len);
return 0;
}
輸出:
[-3, -2, -1, 0, 1, 2, 3] [-3, -2, 2, 3]
2.方法2:[方式更復雜,處理器密集型和復制密集型]就地過濾陣列
對于n陣列中的元素:
時間復雜度:最佳情況(無需過濾):O(n);最壞情況(必須一次過濾掉所有元素):O(n^2)
空間復雜度:O(n)
這種方法更復雜,更需要處理器,也更需要復制。我推薦上面的方法。
printf("\n====================================\n"
"APPROACH 2: FILTER AN ARRAY IN-PLACE\n"
"====================================\n");
// Remove values -1, 0, and 1 from the array **in place**
size_t arr_len = ARRAY_LEN(arr);
printf("Starting array:\n");
print_array(arr, arr_len);
size_t i = 0;
while (i < arr_len)
{
if (is_it_right(arr[i]) == false)
{
// We don't want to keep this value at `arr[i]`, so overwrite it
// by shifting all values from `i 1` to the end of the array to
// the left by 1 place.
printf("Removing value `%i` from the array\n", arr[i]);
size_t i_write = i;
for (size_t i_read = i_write 1; i_read < arr_len; i_read )
{
arr[i_write] = arr[i_read];
i_write ;
print_array(arr, arr_len);
}
arr_len--;
printf("One element has just been removed. Here is the new array:\n");
print_array(arr, arr_len);
}
else // is_it_right(arr[i]) == true
{
// only increment `i` if we did NOT just left-shift a bunch of
// elements by one index place
i ;
}
}
printf("Here is the final, filtered-in-place array:\n");
print_array(arr, arr_len);
示例輸出:
==================================== APPROACH 2: FILTER AN ARRAY IN-PLACE ==================================== Starting array: [-3, -2, -1, 0, 1, 2, 3] Removing value `-1` from the array [-3, -2, 0, 0, 1, 2, 3] [-3, -2, 0, 1, 1, 2, 3] [-3, -2, 0, 1, 2, 2, 3] [-3, -2, 0, 1, 2, 3, 3] One element has just been removed. Here is the new array: [-3, -2, 0, 1, 2, 3] Removing value `0` from the array [-3, -2, 1, 1, 2, 3] [-3, -2, 1, 2, 2, 3] [-3, -2, 1, 2, 3, 3] One element has just been removed. Here is the new array: [-3, -2, 1, 2, 3] Removing value `1` from the array [-3, -2, 2, 2, 3] [-3, -2, 2, 3, 3] One element has just been removed. Here is the new array: [-3, -2, 2, 3] Here is the final, filtered-in-place array: [-3, -2, 2, 3]
同樣,上述兩種方法的完整、可運行示例在我的eRCaGuy_hello_world 存盤庫中:array_filter_and_remove_element.c
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361254.html
