Advertisement
Guest User

Untitled

a guest
Dec 4th, 2012
3,883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <list>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. struct test {
  6.     int n;
  7.     std::string str;
  8. };
  9.  
  10. int main(void) {
  11.     test t;
  12.     std::list<test> l;
  13.     t.str="overflow";
  14.     t.n = 1;
  15.     /* Add this item to the back */
  16.     l.push_back(t);
  17.     t.str="test";
  18.     t.n = 2;
  19.     /* Add this item to the back */
  20.     l.push_back(t);
  21.  
  22.     /* Access it through iterators */
  23.     for(std::list<test>::iterator it = l.begin(); it != l.end(); it++) {
  24.     std::cout << "item: " << (it)->n << " str: " << (it)->str << std::endl;
  25.     }
  26.  
  27.     t.str="stack";
  28.     t.n = 0;
  29.     /* add this item first */
  30.     l.push_front(t);
  31.    
  32.     /* access it directly through an iterator */
  33.     std::cout << "item: " << (*l.begin()).n << " str: " << (*l.begin()).str << std::endl;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement