fork download
  1. #include <list>
  2. #include <algorithm>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. // Fill a doubly-linked list with characters.
  8. std::string str = "racecar";
  9. std::list<char> l;
  10. for (char c : str)
  11. l.emplace_back(c);
  12.  
  13. // Find the center of the list.
  14. auto it = l.begin();
  15. std::advance(it, l.size() / 2);
  16.  
  17. // Compare the first half of the list to the second half.
  18. if (std::equal(l.begin(), it, l.rbegin()))
  19. std::cout << str.c_str() << " is a palindrome." << std::endl;
  20. else
  21. std::cout << str.c_str() << " is not a palindrome." << std::endl;
  22.  
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0s 4240KB
stdin
Standard input is empty
stdout
racecar is a palindrome.