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