I wanna have random access to a position n in an empty STL list. If the list was not empty, I would easily access it by using advance or just using a loop to increment the iterator to n position. However, when the list is empty, I am unable to increment the iterator.
I got a message saying: "List interior not incrementable."
Thank you.
//Here is what I did
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> myList;
list<int>::iterator itr;
itr = myList.begin();
itr++;
myList.insert(itr, 33); //trying to insert 33 in the 2nd position
I know how to populate a STL list. However, the whole point is about an empty STL list.
So, once you insert 10, it is not an empty list anymore.
I wanted to find a way to increment the iterator, to n position, while the STL list is empty.
...specifically, the version of list::insert() you are using inserts a new element before an existing element. An empty list has no existing elements. Hence failure.