是否可以创建一个指针向量?
只是想知道,由于我遇到的问题,是否可以创建指针向量? 如果是这样,怎么办? 特别是关于使用迭代器和 .begin() ,即:我如何将此向量转换为指针向量:
class c
{
void virtual func();
};
class sc:public c
{
void func(){cout<<"using func";}
};
sc cobj;
vector<c>cvect
cvect.push_back(cobj);
vector<c>::iterator citer
for(citer=cvect.begin();citer<cvect.end();citer++)
{
citer->func();
}
Just wondering, because of a problem I am running into, is it possible to create a vector of pointers? And if so, how? Specifically concerning using iterators and .begin() with it, ie: How would I turn this vector into a vector of pointers:
class c
{
void virtual func();
};
class sc:public c
{
void func(){cout<<"using func";}
};
sc cobj;
vector<c>cvect
cvect.push_back(cobj);
vector<c>::iterator citer
for(citer=cvect.begin();citer<cvect.end();citer++)
{
citer->func();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当然。
需要记住的事情:
如果您像我在示例中那样使用动态分配的内存,则需要自行清理,
例如:
这可以通过使用
shared_ptr
向量来简化(例如 <代码> boost::shared_ptr )。 不要尝试为此使用 std::auto_ptr ,它不会工作(甚至不会编译)。另一件要记住的事情是,您应该尽可能避免使用
<
来比较循环中的迭代器,它仅适用于模拟随机访问迭代器的迭代器,这意味着您无法更改您的代码使用例如std::list
。Sure.
Things to keep in mind:
You'll need to cleanup after your self if you use dynamically allocated memory as I did in my example
e.g.:
This can be simplified by using a vector of
shared_ptr
s (likeboost::shared_ptr
). Do not attempt to usestd::auto_ptr
for this, it will not work (won't even compile).Another thing to keep in mind, you should avoid using
<
to compare iterators in your loop when possible, it will only work for iterators that model a random access iterator, which means you can't change out your code to use e.g. astd::list
.向量cvect
不是指针向量。 它是 c 类型对象的向量。 你想要向量; cvect
。 当然然后,给定一个迭代器,您想要类似的东西:
,很可能您一开始并不想要指针向量......
vector <c> cvect
is not a vector of pointers. It is a vector of objects of type c. You wantvector <c*> cvect
. and the you probably want:And then, given an iterator, you want something like:
Of course, it's quite probable you didn't want a vector of pointers in the first place...
是的,这是可能的,事实上,如果您希望向量包含来自整个类层次结构而不是单一类型的对象,则有必要使用指针。 (不使用指针将导致可怕的对象切片问题——所有对象都会默默地进行转换为基类类型。编译器不会对此进行诊断,并且几乎肯定不是您想要的。)
请注意,使用指针向量,您需要进行自己的内存管理,因此非常小心——如果您将使用本地对象(如上所述),它们一定不能在容器之前超出范围。 如果您使用指向通过
new
创建的对象的指针,则需要在销毁容器之前手动删除
它们。 在这种情况下,您绝对应该考虑使用智能指针,例如提供的
。smart_ptr
提升Yes it is possible, and in fact it is necessary to use pointers if you intend your vector to contain objects from an entire class hierarchy rather than of a single type. (Failing to use pointers will result in the dreaded problem of object slicing -- all objects are silently converted to base class type. This is not diagnosed by the compiler, and is almost certainly not what you want.)
Note that with a vector of pointers, you need to do your own memory management, so be very careful -- if you will be using local objects (as above), they must not fall out of scope before the container does. If you use pointers to objects created with
new
, you'll need todelete
them manually before the container is destroyed. You should absolutely consider using smart pointers in this case, such as thesmart_ptr
provided byBoost
.是的,当然。
请注意
vector的声明 cvect
以及 cvect.push_back(&cobj) 的使用。从提供的代码来看,您使用迭代器的方式是错误的。 要访问迭代器指向的成员,您必须使用
*citer
而不是单独使用citer
。Yes, sure.
Please note the declaration of
vector<c*> cvect
and the use ofcvect.push_back(&cobj)
.From the code provided, you are using iterator in a wrong way. To access the member an iterator is pointing to you must use
*citer
instead ofciter
alone.您已经为指针向量创建了
vector
。 然后使用new为c对象分配内存并将其推入向量。 另外,不要忘记您必须自己删除,并且 vector.clear() 不会释放为 c 对象分配的内存。 您必须将 c 作为指针向量存储在这里,否则对虚函数的调用将不起作用。You have create
vector<c*>
for a vector of pointers. Then usenew
to allocate the memory for c objects and push them into vector. Also, don't forget that you have todelete
yourself and vector.clear() will not release the memory allocated for c objects. You have to store c as a vector of pointers here, otherwise the call to the virtual function will not work.尝试Boost指针容器库。 与常规指针向量相比,它有几个优点,例如:
Try Boost Pointer Container Library. It has several advantages over regular vector of pointers, like: