0

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;
}
3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by picass0
0

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.

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.