C ++是否可以从数组中“删除”元素? [英] C++ Is it possible to 'delete' element from an array?

查看:341
本文介绍了C ++是否可以从数组中“删除”元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个新数组,其大小比我的主数组大-1
还有一个问题,我不能声明 string newArray [oldArray.length()-1]

I want to make new array which has size - 1 than my 'main array'. And there is problem I cant declare string newArray[oldArray.length()-1].

在C ++中有可能吗?

Is it this possible in C++ ?

推荐答案

不,它无法从静态数组中删除元素。但是,您可以将后续元素向下移动一个插槽,然后忽略最后一个插槽。您可以使用一个单独的变量来跟踪数组中有多少个可用元素。

No, it is not possible to delete an element from a static array. You can, however, shift the subsequent elements down one slot and then ignore the last slot. You can use a separate variable to keep track of how many usable elements are in the array.

或者,使用 std :: vector 创建一个动态数组。

Alternatively, use std::vector to create a dynamic array.

这篇关于C ++是否可以从数组中“删除”元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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