私がテストしてきたこの非常に単純なリストのどこに問題があるのか 理解できません。アイデアは、リストの位置 i にある項目を取得することです。私は通常、それを行うためにリストを使用しないことを知っています。ただし、これ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
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;
}