标准迭代器问题 [英] Std Iterator Problem

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

问题描述

大家好,


我想知道是否有人能告诉我以下

代码有什么问题,

我在.h文件中的



std :: list< std :: vector< Site> >职位;

std :: list< std :: vector< Site> > :: iterator itSteps;


然后在.cpp文件中


void RmfSystem :: step(){


if(itSteps == positions.end()){


itSteps = positions.begin();


}否则{


itSteps ++;


}


}


其中step由另一个文件中的animate函数调用。这个想法

我有一个位置向量的向量,我希望每次调用

步,迭代器指向另一组位置。然后当达到列表(好向量)的

结束时,它会循环到开头。


因为它代表我的程序崩溃了,因为itStep = = positions.end()从来没有

似乎被认为是真的。


任何想法我做错了什么?

亚当

解决方案
亚当哈茨霍恩写道:大家好,

我想知道是否有人能告诉我以下
代码有什么问题,

在.h文件中我有

std :: list< std: :矢量<网站> >职位;
std :: list< std :: vector< Site> > :: iterator itSteps;

然后在.cpp文件中

void RmfSystem :: step(){

if(itSteps == positions.end()){

itSteps = positions.begin();

}其他{

itSteps ++;

}


其中step由另一个文件中的animate函数调用。这个想法
我有一个位置向量的向量,我希望每次调用
步骤,迭代器指向另一组位置。然后当到达列表的末尾(向量)时,它会循环到开头。

因为它的程序崩溃了,因为itStep == positions.end()从不<似乎被认为是真的。

任何想法我做错了什么?

亚当




除其他外,初始化的代码在哪里?b $ b''itSteps''?在某个地方(在''position''填充

数据之后)你需要这样做:


itSteps = positions.begin();


全局迭代器可能会导致意外的副作用

如果多个函数修改了迭代器引用的容器

。这是一个冒险的设计。


问候,

拉里


-

反垃圾邮件地址,将每个''X''更改为''。''直接回复。


Adam Hartshorne写道:



std :: list< std :: vector< Site> >职位;
std :: list< std :: vector< Site> > :: iterator itSteps;

然后在.cpp文件中

void RmfSystem :: step(){

if(itSteps == positions.end()){

itSteps = positions.begin();

}其他{

itSteps ++;

}

}




我在上面的代码中看不到任何错误。但是,它通常不是* b $ b * *在头文件中定义全局变量不是一个好主意。


我的猜测是错误可能是在别处找到,例如调用擦除。

它/可以/使你的迭代器无效。


-

彼得


拉里我史密斯写道:
亚当哈茨霍恩写道:
大家好,

我想知道是否有人能告诉我以下
代码有什么问题,

在.h文件中我有

std :: list< std ::矢量<网站> >职位;
std :: list< std :: vector< Site> > :: iterator itSteps;

然后在.cpp文件中

void RmfSystem :: step(){

if(itSteps == positions.end()){

itSteps = positions.begin();

}其他{

itSteps ++;

}


其中step由另一个文件中的animate函数调用。这个想法
我有一个位置向量的向量,我希望每次调用
步骤,迭代器指向另一组位置。然后当到达列表的末尾(向量)时,它会循环到开头。

因为它的程序崩溃了,因为itStep == positions.end()从不<似乎被认为是真的。

任何想法我做错了什么?

Adam



其他事情,初始化的代码在哪里''itSteps''?某处(在''position''填充
数据之后)你需要这样做:

itSteps = positions.begin();

全局迭代器可以导致意外的副作用
如果多个函数修改了迭代器引用的容器。这是一个冒险的设计。

问候,
拉里




对不起是我有以下方法


void RmfSystem :: setUpAnimation(){


itSteps = positions.begin();


}


并且它工作正常,但就像我说未能找到匹配.end因此

然后循环。


我该如何做呢?


Adam


Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam

解决方案

Adam Hartshorne wrote:

Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam



Among other things, where is the code that initializes
''itSteps''? Somewhere (after ''positions'' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry

--
Anti-spam address, change each ''X'' to ''.'' to reply directly.


Adam Hartshorne wrote:

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}



I don''t see any errors in the above code per se. However, it''s generally
*not* a good idea to define global variables in a header file.

My guess is that the error can be found elsewhere, e.g. calling erase.
It /can/ invalidate your iterator.

--
Peter


Larry I Smith wrote:

Adam Hartshorne wrote:

Hi All,

I was wondering if anybody can tell me what is wrong with the following
code,

in a .h file I have

std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;

then in the .cpp file

void RmfSystem::step() {

if (itSteps == positions.end()) {

itSteps = positions.begin() ;

} else {

itSteps++ ;

}

}

where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.

As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.

Any ideas what I am doing wrong?

Adam


Among other things, where is the code that initializes
''itSteps''? Somewhere (after ''positions'' is filled with
data) you need to do:

itSteps = positions.begin();

A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.

Regards,
Larry



Sorry yes I have the following method

void RmfSystem::setUpAnimation() {

itSteps = positions.begin() ;

}

and it works fine, but like I say fails to find match the .end and hence
then loop around.

How should I do this instead?

Adam


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

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