指针迭代器交互 [英] pointer iterator interaction

查看:58
本文介绍了指针迭代器交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,对于大多数人来说这应该相当容易(至少我希望如此),

但不适合我:


让我们说我们有以下要素:

std :: vector< Entry>楷模; //条目是一个结构

std :: vector< Entry> :: iterator modelIterator;


在一个方法中,我正在写作,我需要得到一个指向

条目的向量..我想到了以下内容:


条目* MyObject :: getNextEntry(){

条目* tmpEntry;


if(modelIterator == models.end()){

返回NULL;

} else {

tmpEntry = modelIterator;

modelIterator ++;

return tmpEntry;

}

}


不幸的是,这不起作用,我收到错误信息:

错误:无法兑换`

__gnu_cxx :: __ normal_iterator< Entry *,std :: vector< Entry,

std :: allocator< Entry> > >''在分配中输入'Entry *''


我真的不明白这个:据我所知,迭代器

应该是一个指向向量元素的指针,上面的

mehtod中的tmpEntry就是这个......


如何获得指向迭代器的元素

目前指向的?


非常感谢提前

Tim

Ok, this should be fairly easy for most of you (at least I hope so),
but not for me:

Let us say we have got the following elements:
std::vector<Entry> models; //Entry is a struct
std::vector<Entry>::iterator modelIterator;

In a method, I am currently writing, I need to get a pointer to an
entry in the vector.. I thought of the following:

Entry* MyObject::getNextEntry() {
Entry* tmpEntry;

if (modelIterator == models.end()) {
return NULL;
} else {
tmpEntry = modelIterator;
modelIterator++;
return tmpEntry;
}
}

Unfortunately, this does not work, I get the error message:
error: cannot convert `
__gnu_cxx::__normal_iterator<Entry*, std::vector<Entry,
std::allocator<Entry> > >'' to `Entry*'' in assignment

I do not really understand this: As far as I understood it, an iterator
should be a pointer to the elements of the vector, and tmpEntry in the
mehtod above is nothing else than this..

How can I get a pointer to the element to which the iterator is
currently pointing?

Thanks a lot in advance
Tim

推荐答案

silversurfer写道:
silversurfer wrote:
让我们说我们有以下元素:
std :: vector< Entry>楷模; // Entry是一个结构
std :: vector< Entry> :: iterator modelIterator;
[snipped] Entry * MyObject :: getNextEntry(){
Entry * tmpEntry;

if(modelIterator == models.end()){
返回NULL ;
} else {
tmpEntry = modelIterator;
尝试:

tmpEntry =&(* modelIterator); modelIterator ++;
返回tmpEntry;
}
}

我真的不明白这个:据我所知,迭代器应该是一个指向向量元素的指针,上面的
mehtod中的tmpEntry只不过是这个..
Let us say we have got the following elements:
std::vector<Entry> models; //Entry is a struct
std::vector<Entry>::iterator modelIterator; [snipped] Entry* MyObject::getNextEntry() {
Entry* tmpEntry;

if (modelIterator == models.end()) {
return NULL;
} else {
tmpEntry = modelIterator; try:
tmpEntry = &(*modelIterator); modelIterator++;
return tmpEntry;
}
}
I do not really understand this: As far as I understood it, an iterator
should be a pointer to the elements of the vector, and tmpEntry in the
mehtod above is nothing else than this..



它的行为就像你可以进行干涉和增量等。


Alan


It behaves like one as in you can derference and increment etc.

Alan


silversurfer写道:
silversurfer wrote:
好的,这对大多数人来说应该相当容易(至少我希望如此),
但不适合我:

让我们说我们有以下元素:
std :: vector< Entry>楷模; // Entry是一个结构
std :: vector< Entry> :: iterator modelIterator;

在一个方法中,我正在编写,我需要一个指向
向量中的条目..我想到了以下内容:

条目* MyObject :: getNextEntry(){
条目* tmpEntry;

if(modelIterator = = models.end()){
返回NULL;
} else {
tmpEntry = modelIterator;
modelIterator ++;
return tmpEntry;
}
}

不幸的是,这不起作用,我收到错误信息:
错误:无法转换`
__gnu_cxx :: __ normal_iterator< Entry *,std :: vector<输入,
std :: allocator< Entry> > >''到'Entry *''在作业中

我真的不明白这个:据我所知,迭代器
应该是指向向量元素的指针上面的
mehtod中的tmpEntry只不过是这个......

不,它可能是,在某些实现中是,但它不一定是。如何获得指向迭代器当前指向的元素的指针?
Ok, this should be fairly easy for most of you (at least I hope so),
but not for me:

Let us say we have got the following elements:
std::vector<Entry> models; //Entry is a struct
std::vector<Entry>::iterator modelIterator;

In a method, I am currently writing, I need to get a pointer to an
entry in the vector.. I thought of the following:

Entry* MyObject::getNextEntry() {
Entry* tmpEntry;

if (modelIterator == models.end()) {
return NULL;
} else {
tmpEntry = modelIterator;
modelIterator++;
return tmpEntry;
}
}

Unfortunately, this does not work, I get the error message:
error: cannot convert `
__gnu_cxx::__normal_iterator<Entry*, std::vector<Entry,
std::allocator<Entry> > >'' to `Entry*'' in assignment

I do not really understand this: As far as I understood it, an iterator
should be a pointer to the elements of the vector, and tmpEntry in the
mehtod above is nothing else than this..
No, it might be and in some implementation is is, but it doesn''t have to be. How can I get a pointer to the element to which the iterator is
currently pointing?



&(* modelIterator);


应该这样做。


-

Ian Collins。


&(*modelIterator);

should do it.

--
Ian Collins.


" silversurfer" <き**** @ web.de>写道:
"silversurfer" <ki****@web.de> wrote:
好的,对大多数人来说这应该相当容易(至少我希望如此),
但不适合我:

让我们说我们有以下元素:
std :: vector< Entry>楷模; // Entry是一个结构
std :: vector< Entry> :: iterator modelIterator;

在一个方法中,我正在编写,我需要一个指向
向量中的条目。


不,你不是。不要在向量中使用指向元素的指针。

指向向量中元素的指针往往会变得陈旧,因为

向量会在它们增长时重新分配,所以你''d最终

最终取消引用狂野指针并使你的程序崩溃。

我想到了以下内容:

Entry * MyObject :: getNextEntry ()条目* tmpEntry;

if(modelIterator == models.end()){
返回NULL;
}否则{
tmpEntry = modelIterator;
modelIterator ++;
返回tmpEntry;
}
}
不幸的是,这不起作用


当然它不起作用。您的功能的返回类型是:


条目*


但是tmpEntry的类型是:


std :: vector< Entry> :: iterator


这两种类型甚至不是很相似。

我收到错误消息: __gnu_cxx :: __ normal_iterator< Entry *,std :: vector< Entry,
std :: allocator< Entry> > >''到'条目*''在作业中分配


是的,正如我所说的,甚至没有相似之处。你问

编译器将苹果转换为pipedreams。编译器是否怀疑你的理智,这是正确的。

我真的不明白这一点:据我所知,
一个迭代器应该是一个指向向量元素的指针,


迭代器不是指针。它可能(或可能不是)

在指针方面具有重要性,但除非您正在编写

a编译器,否则这不属于您的业务。 br />

迭代器可以(并且应该)使用指针,是的。

但它不是指针。它是一个迭代器。可以把它想象成b $ b抽象级别高一级。与指针相比。

如何获得指向迭代器当前指向的元素的指针?
Ok, this should be fairly easy for most of you (at least I hope so),
but not for me:

Let us say we have got the following elements:
std::vector<Entry> models; //Entry is a struct
std::vector<Entry>::iterator modelIterator;

In a method, I am currently writing, I need to get a pointer to an
entry in the vector.
No you don''t. Don''t use pointers to elements in Vectors.
Pointers to elements in vectors tend to get "stale", because
vectors reallocate themselves if they grow, so you''d eventually
end up dereferencing wild pointers and crashing your program.
I thought of the following:

Entry* MyObject::getNextEntry() {
Entry* tmpEntry;

if (modelIterator == models.end()) {
return NULL;
} else {
tmpEntry = modelIterator;
modelIterator++;
return tmpEntry;
}
}

Unfortunately, this does not work
Of course it doesn''t work. The return type of your function is:

Entry*

but the type of tmpEntry is:

std::vector<Entry>::iterator

The two types are not even remotely similar.
I get the error message:
error: cannot convert `
__gnu_cxx::__normal_iterator<Entry*, std::vector<Entry,
std::allocator<Entry> > >'' to `Entry*'' in assignment
Yes, as I said, "not even remotely similar". You''re asking the
compiler to convert apples to pipedreams. The compiler is
doubting your sanity, and rightly so.
I do not really understand this: As far as I understood it,
an iterator should be a pointer to the elements of the vector,
An iterator is not a pointer. It might (or might not) be
IMPLIMENTED in terms of a pointer, but unless you''re writing
a compiler, that''s none of your business.

An iterator can (and should) be USED LIKE a pointer, yes.
But it''s not a pointer. It''s an iterator. Think of it as
"one level higher of abstraction" compared to a pointer.
How can I get a pointer to the element to which the iterator
is currently pointing?




如果你绝对必须这样做,那么你可以很容易地这样:


条目* DangerousPointer =&(* modelIterator);


但这非常危险。要迭代Vector的元素

,请使用实数迭代器。您可以使用迭代器(几乎)

任何可以使用指针的东西。


或者使用整数下标。使用向量,使用迭代器通常比使用
更容易,并且您不必担心旧的

迭代器是否仍然指向有效的东西。 br />

-

干杯,

Robbie Hatley

美国加利福尼亚州塔斯廷

lonewolfintj在pacbell dot net(将[ciao]置于旁路垃圾邮件过滤器中)
http://home.pacbell.net/earnur/



If you absolutely MUST do that, then you can, easily, like so:

Entry* DangerousPointer = &(*modelIterator);

But that is very dangerous. For iterating through the elements
of a Vector, use real iterators. You can use iterators for (almost)
anything you could use pointers for.

Or use integer subscripting. With vectors, that''s often easier than
using iterators, and you don''t need to worry about whether an old
iterator still points to something valid.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net (put "[ciao]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/



这篇关于指针迭代器交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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