A Palindrome Template Function Write a function template called palindrome that
ID: 3621370 • Letter: A
Question
A Palindrome Template FunctionWrite a function template called palindrome that takes a vector parameter and returns true or false according to whether the vector contains a palindrome. For example the integer sequence 1, 2, 3, 2, 1 is a palindrome; 1, 2, 3, 4, 5 is not. Another example the characters sequence ‘A’, ‘B’, ‘B’, ‘A’ is a palindrome; ‘A’, ‘C’, ‘D’, ‘C’ is not. This is why ABBA is a better band than ACDC.
Test your code with sequences of various types, including:
1) Integers
2) Characters
3) Complex Numbers (the complex class will need the equivalence operator, operator== and/or the nonequivalence operator!=)
#include <vector> // vector class-template definition
// function template palindrome definition
template < class X >
bool palindrome( const std::vector< X > &vec )
{
// your code
}
I have this item:
#include <iostream>
#include <vector>
using namespace std;
// [put all complex class here
template <class X>
void foo(vector<X> & v)
{
cout << "The vector contains: ";
for(int i = 0; i < v.size(); i++)
cout << v[i] << endl;
}
int main()
{
vector<int> vic;
vic.push_back(42);
vic.push_back(13);
vic.push_back(13);
vic.push_back(42);
foo(vic);//template
vector<char> vcc;// overloads vector class for chars.
vcc.push_back(65);
vcc.push_back(66);
vcc.push_back(66);
vcc.push_back(65);
foo(vcc);//template
}
//for(i=0 j= vector size()-1; if (v[i] != v[j];
//while i< j, i++, j-- bool palindrome
Explanation / Answer
Dear, Here is the code template bool isPalindrome (vector v) { reverse(v.begin(), v.begin()+v.size()/2); return vector(v.begin(),v.begin()+v.size()/2)==vector(v.end()-v.size()/2,v.end()); } Hope this will help you..Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.