如何使用迭代器? [英] How to use iterator ?
本文介绍了如何使用迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class A<br />
{<br />
private:<br />
int m_aData[100];<br />
public:<br />
// TH1<br />
int getKth(const int &p)<br />
{<br />
for (int i = 1; i <= 100; i ++)<br />
if (m_aData[i - 1] == p)<br />
return i;<br />
};<br />
// TH2<br />
int getKth (const int &p)<br />
{<br />
for (int i = 0; i <= 99; i ++)<br />
if (m_aData[i] == p)<br />
return i;<br />
};<br />
};
我不知道该选择TH1还是TH2机箱
因为我认为TH1比TH2自然,但是当我使用更多m_aData [iPosition-1]
时,它非常复杂
但是当我思考时,m_aData [iPosition]很简单但并不自然.
请说明我该怎么做.
I don''t know i should choose case TH1 or TH2
Because i think TH1 is natural than TH2, but it very complex when i use more m_aData[iPosition - 1]
But m_aData[iPosition] is simple but isnot natural, logic when i thinking.
Please explain what i should do.
推荐答案
我将其称为''index'',而不是''iterator'' .
请注意:您忘了返回一个值来指示"未找到项目"条件(顺便说一句,这比选择索引用法约定更为重要).
这是我的(个人)偏好:
I would call it ''index'', not ''iterator''.
Please note: you forgot to return a value to indicate the ''item not found'' condition (incidentally this si far more important than choosing a convention on index usage).
This is my (personal) preference:
class A
{
private:
int m_aData[100];
public:
int findIndexOf(const int p)// there''s no reason to use reference.
{
for (int i = 0; i <100; i++)
if (m_aData[i] == p)
return i;
return -1; // you forgot this.
}
//..
};
:)
好.您对我的问题回答有误.
我想问位置或位置-1
ok. you answer wrong about my question.
i want to ask positon or position - 1
这篇关于如何使用迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文