[英]Error: Vector iterator not dereferencing
我剛插入一個向量,一切都很好,當我去插入下一個時,向量迭代器當前指向vectorname.begin()。 由於我只有一個值,我所擁有的后衛應該停止迭代,但由於某種原因它會中斷。 我覺得我沒有訪問結束正確,或停止迭代..無論如何,這里的代碼正在破壞。
// check if iterator X and Y are equal to destX and destY
if(iter != this->playerVector.end())
{
while((iter->myX != destX) && (iter->myY != destY) || (iter != this->playerVector.end()))
{
iter++;
}
}
在一次迭代后檢查while語句時它會中斷。
看起來你在while
循環中缺少適當的條件 ,這就是為什么它總是true
(當你有各種操作符時也會使用括號)。 你的病情應該是,
while
((iter-> myX!= destX)&&(iter-> myY!= destY)&&
(iter!= this-> playerVector.end()))
以下是while
循環的另一個簡單版本:
while(iter != this->playerVector.end())
{
if(iter->myX == destX || iter->myY == destY)
break;
iter++;
}
如果你想要做的是“//檢查迭代器X和Y是否等於destX和destY”那么你已經使它比需要的更復雜..
while(iter != this->playerVector.end())
{
if(iter->myX == destX && iter->myY == destY) {
cout << "found destX/Y at " << iter << endl;
break;
}
iter++;
}
- 編輯 -
也知道當你寫這樣的looong語句如x || y ^ z && a & b...
x || y ^ z && a & b...
,然后你還必須考慮運算符優先級 ,但在這種情況下我猜他們唯一的問題就是讓它不可讀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.