i'm using stl list to do a simple inserting of a range of value into the list. The problem i have is that when i do a push_front i couldnt see the value when i did a displayed of my list, but when i did a count it show that i have the number of range but couldnt see my newly inserted value. Could someone explain to me wat happen?

int main()
{ 
 list<int> myList;
 myList.push_front(9);
 myList.push_front(7);
 myList.push_front(5);
 myList.push_front(3);
 myList.push_front(1);
 
 
 list<int>::iterator p = myList.begin() ;
 
 cout << "List initialised" <<endl;
 
 myList.push_front(11);
  myList.push_back(21);
   
 cout << "List: " ;
 while(p != myList.end())
 {
    cout << *p << " ";
    p++;        
 }

 cout <<endl;
 cout << "Count: " << myList.size()<< endl;
}

Recommended Answers

All 3 Replies

Iterator is showing the first begin "adress" of list.Then you are adding the numbers.So they are not in sequence of the first ones.
The size of list will the count of all numbers.But iterator wont show last added ones.

line 11: that works only at the time the line is executed. If you add new items to the front then you have to call begin() again in order to get pointer to the new front.

thanks guys!! finally understand it!!! thanks again guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.