使用vector :: iterator时出现问题 [英] Trouble with vector::iterator

查看:74
本文介绍了使用vector :: iterator时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一直在使用vector :: iterators一段时间了。这是我遇到这个问题的第一个时间。

向量包含一个元素。


1.向量< GroupSetTemplate> ; :: iterator gstIt;

2. for(gstIt = this-> invariantState.getGroupSetTemplates()。begin();

gstIt!= this-> invariantState .getGroupSetTemplates()。end(); gstIt ++)

3 {

4 cout<<"> Gst name"<<(* gstIt)。 getName()<< endl;

5}


第4行引发了我的分段错误。


但是,如果我使用vector :: at()重写上面的代码,它工作正常。

GroupSetTemplate element = invariantState.getGroupSetTemplates()。at

(0);

cout<<"" Gst name"<< element.getName()<< endl;


所以我知道没有问题与元素。我错过了

的东西吗?

Hi,

I have been using vector::iterators for a while now. This is the first
time I have encountered this problem.
The vector contains one element.

1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}

Line 4 throws me segmentation fault.

However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;

So I know that there is no problem with the element. Am I missing
something ?

推荐答案

Prasad写道:
Prasad wrote:

我一直在使用vector :: iterators一段时间了。这是我遇到这个问题的第一个时间。

向量包含一个元素。


1.向量< GroupSetTemplate> ; :: iterator gstIt;

2. for(gstIt = this-> invariantState.getGroupSetTemplates()。begin();

gstIt!= this-> invariantState .getGroupSetTemplates()。end(); gstIt ++)

3 {

4 cout<<"> Gst name"<<(* gstIt)。 getName()<< endl;

5}


第4行引发了我的分段错误。


但是,如果我使用vector :: at()重写上面的代码,它工作正常。

GroupSetTemplate element = invariantState.getGroupSetTemplates()。at

(0);

cout<<"" Gst name"<< element.getName()<< endl;


所以我知道没有问题与元素。我错过了什么?b $ b某事?
I have been using vector::iterators for a while now. This is the first
time I have encountered this problem.
The vector contains one element.

1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}

Line 4 throws me segmentation fault.

However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;

So I know that there is no problem with the element. Am I missing
something ?



不,你不知道该元素是否存在问题。在你的第二个
版本中(带''at'')你创建了一个vector元素的_copy_,而在

中你可以访问vector元素本身的第一个版本。

创建副本的过程可能会使用

向量元素伪装现有问题。试试这个


GroupSetTemplate& element =

invariantState.getGroupSetTemplates()。at(0);

cout<< Gst名称 << element.getName()<< endl;


(注意使用引用)并查看它是否正常工作还是抛出了一个

段错误。与具有副本的版本相比,这将是一个更好的测试。


否则,您提供的代码看起来很好。问题必定在其他地方。


-

祝你好运,

Andrey Tarasevich

No, you don''t know if there''s a problem with the element. In your second
version (with ''at'') you create a _copy_ of the vector element, while in
the first version you access the vector element itself. The process of
creating a copy can possibly masquerade the existing problem with the
vector element. Try this

GroupSetTemplate& element =
invariantState.getGroupSetTemplates().at(0);
cout << "Gst name" << element.getName() << endl;

(note the use of reference) and see if it works fine or also throws a
segfault. This would be a better test, compared to your version with a copy.

Otherwise, the code you provided looks fine. The problem must be elsewhere.

--
Best regards,
Andrey Tarasevich


11月18日晚上7:17,Prasad< prasadmpa ... @ gmail.comwrote:
On Nov 18, 7:17 pm, Prasad <prasadmpa...@gmail.comwrote:




我一直在使用vector :: iterators一段时间了。这是我遇到这个问题的第一个时间。

向量包含一个元素。


1.向量< GroupSetTemplate> ; :: iterator gstIt;

2. for(gstIt = this-> invariantState.getGroupSetTemplates()。begin();

gstIt!= this-> invariantState .getGroupSetTemplates()。end(); gstIt ++)

3 {

4 cout<<"> Gst name"<<(* gstIt)。 getName()<< endl;

5}


第4行引发了我的分段错误。


但是,如果我使用vector :: at()重写上面的代码,它工作正常。

GroupSetTemplate element = invariantState.getGroupSetTemplates()。at

(0);

cout<<"" Gst name"<< element.getName()<< endl;


所以我知道没有问题与元素。我错过了什么?b $ b某事?
Hi,

I have been using vector::iterators for a while now. This is the first
time I have encountered this problem.
The vector contains one element.

1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}

Line 4 throws me segmentation fault.

However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;

So I know that there is no problem with the element. Am I missing
something ?



如前所述,在(0)返回一个副本。您的原始代码涉及

迭代器。让赌注成员函数getGroupSetTemplates()返回一个

局部变量(因此seg错误)。


事实上你在for循环中有一个条件表达式喜欢

所以:


gstIt!= this-> invariantState.getGroupSetTemplates()。end()


表示编程实践不佳。

你总是可以尝试简化你班级的重建

层次结构并发布一个简短的可编辑片段。


11月19日凌晨4:09,Salt_Peter< pj_h ... @ yahoo.comwrote:
On Nov 19, 4:09 am, Salt_Peter <pj_h...@yahoo.comwrote:

On 11月18日晚上7:17,Prasad< prasadmpa ... @ gmail.comwrote:
On Nov 18, 7:17 pm, Prasad <prasadmpa...@gmail.comwrote:

我一直在使用vector :: iterators一段时间。这是我第一次遇到这个问题时的


向量包含一个元素。
I have been using vector::iterators for a while now. This is
the first time I have encountered this problem.
The vector contains one element.


1. vector< GroupSetTemplate> :: iterator gstIt;

2. for(gstIt = this-> invariantState.getGroupSetTemplates()。begin();

gstIt!= this-> invariantState.getGroupSetTemplates()。end(); gstIt ++)

3 {

4 cout<<""> Gst name"<<(* gstIt).getName()<< endl;

5}
1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}


第4行引发了我的分段错误。
Line 4 throws me segmentation fault.


但是如果我使用vector :: at()重写上面的代码,它可以正常工作。

GroupSetTemplate element = invariantState.getGroupSetTemplates()。at

(0);

cout<<"" Gst name"<< ; element.getName()<< ENDL;
However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;


所以我知道元素没有问题。我错过了什么?b $ b错过了什么?
So I know that there is no problem with the element. Am I
missing something ?


如前所述,在(0)返回一个副本。
As already noted, at(0) returns a copy.



否。函数向量<> :: at()返回一个引用。之后他复制了


No. The function vector<>::at() returns a reference. He makes
the copy afterwards.


您的原始代码涉及迭代器。让赌注成员

函数getGroupSetTemplates()返回一个局部变量

(因此seg错误)。
Your original code involves iterators. Lets bet member
function getGroupSetTemplates() returns a local variable
(hence the seg fault).



返回局部变量,或返回对本地

变量的引用。要么可以解释他的症状。 (所以

可以做一些其他的事情,但这听起来很不错

猜。)

Returns a local variable, or returns a reference to a local
variable. Either could certainly explain his symptoms. (So
could a few other things, but that sounds like a pretty good
guess.)


事实上你有一个条件表达式,因为

循环如下:
The fact that you have a conditional expression in that for
loop like so:


gstIt!= this-> invariantState.getGroupSetTemplates()。end()
gstIt!=this->invariantState.getGroupSetTemplates().end()


表示编程实践不佳。
is indicative of poor programming practices.



为什么?我会说它或多或少是标准做法。实际上,没有条件表达式的循环将是一个无穷无尽的
循环。 (当然,一直写出

this->这不是标准做法,但也许这个代码在模板中,并且

invariantState是一个依赖基类的成员。)


-

James Kanze(GABI软件)电子邮件:ja ********* @gmail .com

Conseils en informatique orient ?? e objet /

Beratung in objektorientierter Datenverarbeitung

9 place S ?? mard,78210 St.- Cyr-l''?? cole,法国,+ 33(0)1 30 23 00 34

Why? I''d say it was more or less standard practice. In fact, a
for loop without a conditional expression would be an endless
loop. (It''s not standard practice to consistently write out the
this->, of course, but maybe this code is in a template, and
invariantState is a member of a dependent base class.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient??e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S??mard, 78210 St.-Cyr-l''??cole, France, +33 (0)1 30 23 00 34


这篇关于使用vector :: iterator时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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