C整数数组提取 [英] C Integer Array Extraction

查看:94
本文介绍了C整数数组提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数数组。我们假设int a [5] = {1,2,1,2,2} ;.我想通过提取数组的一些元素a [5]将这个数组分成两个数组进入一些不同的数组,如b [2]和c [3],这样int b [2]将包含{1,2},int c [3]将包含{1,2,2} 。如何才能使用C编程?

解决方案

您需要的是一个动态分配/大小的数组,该语言本身不支持但您可以创建一个为自己。由于您是初学者,我建议您编写一个不根据需要重新分配其存储的数组。通过省略内存管理,您可以简化任务。当你更擅长C语言时,可以在以后使用内存管理。在没有内存管理的情况下,动态大小的数组将是固定大小数组和一个整数的组合,该数组指示数组中有多少项有效(当然在数组的开头)。其余项目未使用。在没有内存管理的情况下,此阵列对最大大小有限制,即使列表为空,它也始终占用最大大小。不过,作为第一次尝试,它更容易实现。

  #define MAX_ARRAY_LEN 20 

typedef struct MyArray
{
size_t 长度;
int items [MAX_ARRAY_LEN];
} MyArray;


void MyArray_Init(MyArray * arr)
{
arr-> length = 0 ;
#if _DEBUG
memset(arr-> items, 0 ,< span class =code-keyword> sizeof (arr-> items));
#endif
}

void MyArray_Add( MyArray * arr, int item)
{
assert(arr-> length< MAX_ARRAY_LEN);
arr-> items [arr-> length] = item;
++ arr->长度;
}

void MyArray_Remove(MyArray * arr, size_t index )
{
断言(索引< arr->长度);
memmove(& arr-> items [index],& arr-> items [index + 1],(arr-> length-index- 1 )* sizeof int ));
--arr->长度;
}

// 您可以使用其他方法...
// 喜欢:int MyArray_Contains(MyArray * arr,int item);


根据您的实际需要,快速而肮脏的解决方案甚至可以是

  int  * b =& a [ 0 ]; 
int * c =& a [ 2 ];



当然你必须跟踪(至少含蓄地) b c 大小


I have an array of integers.Let's assume int a[5]={1,2,1,2,2};.I want to divide this array into two arrays by extracting some elements of array "a[5]" into some different arrays like "b[2]" and "c[3]",such that int b[2] will contain {1,2} and int c[3] will contain {1,2,2}. How can it be possible using C programming?

解决方案

What you need is a dynamically allocated/sized array that isn't natively supported by the language but you can create one for yourself. Since you are a beginner I would recommend you to write an array that doesn't reallocate its storage as needed. By leaving out memory management you make your task simpler. Its ok to bother with memory management later when you are better at the C language. Without memory management your dynamically sized array will be a combination of a fixed size array and an integer that indicates how many items are valid in the array (of course at the beginning of the array). The rest of the items are unused. Without memory management this array has a limit on the maximum size and it occupies that maximum size all the time even if the list is empty. Still, as a first try it is simpler to implement.

#define MAX_ARRAY_LEN 20

typedef struct MyArray
{
    size_t length;
    int items[MAX_ARRAY_LEN];
} MyArray;


void MyArray_Init(MyArray* arr)
{
    arr->length = 0;
#if _DEBUG
    memset(arr->items, 0, sizeof(arr->items));
#endif
}

void MyArray_Add(MyArray* arr, int item)
{
    assert(arr->length < MAX_ARRAY_LEN);
    arr->items[arr->length] = item;
    ++arr->length;
}

void MyArray_Remove(MyArray* arr, size_t index)
{
    assert(index < arr->length);
    memmove(&arr->items[index], &arr->items[index+1], (arr->length-index-1)*sizeof(int));
    --arr->length;
}

// You additional methods come here...
// Like: int MyArray_Contains(MyArray* arr, int item);


Depending on your actual needs, the quick and dirty solution could even be

int * b = &a[0];
int * c = &a[2];


Of course you have to keep track (at least implicitly) of b and c sizes.


这篇关于C整数数组提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆