简体   繁体   English

如何在 C++ 中释放数组的最后一个元素?

[英]How can I deallocate the last element of an array in C++?

I have a dynamically allocated array like this: a[1,2,3,*unallocated value*] which has 4 elements.我有一个像这样动态分配的数组: a[1,2,3,*unallocated value*] ,它有 4 个元素。 I also have a length variable from which I know how long it is.我还有一个length变量,我知道它有多长。

I want to remove the last element in this array and set it to unallocated or uninitialize it somehow.我想删除此数组中的最后一个元素并将其设置为未分配或以某种方式取消初始化。

I think something like this might work a[lenght-1]=*something-something* but I don't know what to put on the end.我认为这样的事情可能会起作用a[lenght-1]=*something-something*但我不知道最后放什么。 Is there a way I can unallocate a single element?有没有办法可以取消分配单个元素? Or do I have to make a new array and copy all the elements into it except the last one?或者我是否必须创建一个新数组并将除最后一个元素之外的所有元素复制到其中?

Thanks in advance.提前致谢。

The *something-something* you are looking for is called sentinel .您正在寻找的*something-something*称为sentinel

For example, if your array could only contain positive integers, you could use the value 0 to indicate an "empty" slot.例如,如果您的数组只能包含正整数,您可以使用值0来表示“空”槽。 Or -1 .-1

However, if ANY integer values are allowed, this won't work.但是,如果允许任何整数值,这将不起作用。

On the other hand - why do you need to place any value at all in that slot?另一方面 - 为什么您需要在该插槽中放置任何值? You can simply rely on your length variable to figure if that element is valid or not.您可以简单地依靠您的length变量来确定该元素是否有效。

To answer your question:回答你的问题:

How can I deallocate the last element of an array in C++?如何在 C++ 中释放数组的最后一个元素?

  1. Allocate a new array that is one less in capacity.分配一个容量减少一个的新阵列。
  2. Copy all items, except for last, to the newly created array.将所有项目(最后一个除外)复制到新创建的数组中。
  3. Delete the original array.删除原始数组。

Example:例子:

int *p_original_array = new int[5];
int *p_shorter_array  = new int[4];
//...
std::copy(p_original_array, p_original_array + 4, p_shorter_array);
delete [] p_original_array;

first you can always set something to a NULL pointer.首先,您始终可以将某些内容设置为 NULL 指针。 But i think for your use case a vector is what you want to use.但我认为对于您的用例,您想要使用向量 Vector uses array as data structure but you can add/remove elements at the end. Vector 使用数组作为数据结构,但您可以在末尾添加/删除元素。 (if vector fill's the array it will create a bigger array and copy everything so no need to worry about length) (如果向量填充是数组,它将创建一个更大的数组并复制所有内容,因此无需担心长度)

It's not clear if you want to remove elements by end only or if it can happen at any position.目前尚不清楚您是否只想通过 end 删除元素,或者是否可以在任何位置发生。

In the first case, you could use the length variable to store the "used length" and another variable, maybe named "capacity", to store the actual capacity of your array.在第一种情况下,您可以使用 length 变量来存储“已用长度”和另一个变量(可能名为“容量”)来存储数组的实际容量。 The first would never be greater than the second but it could be smaller.第一个永远不会大于第二个,但它可以更小。 This is a very simple solution : if you put the size to 3, the index 3 is not usable anymore and you have nothing more to do.这是一个非常简单的解决方案:如果将大小设置为 3,则索引 3 不再可用,并且您无事可做。

In the second case, I would suggest the sentinel strategy that is described in the answer of @Vlad Feinstein.在第二种情况下,我会建议@Vlad Feinstein 的回答中描述的哨兵策略。

Since you have a length indicating the number of valid elements in the array, there is no need to copy values to a new array.由于您有一个指示数组中有效元素数量的length ,因此无需将值复制到新数组中。 Simply decrement the length .只需减少length Depending on the data type used in the array, it may or may not make sense to reset the last element's value.根据数组中使用的数据类型,重置最后一个元素的值可能有意义也可能没有意义。

For instance, it doesn't really make sense to reset an integer at all, but you can if you want to, eg:例如,重置一个整数根本没有意义,但如果你愿意,你可以,例如:

a[--lenght] = 0;

But, it does make sense to reset a std::string to free up any dynamic memory it may be using internally, eg:但是,重置std::string以释放它可能在内部使用的任何动态内存确实有意义,例如:

a[--lenght] = "";
a[--lenght] = string();
--lenght;
a[lenght].clear();
a[length].shrink_to_fit(); 

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

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