I have three questions about setting homework for C ++. The goal was to create a simple palindrome method. Here is my template for this:
#ifndef PALINDROME_H
#define PALINDROME_H
#include <vector>
#include <iostream>
#include <cmath>
template <class T>
static bool palindrome(const std::vector<T> &input)
{
std::vector<T>::const_iterator it = input.begin();
std::vector<T>::const_reverse_iterator rit = input.rbegin();
for (int i = 0; i < input.size()/2; i++, it++, rit++)
{
if (!(*it == *rit)) {
return false;
}
}
return true;
}
template <class T>
static void showVector(const std::vector<T> &input)
{
for (std::vector<T>::const_iterator it = input.begin(); it != input.end(); it++) {
std::cout << *it << " ";
}
}
#endif
As for the above code, can you have more than one iterator declared in the first part of the for loop? I tried to define both "it" and "rit" in the palindrome () method, and I continued to get the error about the need "," before rit. But when I cut and paste outside the for loop, no compiler errors occur. (I am using VS 2008).
, . return palindrome()? , , , * it * rit , false, . , for, true . , return, , , .
, :
\palindrome.h(14) : warning C4018: '<' : signed/unsigned mismatch
, for (i < input.size()/2), , ? !