![](/img/trans.png)
[英]C++ needs an intermediate variable to accept iterators in <vector <vector int>>
[英]Vector int iterators
我目前正在嘗試使用迭代器遍歷int
的向量。 每次運行它時,我只會得到0值。兩個向量中都確實包含有效數據。
OffenseName
和Down
是用戶輸入的私有成員數據。
vector <string> :: iterator itr;
vector <int> :: iterator itrdown;
int count = 0;
for (itr = dataOffenseName.begin(); itr!= dataOffenseName.end(); ++itr)
{
if ( OffenseName == *itr )
{
for (itrdown = dataDown.begin(); itrdown != dataDown.end(); ++itrdown)
{
//Here itrdown is always coming at 0. The dataDown vector
//does have valid data in it
if (Down == *itrdown)
{
count++;
}
else
{
break;
}
}
}
}
return count;
if (Down = *itrdown)
來吧,這本書中最古老的把戲:)
if (Down == *itrdown)
將是正確的。
if (Down = *itrdown)
應該
if (Down == *itrdown)
前者不是比較,只是一項任務,而您的意思是第二:比較。
但是,這里有一個更重要的教訓:在構建時打開警告。 如果您這樣做了,那么除非您在未評估警告的情況下忽略警告,否則您將不會錯過此警告,這不是一個好主意。
了解這種情況可能是更重要的一課,因為您根本不需要調試這樣的問題。 ;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.