- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
using namespace std;
class Derived
{
public:
int i;
Derived() {cout<<"Constructed Derived"<<endl;}
Derived(int ii):i(ii) {cout<<"Constructed Derived"<<i<<endl;}
~Derived() {cout<<"* Destructed Derived"<<i<<endl;}
};
int main()
{
boost::ptr_vector<Derived> pv;
for(int i=0;i<10;++i) pv.push_back(new Derived(i));
boost::ptr_vector<Derived>::iterator it;
for (it=pv.begin(); it<pv.end();/*no iterator increment*/ )
pv.erase(it);
cout<<"Done erasing..."<<endl;
}
请注意,第二个 for 循环不会递增迭代器,但它会迭代并删除所有元素。我的问题是:
- Is my technique of iteration and using the iterator correct?
- If iterator increment is not required in the for loop, then where does the increment happen?
- Is it better to use an iterator or will an ordinary integer suffice (ie: is there any value-add with using iterators)? (coz I can also erase the 5th element like pv.erase(pv.begin()+5);)
- Is there any way to assign a new object to a specific position (let's say the 5th position) of ptr_vector, directly? I'm looking for something like pv[5]=new Derived(5);. Any way of doing that?
最佳答案
ptr_vector::iterator
递增就像一个普通的随机访问迭代器。在您的示例中,您可以在不实际递增的情况下删除每个元素,因为在删除一个元素之后,它之后的每个元素都会在数组中移动。因此,当您删除第 0 个元素时,您的迭代器现在指向 曾经 作为第 1 个元素但现在是第 0 个元素的元素,依此类推。换句话说,迭代器停留在原地,而整个 vector 向左移动。
这与 ptr_vector 没有任何关系。请注意,使用普通 std::vector
会发生相同的行为。
另请注意,在删除它指向的元素后使用迭代器是危险的。在您的情况下它可以工作,但最好采用 ptr_vector::erase
的返回值,这样您就可以获得一个保证有效的新迭代器。
for (it = pv.begin(); it != pv.end(); )
it = pv.erase(it);
关于您的其他问题:
如果你只想删除一个特定的元素,那么你当然应该直接使用pv.erase(pv.begin() + N)
删除它。要为指针 vector 中的特定元素分配新值,只需说 pv[N] = Derived(whatever)
。重新分配值时不需要使用 new
。指针 vector 将在您将新值赋给的索引处调用对象的赋值运算符。
关于c++ - ptr_vector 迭代器不需要增量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4003292/
我正在尝试使用 ptr_vector 来存储一些指针,但是我的 main 方法一启动就出现错误。这是我的代码: int main(void) { boost::ptr_vector temp;
我目前正在使用 C++ 使用 OpenGL 进行较低级别的编码。我来自沉重的 objc 背景,所以我对内存管理有一些了解,但我似乎无法理解“boost”库如何管理 ptr_vector 等容器类型。
#include #include using namespace std; class Derived { public: int i; Derived() {c
是否有可能插入和/或删除元素会使现有元素的迭代器无效。 谢谢。 最佳答案 是的。 documentation对于 boost::ptr_vector状态: A ptr_vector is a poin
我正在尝试使用 boost 的指针容器,并对其应用 STL 算法。我写了一段代码来排序 ptr_vector其中Point是一个有成员的类int x, y .代码如下: #include #incl
我正在开发游戏引擎,在之前的一个问题中有人建议我开始使用 boost::ptr_vector 来维护指针列表。 基本思路是有几个State的,每个State有一个SceneGraph。每个状态都有几个
我正在尝试在当前项目中使用 boost::ptr_vector,想知道是否可以在 push_back() 方法中使用参数化构造函数,或者我是否需要使用默认构造函数? 作为次要问题,如果我定义它,它是否
我想随机分配给一个 boost::ptr_vector。使用普通的 STL vector 我会这样做: class A{ }; A* a1 = new A(); A* a2 = new A(); ve
所以现在我有以下内容: boost::ptr_vector cvect; ifstream cDbase("datafiles/customers.txt");
我找不到成员函数 boost::ptr_vector 有什么...... 我能找到的是http://www.boost.org/doc/libs/1_54_0/libs/ptr_container/d
我有一个 boost::ptr_vector,我想按排序顺序维护它。 我正在创建一堆包含几个简单字符串和值的 Customer 对象,然后将它们存储在 boost::ptr_vector 中,并且需要
我想做这样的事情: typedef X* X_Pointer; boost::ptr_vector myvec; X_Pointer x = new X(); myvec.push_back(x);
设置 class Base { public: Base(); virtual ~Base(); int getType(); protected: int type;
试图找到使用 ptr_vector 存储、访问和释放对象的最佳方法,尤其是当存储的对象是从其他对象继承时(ptr_vector 不应该有对象切片的任何问题)。但是当运行下面的程序时,令人惊讶的是派生类
我正在遍历树节点的子节点。子级存储在 ptr_vector 中,在整个迭代过程中的某个时刻我陷入无限递归,但我不明白为什么。 这里是无限递归发生的方法(这个方法只是用来打印树结构到cout): std
我在程序中有以下类(class)。 class Class1 { public: boost::ptr_vector fields; } class Class2 {
ptr_vector 是可复制构造和可复制赋值的。当不知道底层对象的具体类型时,它如何深度复制底层对象? 最佳答案 boost::ptr_vector 容器有一个可选的模板参数,CloneAlloca
我在 std::vector 上使用了 boost::ptr_vector,因为它将为我处理所有指针的删除。但是当我这样做时: ptr_vector functors; functors.resize
拜托,有人能告诉我如何从同一个 ptr_vector 中切换两个元素的位置吗?新的动态内存分配。也就是说,我想使用 std::vector 执行与下面相同的操作,但使用 boost::ptr_vect
如何在不删除的情况下从 boost::ptr_vector 中排除指针? =) 最佳答案 ptr_vector v; v.push_back(new A); A *temp=v.release(v.b
我是一名优秀的程序员,十分优秀!