我不明白我测试过的这个非常简单的列表的问题在哪里。这个想法是让我的位置在列表中的项目。 我通常知道我不会用列表来做到这一点。 然而,这个工作当我设置item = 11
,item = 12
和item = 13
(输出将分别at position {1, 2, 3} there's the item {11, 12, 13}
),但是当我设置item = 10
,因为输出是at position 0 there's the item 6
这是行不通的。这个简单的std :: list中的错误在哪里?
int main(void)
{
list<int> L;
L.push_back(10);
L.push_back(11);
L.push_back(12);
L.push_back(13);
int item = 10;
int pos;
list<int>::iterator it = moveToItem(item, L, pos);
cout << "at position " << pos << " there's the item " << *it;
}
list<int>::iterator moveToItem(int item, list<int> L, int& pos)
{
pos = 0;
list<int>::iterator it = L.begin();
while(*it != item)
{
it++;
pos++;
}
return it;
}
谢谢,就是这样! :) – FerranMG