在 C++ 中从数组中删除对象
Remove object from an array in C++?
这是我的代码的简化示例:.h
class Company {
public:
Company();
void addEmployee(const Employee &emp);
void removeEmployee();
private:
Employees *listEmployees;
};
.cpp
Company::Company(){ listEmployees = new Employees[16]; }
Company::addEmployee(const Employee &emp) {listEmployee[index]=emp;}
Company::removeEmployee(){ ??? }
我想删除存储在我的阵列中的员工。我尝试使用:
delete listEmployee[index]; //->"cannot delete expression of type 'Employees'
listEmployee[index]=NULL; //->"no viable overloaded '='"
我在网络上没有找到令人满意的解决方案。另外,我对指针和引用不是很熟悉,也许错误由此而来。谢谢。
编辑:我不允许使用向量,我必须使用数组。
编辑2:感谢您的回答。这是我使用的解决方案:
for(int i=indexEmp;i<sizeArray-1;i++){
listEmployees[i]=listEmployees[i+1];
}
其中 indexEmp 是我要删除的员工的索引。
方法是使用相同长度Employess
的布尔数组来跟踪特定位置的对象是否有效。
在数组中设置相应的值,删除元素时,重置相应的值。
当您访问您的员工时,您还应该检查它是否有效......
这比使用std::vector
要多得多,这应该是您的首选。
没有内置的方法可以从C++数组中删除元素。数组始终具有固定大小,因此无法添加或删除元素。
您确实还有其他一些选择。例如,您可以使用 std::vector
类型,它的作用类似于数组,但允许您添加和删除元素。或者,如果您只需要按某种顺序存储元素并且不关心顺序是什么,请尝试使用 std::unordered_map
或 std::map
。
希望这有帮助!
delete listEmployee[index]; //->"cannot delete expression of type 'Employees'
这不起作用,因为您只能通过指针删除,但listEmployee[index]
不是指针(它是对实例的直接访问(。
此外,新删除必须平衡。您只能delete
通过new
分配的内容。并且只delete[]
new[]
分配的内容.因此,由于您的listEmployees
来自new[]
分配,因此唯一有效的释放操作是对listEmployees
本身的delete[]
。
换句话说,您不能从数组中间删除元素。一次只能释放整个阵列。
正如其他人所建议的那样,vector
等非常适合代替原始数组使用。但听起来这可能是家庭作业问题或其他一些排除简单选项的情况。
在这种情况下,有两种可能性:
1( 分配第二个Employees
大小比原始数组小一个数组。然后按顺序复制所有应该保留的那些,并排除要删除的那个。然后解除分配原始数组并将新数组分配给listEmployees
。
2( 从要删除的条目开始,获取数组中的下一个条目并将其分配给该条目。前进一步,再次做同样的事情。继续这样做,直到阵列结束。目的是将所有其他移动一个,覆盖要删除的那个。数组的最后一个元素只是闲置。如果它有任何一种方法可以清除其值,则可以使用它。据推测,您还对正在更新的数组中的元素数量进行了某种计数,因此没有任何东西会尝试访问最后的死条目。因此,有效地删除了中间的条目,但不需要另一个数组分配。
您可以只为要删除的元素分配一个无效值,然后具有此无效或默认值的元素可以被视为未初始化或有效以被覆盖。
例如,删除函数可以是:
void removeEmployee(int index) {
Employees default_val = NIL_emp;
listEmployees[index] = defaul_val;
}
只是为了更多帮助:
看看"在 C++ 中思考第 1 卷第 588 页"中的这个旧示例,使用指向对象的指针从头开始制作一种动态且可调整大小的数组非常有用,例如;
new Employee.
注意:您需要删除#include require.h
并使用if
添加类似的代码。 此外,您还需要查看书中的标题"PStash"。看看这个主题,特别是remove()
和inflate()
函数实现来回答你的问题非常有用。
此示例使用void *
指针,当您获得指针时,您可以投射回您的对象,例如Embloyee
如果你不喜欢使用void,你可以在templated class
中制作它并避免施法的东西
//: C13:PStash.cpp {O}
// Pointer Stash definitions
#include "PStash.h"
#include "../require.h"
#include <iostream>
#include <cstring> // 'mem' functions
using namespace std;
int PStash::add(void* element) {
const int inflateSize = 10;
if(next >= quantity)
inflate(inflateSize);
storage[next++] = element;
return(next - 1); // Index number
}
// No ownership:
PStash::~PStash() {
for(int i = 0; i < next; i++)
require(storage[i] == 0,
"PStash not cleaned up");
delete []storage;
}
// Operator overloading replacement for fetch
void* PStash::operator[](int index) const {
require(index >= 0,
"PStash::operator[] index negative");
if(index >= next)
return 0; // To indicate the end
// Produce pointer to desired element:
return storage[index];
}
void* PStash::remove(int index) {
void* v = operator[](index);
// "Remove" the pointer:
if(v != 0) storage[index] = 0;
return v;
}
void PStash::inflate(int increase) {
const int psz = sizeof(void*);
void** st = new void*[quantity + increase];
memset(st, 0, (quantity + increase) * psz);
memcpy(st, storage, quantity * psz);
quantity += increase;
delete []storage; // Old storage
storage = st; // Point to new memory
} ///:~
我已经完成了它的另一个版本,我觉得最好通过该程序与您分享,我们可以使用以下C++删除和移动数组中的元素位置:
#include<iostream>
using namespace std;
int a[5];
int found = 0;
//to get data
int getdata(){
cout<<"nEnter data in 5 arrays: ";
for(int i = 0; i<5; i++)
{
cin>>a[i];
}
}
//to show data
void showdata(){
for(int i = 0; i<5; i++)
{
cout<<"na["<<i<<"] = "<<a[i];
}
}
//for removing and replacing index's
void rerange()
{
int elloc;
cout<<"nWhich element do you want to remove please enter index: ";
cin>>elloc;
for(int i = 0; i<5; i++)
if(a[elloc] == a[i])
{
for(int i = elloc; i<5; i++)
{
a[i] = a[i+1];
}
found++;
}
}
int main()
{
getdata();
showdata();
rerange();
if(found == 0)
cout<<"Element not found";
else
showdata();
return 0;
}
- 迭代时从向量和内存中删除对象
- C++从对象自己的类中删除对象
- 如何通过对象的类属性删除对象,并返回其对象值?
- 删除对象(具有不同类型)的引用时会发生什么情况?
- 从列表C++中删除对象
- c++ 循环访问对象列表并删除对象
- 从对象本身的容器中删除对象
- 从尝试引用已删除函数的矢量 C++ 中删除对象
- 如何通过指向元组的共享指针删除对象
- 防止通过接口删除对象
- 在 c++ 中从内存中删除对象
- 当对象被删除时,复制被删除对象的对象如何
- 当我使用dynamic_cast并删除对象删除时,析构函数是如何工作的?
- 从内存中删除对象
- 通过C++中的删除方法自行删除对象
- C++ - 析构函数只是释放内存还是实际删除对象
- 如何通过存储在 std::list 中的指针删除对象?
- 如何通过对象参数从指针矢量中删除对象和指针
- 我可以删除对象的右值版本的函数吗?
- 使用对象的索引从矢量中删除对象