将指针存储在向量中? [英] Store pointers in a vector?

查看:62
本文介绍了将指针存储在向量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Conception,我在向量中有这个,它应该是:


vector< Conception> VEK; // vector

vector< Conception> :: iterator vek; // vet的迭代器


但是如果我想要指向类Conception的指针呢?怎么

我可以这样做吗?我应该如何编写声明这个

向量的迭代器?

I have a class Conception and I have this in a vector, it should be:

vector<Conception> vek; // vector
vector<Conception>::iterator vek; // iterator to vek

But what if I want to have pointers to class Conception instead? How
can I do that? And how should I write to declare an iterator to this
vector?

推荐答案

" Roland Bengtsson" < RO **************** @ home.se>在消息中写道

news:49 ************************** @ posting.google.c om ...
"Roland Bengtsson" <ro****************@home.se> wrote in message
news:49**************************@posting.google.c om...
我有一个类Conception,我在矢量中有这个,它应该是:

vector< Conception> VEK; // vector
vector< Conception> :: iterator vek; // vet的迭代器

但是,如果我想要指向类Conception的指针呢?我怎么能这样做?我应该如何编写声明这个
向量的迭代器?
I have a class Conception and I have this in a vector, it should be:

vector<Conception> vek; // vector
vector<Conception>::iterator vek; // iterator to vek

But what if I want to have pointers to class Conception instead? How
can I do that? And how should I write to declare an iterator to this
vector?




我只是转向C ++,所以这可能是错的,但不是'' ta指针只是一个

数据类型和其他什么一样?我认为你可以这样声明:


vector< Conception *> VEK; //矢量


否?


Jeremy



I am just moving to C++, so this may be wrong, but isn''t a pointer just a
data type like anything else? I would think you could declare it like this:

vector<Conception*> vek; // vector

No?

Jeremy


" ; Roland Bengtsson < RO **************** @ home.se>在消息中写道

news:49 ************************** @ posting.google.c om ...
"Roland Bengtsson" <ro****************@home.se> wrote in message
news:49**************************@posting.google.c om...
我有一个类Conception,我在矢量中有这个,它应该是:

vector< Conception> VEK; // vector
vector< Conception> :: iterator vek; // vet的迭代器


两个不同的对象有相同的名字(''vek'')!

但是如果我想有指针怎么办?改为Conception而不是?


没问题。

我怎么能这样做?


指定任何其他类型元素的方式相同。


vector< Conception *> vek;

我该如何编写声明这个
向量的迭代器?
I have a class Conception and I have this in a vector, it should be:

vector<Conception> vek; // vector
vector<Conception>::iterator vek; // iterator to vek
You have the same name (''vek'') for two different objects!

But what if I want to have pointers to class Conception instead?
No problem.
How
can I do that?
Same way you specify any other type element.

vector<Conception *> vek;
And how should I write to declare an iterator to this
vector?




vector< Conception *>: :iterator vi;


当然,现在你负责管理你的指针所指出的任何内存




-Mike



vector<Conception *>::iterator vi;

Of course now you''re responsible for managing any memory pointed
to by your pointers.

-Mike


Jeffrey Schwab写道:
Jeffrey Schwab wrote:
Jeremy Cowles写道:
Jeremy Cowles wrote:
" ; Roland Bengtsson < RO **************** @ home.se>在消息中写道
新闻:49 ************************** @ posting.google.c om ...
"Roland Bengtsson" <ro****************@home.se> wrote in message
news:49**************************@posting.google.c om...
我有一个类Conception,我在矢量中有这个,它应该是:

vector< Conception> VEK; // vector
vector< Conception> :: iterator vek; // vet的迭代器

但是,如果我想要指向类Conception的指针呢?我怎么能这样做?我应该如何编写声明这个
向量的迭代器?

我只是转向C ++,所以这可能是错的,但不是指针只是一个
数据类型和其他什么类似?我认为你可以宣布它像
这样:

vector< Conception *> VEK; //矢量

没有?

Jeremy
I have a class Conception and I have this in a vector, it should be:

vector<Conception> vek; // vector
vector<Conception>::iterator vek; // iterator to vek

But what if I want to have pointers to class Conception instead? How
can I do that? And how should I write to declare an iterator to this
vector?
I am just moving to C++, so this may be wrong, but isn''t a pointer just a
data type like anything else? I would think you could declare it like
this:

vector<Conception*> vek; // vector

No?

Jeremy



是的,但要确保你已经宣布了向量,并且你'正在寻找正确的命名空间:

#include< vector>

int main()
{
std ::矢量<概念* GT; VEK; //无用的评论



Right, but make sure you''ve declared vector, and that you''re looking in
the right namespace for it:

#include <vector>

int main( )
{
std::vector<Conception*> vek; // useless comment




哎呀!这应该是:


class Conception;


std :: vector< Conception *> VEK; //无用的评论

}



Whoops! That should be:

class Conception;

std::vector<Conception*> vek; // useless comment
}






这篇关于将指针存储在向量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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