fork download
  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. list<string> abcd;
  8. list<string>::iterator it;
  9.  
  10. cout << "testing empty list" << endl;
  11. cout << "begin, pointer is " << &*abcd.begin() << ", value is " << *abcd.begin() << endl;
  12. cout << "end, pointer is " << &*abcd.end() << ", value is " << *abcd.end() << endl;
  13. for (it = abcd.begin(); it != abcd.end(); it++) {
  14. cout << "loop iteration, pointer is " << &*it << ", value is " << *it << endl;
  15. }
  16.  
  17. cout << endl << endl;
  18.  
  19. abcd.push_back("val1");
  20. abcd.push_back("val2");
  21. abcd.push_back("val3");
  22. cout << "testing three values in list" << endl;
  23. cout << "begin, pointer is " << &*abcd.begin() << ", value is " << *abcd.begin() << endl;
  24. cout << "end, pointer is " << &*abcd.end() << ", value is " << *abcd.end() << endl;
  25. for (it = abcd.begin(); it != abcd.end(); it++) {
  26. cout << "loop iteration, pointer is " << &*it << ", value is " << *it << endl;
  27. }
  28.  
  29. cout << endl;
  30.  
  31. cout << "comparing strings, looking for \"val2\"" << endl;
  32. for (it = abcd.begin(); it != abcd.end(); it++) {
  33. cout << "comparing \"" << *it << "\" to \"val2\"" << endl;
  34. cout << "it->compare returns " << it->compare("val2") << endl;
  35. if (it->compare("val2") == 0) {
  36. cout << "match, breaking" << endl;
  37. break;
  38. } else {
  39. cout << "no match, continuing" << endl;
  40. continue;
  41. }
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.02s 2816KB
stdin
Standard input is empty
stdout
testing empty list
begin, pointer is 0xbfec69d4, value is 
end, pointer is 0xbfec69d4, value is 


testing three values in list
begin, pointer is 0x9c51028, value is val1
end, pointer is 0xbfec69d4, value is val3
loop iteration, pointer is 0x9c51028, value is val1
loop iteration, pointer is 0x9c51050, value is val2
loop iteration, pointer is 0x9c51078, value is val3

comparing strings, looking for "val2"
comparing "val1" to "val2"
it->compare returns -1
no match, continuing
comparing "val2" to "val2"
it->compare returns 0
match, breaking