是否可以创建一个指针向量?

发布于 2024-07-18 21:54:48 字数 388 浏览 6 评论 0原文

只是想知道,由于我遇到的问题,是否可以创建指针向量? 如果是这样,怎么办? 特别是关于使用迭代器和 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

与酒说心事 2024-07-25 21:54:48

当然。

vector<c*> cvect;
cvect.push_back(new sc);
vector<c*>::iterator citer;
for(citer=cvect.begin(); citer != cvect.end(); citer++) {
  (*citer)->func();
}

需要记住的事情:

如果您像我在示例中那样使用动态分配的内存,则需要自行清理,

例如:

 for(...) { delete *i; }

这可以通过使用 shared_ptr 向量来简化(例如 <代码> boost::shared_ptr )。 不要尝试为此使用 std::auto_ptr ,它不会工作(甚至不会编译)。

另一件要记住的事情是,您应该尽可能避免使用 < 来比较循环中的迭代器,它仅适用于模拟随机访问迭代器的迭代器,这意味着您无法更改您的代码使用例如 std::list

Sure.

vector<c*> cvect;
cvect.push_back(new sc);
vector<c*>::iterator citer;
for(citer=cvect.begin(); citer != cvect.end(); citer++) {
  (*citer)->func();
}

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.:

 for(...) { delete *i; }

This can be simplified by using a vector of shared_ptrs (like boost::shared_ptr). Do not attempt to use std::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. a std::list.

温柔嚣张 2024-07-25 21:54:48

向量cvect 不是指针向量。 它是 c 类型对象的向量。 你想要向量; cvect。 当然

cvect.push_back( new c );

然后,给定一个迭代器,您想要类似的东西:

(*it)->func();

,很可能您一开始并不想要指针向量......

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...

没有你我更好 2024-07-25 21:54:48

是的,这是可能的,事实上,如果您希望向量包含来自整个类层次结构而不是单一类型的对象,则有必要使用指针。 (不使用指针将导致可怕的对象切片问题——所有对象都会默默地进行转换为基类类型。编译器不会对此进行诊断,并且几乎肯定不是您想要的。)

class c
{
     void virtual func();
};

class sc:public c
{
     void func(){cout<<"using func";}
};

sc cobj;

vector<c*> cvect;             // Note the type is "c*"
cvect.push_back(&cobj);       // Note the "&"
vector<c*>::iterator citer;

for(citer=cvect.begin();citer != cvect.end();citer++)   // Use "!=" not "<"
{
     (*citer)->func();
}

请注意,使用指针向量,您需要进行自己的内存管理,因此非常小心——如果您将使用本地对象(如上所述),它们一定不能在容器之前超出范围。 如果您使用指向通过 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.)

class c
{
     void virtual func();
};

class sc:public c
{
     void func(){cout<<"using func";}
};

sc cobj;

vector<c*> cvect;             // Note the type is "c*"
cvect.push_back(&cobj);       // Note the "&"
vector<c*>::iterator citer;

for(citer=cvect.begin();citer != cvect.end();citer++)   // Use "!=" not "<"
{
     (*citer)->func();
}

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 to delete them manually before the container is destroyed. You should absolutely consider using smart pointers in this case, such as the smart_ptr provided by Boost.

是的,当然。

// TestCPP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>


using namespace std;

class c
{
public:
    void virtual func() = 0;
};

class sc:public c
{
public:
    void func(){cout<<"using func";}
};

int _tmain(int argc, _TCHAR* argv[])
{
    sc cobj;

    vector<c*> cvect;
    cvect.push_back(&cobj);
    vector<c*>::iterator citer;

    for(citer=cvect.begin();citer<cvect.end();citer++)
    {
        (*citer)->func();
    }

    return 0;
}

请注意 vector的声明 cvect 以及 cvect.push_back(&cobj) 的使用。

从提供的代码来看,您使用迭代器的方式是错误的。 要访问迭代器指向的成员,您必须使用 *citer 而不是单独使用 citer

Yes, sure.

// TestCPP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>


using namespace std;

class c
{
public:
    void virtual func() = 0;
};

class sc:public c
{
public:
    void func(){cout<<"using func";}
};

int _tmain(int argc, _TCHAR* argv[])
{
    sc cobj;

    vector<c*> cvect;
    cvect.push_back(&cobj);
    vector<c*>::iterator citer;

    for(citer=cvect.begin();citer<cvect.end();citer++)
    {
        (*citer)->func();
    }

    return 0;
}

Please note the declaration of vector<c*> cvect and the use of cvect.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 of citer alone.

灰色世界里的红玫瑰 2024-07-25 21:54:48

您已经为指针向量创建了vector。 然后使用new为c对象分配内存并将其推入向量。 另外,不要忘记您必须自己删除,并且 vector.clear() 不会释放为 c 对象分配的内存。 您必须将 c 作为指针向量存储在这里,否则对虚函数的调用将不起作用。

You have create vector<c*> for a vector of pointers. Then use new to allocate the memory for c objects and push them into vector. Also, don't forget that you have to delete 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.

孤寂小茶 2024-07-25 21:54:48

尝试Boost指针容器库。 与常规指针向量相比,它有几个优点,例如:

my_container.push_back( 0 );            // throws bad_ptr 
ptr_vector<X> pvec; 
std::vector<X*> vec;
( *vec.begin() )->foo(); // call X::foo(), a bit clumsy
pvec.begin()->foo();     // no indirection needed

Try Boost Pointer Container Library. It has several advantages over regular vector of pointers, like:

my_container.push_back( 0 );            // throws bad_ptr 
ptr_vector<X> pvec; 
std::vector<X*> vec;
( *vec.begin() )->foo(); // call X::foo(), a bit clumsy
pvec.begin()->foo();     // no indirection needed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文