好吧,這是一個小問題,希望它有一個快速簡單的解決方案。C++:無法將索引分配給迭代器
在我的學校教科書,在一個關於STL一章,它提供了一個簡單的示例程序來輸入使用列表以及使用迭代器與列表,像這樣:
#include <list>
#include <iostream>
#include <string>
using namespace std;
int main()
{
list<int> myIntList;
// Insert to the front of the list.
myIntList.push_front(4);
myIntList.push_front(3);
myIntList.push_front(2);
myIntList.push_front(1);
// Insert to the back of the list.
myIntList.push_back(5);
myIntList.push_back(7);
myIntList.push_back(8);
myIntList.push_back(9);
// Forgot to add 6 to the list, insert before 7. But first
// we must get an iterator that refers to the position
// we want to insert 6 at. So do a quick linear search
// of the list to find that position.
list<int>::iterator i = 0;
for(i = myIntList.begin(); i != myIntList.end(); ++i)
if(*i == 7) break;
// Insert 6 were 7 is (the iterator I refers to the position
// that 7 is located. This does not overwrite 7; rather it
// inserts 6 between 5 and 7.
myIntList.insert(i, 6);
// Print the list to the console window.
for(i = myIntList.begin(); i != myIntList.end(); ++i)
cout << *i << " "; cout << endl;
}
現在,在行說
list<int>::iterator i = 0;
我得到VS 2015年的錯誤,說:
no suitable constructor exists to convert from"int" to "std::_List_iterator<std::_List_val<std::_List simple_types<int>>>"
什麼爲t他提出的代碼存在問題,解決方案是什麼,爲什麼這是一個開始的問題? < - (我甚至會解決一個簡單的語法錯誤)。
「在我的學校教科書」哇。 [考慮獲得更好的](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –
'std :: list'沒有隨機訪問迭代器,如'std :: vector' – ProXicT
如果學校教科書有這樣一行:*** list :: iterator i = 0; ***我同意你需要一本更好的書.. –
drescherjm