如何在c++中从结构数组中删除元素
How to delete an element from a struct array in c++
假设我有一个这样的结构:
struct customer{
int c1;
int c2;
int c3;
};
customer phone[10];
phone[0].c1 = 1;
phone[0].c2 = 1;
phone[0].c3 = 1;
phone[1].c1 = 2;
phone[1].c2 = 2;
phone[1].c3 = 2;
所以我的问题是如何从struct
数组中移除phone[1]
?
提前谢谢。
你能做的最好的事情就是覆盖它。
这是一个内置数组。所以其他元素没有初始化。同样,一旦你向其中一个元素写入了一些东西,它就会一直呆在那里,直到数组超出范围,或者你在那里写入了其他东西。
最好对std::vector
:也这样做
#include <iostream>
#include <vector>
int main()
{
struct customer {
int c1;
int c2;
int c3;
};
customer phone[10];
phone[0].c1 = 1;
phone[0].c2 = 1;
phone[0].c3 = 1;
phone[1].c1 = 2;
phone[1].c2 = 2;
phone[1].c3 = 2;
std::vector<customer> phonev{{1,1,1},{2,2,2}};
phonev.erase(phonev.begin()+1);
return 0;
}
然后可以擦除元素。
相关文章:
- 将数组的地址分配给变量并删除
- 你能帮我解决删除数组 c++ 中重复值的挑战吗?
- 如何在C++中删除静态数组?
- 删除指针数组 (C++) 中的元素
- 删除动态数组时未定义标识符
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- C++数组删除的例外
- C++:指向对象的指针数组 - 删除元素时程序崩溃>
- 你如何打印出使用数组删除某些字符的回文
- 类数组 - 删除操作
- 何时从数组删除类指针
- 从自定义数据类型向量数组C 删除
- 动态数组删除
- 动态数组C++(删除)
- 动态数组删除旧指针并将其设置为新数组C++
- C++ 字符数组删除重复字符
- 使用数组删除记录
- 数组删除导致EXC_BAD_ACCESS错误
- 为什么常规删除运算符在需要数组删除运算符时起作用
- 智能指针数组删除器