43 lines
694 B
C++
43 lines
694 B
C++
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
|
||
|
template<class T>
|
||
|
bool isPalindrome(std::vector<T> v) {
|
||
|
|
||
|
// The Empty Vector is also a Palindrome I guess
|
||
|
if ( v.begin() == v.end() ) {
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
auto iter_front = v.begin() ;
|
||
|
auto iter_end = v.end() - 1;
|
||
|
|
||
|
// If
|
||
|
while ( iter_front < iter_end ) {
|
||
|
|
||
|
// Values are not the same so it is not a palindrome
|
||
|
if ( *iter_front != *iter_end ) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
// Move iterators
|
||
|
iter_front++;
|
||
|
iter_end--;
|
||
|
}
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
std::vector<int> l = {1, 2, 3, 2, 1};
|
||
|
|
||
|
if ( isPalindrome(l) ) {
|
||
|
std::cout << "This is a Palindrome" << std::endl;
|
||
|
}
|
||
|
else {
|
||
|
std::cout << "This is *not* a Palindrome" << std::endl;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|