C ++从向量中删除对象 [英] C++ Remove object from vector

查看:35
本文介绍了C ++从向量中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从向量中删除一个元素.例如:

I would like to remove an element from a vector. For example:

// object that is in the vector: MyClass obj;
// vector looks as so: vector<MyClass*> pVector;

pVector.remove(obj);

推荐答案

这将完全基于指针删除对象.理想情况下,您应该为您的 MyClass 对象提供比较功能,以实际检查对象以查看它们是否相同.

This will remove the object based purely on the pointer. Ideally, you would have comparison functions for your MyClass objects that actually check the objects to see if they are the same.

pVector.erase(std::remove(pVector.begin(), pVector.end(), obj), pVector.end());

这篇关于C ++从向量中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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