是否有可能创建一个指针的向量? [英] Is it possible to create a vector of pointers?

查看:136
本文介绍了是否有可能创建一个指针的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道,因为我遇到了一个问题,是否有可能创建一个指针的向量?如果是,如何?特别是关于使用迭代器和.begin()与它,即:我将如何将这个向量变成指针的向量:

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();
}


推荐答案

载体< c> cvect 不是指针的向量。它是类型c的对象的向量。您想要向量< c *> cvect 。您可能需要:

vector <c> cvect is not a vector of pointers. It is a vector of objects of type c. You want vector <c*> cvect. and the you probably want:

cvect.push_back( new c );

然后,给定一个迭代器,你需要如下:

And then, given an iterator, you want something like:

(*it)->func();

当然,很可能你不想要一个指针的向量。 。

Of course, it's quite probable you didn't want a vector of pointers in the first place...

这篇关于是否有可能创建一个指针的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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