(Palindromes) Write a function template palindrome that takes a vector parameter and re- turns true or false according to whether the vector does or does not read the same forward as back- ward (e.g., a vector containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).
What will be an ideal response?
```
#include
#include
using namespace std;
// function template palindrome definition
template < typename X >
bool palindrome( const vector< X > &vec )
{
typename vector< X >::const_reverse_iterator r = vec.rbegin();
typename vector< X >::const_iterator i = vec.begin();
while ( r != vec.rend() && i != vec.end() )
{
if ( *r != *i ) // values are not equal
return false;
++r;
++i;
} // end while
return true; // the vector is a palindrome
} // end function palindrome
// function template printVector definition
template < typename Y >
void printVector( const vector< Y > &vec )
{
typename vector< Y >::const_iterator i;
for ( i = vec.begin(); i != vec.end(); ++i )
cout << *i << ' ';
} // end function palindrome
int main()
{
vector< int > iv;
vector< char > ic;
int x = 0;
for ( int i = 75; i >= 65; i-- )
{
iv.push_back( i );
ic.push_back( static_cast< char > ( i + x ) );
if ( i <= 70 )
x += 2;
} // end for
printVector( iv );
cout << ( palindrome( iv ) ? " is " : " is not " ) << "a palindrome\n";
printVector( ic );
cout << ( palindrome( ic ) ? " is " : " is not " ) << "a palindrome\n";
} // end main
```
75 74 73 72 71 70 69 68 67 66 65 is not a palindrome
K J I H G F G H I J K is a palindrome
You might also like to view...
___________________ communications occur in discrete units with a start bit at the front and a stop bit at the back.
Fill in the blank(s) with the appropriate word(s).
The bottom half of the window in a query's design view is the Query ________
Fill in the blank(s) with correct word
The Go to Slide command allows you to jump among slides while presenting a slide show
Indicate whether the statement is true or false
Once a dashboard has been designed to meet the business requirements, there are several steps you need to take to get it ready for use
Indicate whether the statement is true or false