Skip to main content
added const to mid.
Source Link
fred
  • 111
  • 3

For the C++ code I would write the following

template<typename biDiIt, typename compare>
bool
isPalindrome(biDiIt first, biDiIt last, compare comp)
{
  const auto dist = std::distance(first,last);
  const auto rev_last = std::make_reverse_iterator(last);
  const auto mid = std::next(first,dist/2);
  return std::equal(first, mid, rev_last, comp);
}

template<typename biDiIt>
bool
isPalindrome(biDiIt first, biDiIt last)
{
  using value_type = typename std::iterator_traits<biDiIt>::value_type;
  return isPalindrome(first, last, std::equal_to<value_type>() );
}

This is code in the spirit of the standard template library, and it would work for any data structure equipped with bidirectional iterators. It also can use a custom comparision function if you wanted to say, for example that all vowels are equivalent and all constanants are equivalent.

So for your function we would write

bool
isPalindrome(const std::string& input)
{
   return isPalindrome(std::begin(input), std::end(input));
}

For the C++ code I would write the following

template<typename biDiIt, typename compare>
bool
isPalindrome(biDiIt first, biDiIt last, compare comp)
{
  const auto dist = std::distance(first,last);
  const auto rev_last = std::make_reverse_iterator(last);
  auto mid = std::next(first,dist/2);
  return std::equal(first, mid, rev_last, comp);
}

template<typename biDiIt>
bool
isPalindrome(biDiIt first, biDiIt last)
{
  using value_type = typename std::iterator_traits<biDiIt>::value_type;
  return isPalindrome(first, last, std::equal_to<value_type>() );
}

This is code in the spirit of the standard template library, and it would work for any data structure equipped with bidirectional iterators. It also can use a custom comparision function if you wanted to say, for example that all vowels are equivalent and all constanants are equivalent.

For the C++ code I would write the following

template<typename biDiIt, typename compare>
bool
isPalindrome(biDiIt first, biDiIt last, compare comp)
{
  const auto dist = std::distance(first,last);
  const auto rev_last = std::make_reverse_iterator(last);
  const auto mid = std::next(first,dist/2);
  return std::equal(first, mid, rev_last, comp);
}

template<typename biDiIt>
bool
isPalindrome(biDiIt first, biDiIt last)
{
  using value_type = typename std::iterator_traits<biDiIt>::value_type;
  return isPalindrome(first, last, std::equal_to<value_type>() );
}

This is code in the spirit of the standard template library, and it would work for any data structure equipped with bidirectional iterators. It also can use a custom comparision function if you wanted to say, for example that all vowels are equivalent and all constanants are equivalent.

So for your function we would write

bool
isPalindrome(const std::string& input)
{
   return isPalindrome(std::begin(input), std::end(input));
}
Source Link
fred
  • 111
  • 3

For the C++ code I would write the following

template<typename biDiIt, typename compare>
bool
isPalindrome(biDiIt first, biDiIt last, compare comp)
{
  const auto dist = std::distance(first,last);
  const auto rev_last = std::make_reverse_iterator(last);
  auto mid = std::next(first,dist/2);
  return std::equal(first, mid, rev_last, comp);
}

template<typename biDiIt>
bool
isPalindrome(biDiIt first, biDiIt last)
{
  using value_type = typename std::iterator_traits<biDiIt>::value_type;
  return isPalindrome(first, last, std::equal_to<value_type>() );
}

This is code in the spirit of the standard template library, and it would work for any data structure equipped with bidirectional iterators. It also can use a custom comparision function if you wanted to say, for example that all vowels are equivalent and all constanants are equivalent.