通用程序算法和数据结构_C中的对象列表的标准或通用数据结构是什么?

通用程序算法和数据结构

In C, what’s the standard or common data structure for a list of objects?

C中 ,对象列表的标准或通用数据结构是什么?

In C, one common way to store a list/collection of objects of the same type is to use one-dimensional arrays.

C语言中 ,存储相同类型对象列表/集合的一种常用方法是使用一维数组。

In C, an array is a collection of data objects of the same type. An array is in a contiguous memory area. The lowest address of the memory area corresponds to the first element in the array and all other elements follow it one by one.

在C语言中,数组是相同类型的数据对象的集合。 阵列位于连续的内存区域中。 存储区的最低地址对应于数组中的第一个元素,所有其他元素一个接一个地跟随它。

Arrays can be statically allocated or dynamically allocated:

数组可以静态分配也可以动态分配:

// statically allocated
// declare a static array a1 of 100 ints
int a1[100];

// dynamically allocated on the heap
// allocate a dynamical array a1 of 100 ints
int a2_len = 100;
int *a2 = (int *)malloc(sizeof(int) * a2_len);

Element access time in an array has O(1) complexity. Memory management like increasing the size of an array should be done grammatically. Some library functions may help though. For example, void *realloc(void *ptr, size_t size) changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes, and returns a pointer to the newly allocated memory which may be different from ptr.

数组中元素的访问时间具有O(1)复杂度。 像增加数组大小一样的内存管理应该在语法上进行。 一些函数可能会有所帮助。 例如, void *realloc(void *ptr, size_t size)ptr指向的内存块的大小更改为size字节。 内容将会是从范围不变启动该地区提升至最低新旧和大小,并返回一个指向新分配的内存可以来自不同的ptr

One example to sum the a1 and a2 together into a2:

一个将a1和a2总计为a2的示例:

int i = 0;
for (i = 0; i < 100; ++i) {
  a2[i] += a1[i];
}
Answered by dtivl.
dtivl回答。

翻译自: https://www.systutorials.com/whats-the-standard-or-common-data-structure-for-a-list-of-objects-in-c-2/

通用程序算法和数据结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值