动态增加/减少数组大小 [英] Dynamically increase/decrease array size

查看:25
本文介绍了动态增加/减少数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态增加数组的大小.是否有任何标准的 C 或 C++ 函数可以在数组的末尾附加额外的空间或删除它?我知道,这很困难,因为不能保证堆的末尾有足够的空间.但这不应该是操作系统的工作吗?

I'm trying to increase the size of an array dynamically. Is there any standard C or C++ function, which appends additional space at the end of an array or removes it? I know, this is difficult, since it cannot be assured that there is enough space at the end on the heap. But shouldn't this be the job of an operating system?

推荐答案

你要找的函数是 realloc() 在 C 中,它也在 C++ STL 中作为 std::realloc

The function you're looking for is realloc() in C, which is also present in the C++ STL as std::realloc

虽然正如你提到的 C++,你也可以选择一个标准的容器,比如 std::vector 封装了相关的内存管理.

Though as you mentioned C++, you could also go for an standard container like std::vector which encapsulate the associated memory management.

这篇关于动态增加/减少数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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