is_palindrome_example.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright (c) Alexander Zaitsev <zamazan4ik@gmail.by>, 2016
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <vector>
  8. #include <list>
  9. #include <iterator>
  10. #include <functional>
  11. #include <iostream>
  12. #include <boost/algorithm/is_palindrome.hpp>
  13. namespace ba = boost::algorithm;
  14. template <typename T>
  15. bool funcComparator(const T& v1, const T& v2)
  16. {
  17. return v1 == v2;
  18. }
  19. struct functorComparator
  20. {
  21. template <typename T>
  22. bool operator()(const T& v1, const T& v2) const
  23. {
  24. return v1 == v2;
  25. }
  26. };
  27. int main ( int /*argc*/, char * /*argv*/ [] )
  28. {
  29. //You can this algorithm with iterators(minimum Bidirectional)
  30. std::vector<int> vec{1,2,1};
  31. if(ba::is_palindrome(vec.begin(), vec.end()))
  32. std::cout << "This container is palindrome" << std::endl;
  33. else
  34. std::cout << "This container is not palindrome" << std::endl;
  35. //Of course, you can use const iterators
  36. if(ba::is_palindrome(vec.cbegin(), vec.cend()))
  37. std::cout << "This container is palindrome" << std::endl;
  38. else
  39. std::cout << "This container is not palindrome" << std::endl;
  40. //Example with bidirectional iterators
  41. std::list<int> list{1,2,1};
  42. if(ba::is_palindrome(list.begin(), list.end()))
  43. std::cout << "This container is palindrome" << std::endl;
  44. else
  45. std::cout << "This container is not palindrome" << std::endl;
  46. //You can use custom comparators like functions, functors, lambdas
  47. auto lambdaComparator = [](int v1, int v2){ return v1 == v2; };
  48. auto objFunc = std::function<bool(int, int)>(lambdaComparator);
  49. if(ba::is_palindrome(vec.begin(), vec.end(), lambdaComparator))
  50. std::cout << "This container is palindrome" << std::endl;
  51. else
  52. std::cout << "This container is not palindrome" << std::endl;
  53. if(ba::is_palindrome(vec.begin(), vec.end(), funcComparator<int>))
  54. std::cout << "This container is palindrome" << std::endl;
  55. else
  56. std::cout << "This container is not palindrome" << std::endl;
  57. if(ba::is_palindrome(vec.begin(), vec.end(), functorComparator()))
  58. std::cout << "This container is palindrome" << std::endl;
  59. else
  60. std::cout << "This container is not palindrome" << std::endl;
  61. if(ba::is_palindrome(vec.begin(), vec.end(), objFunc))
  62. std::cout << "This container is palindrome" << std::endl;
  63. else
  64. std::cout << "This container is not palindrome" << std::endl;
  65. //You can use ranges
  66. if(ba::is_palindrome(vec))
  67. std::cout << "This container is palindrome" << std::endl;
  68. else
  69. std::cout << "This container is not palindrome" << std::endl;
  70. //You can use C-strings
  71. if(ba::is_palindrome("aba"))
  72. std::cout << "This C-string is palindrome" << std::endl;
  73. else
  74. std::cout << "This C-string is not palindrome" << std::endl;
  75. return 0;
  76. }