Vector iterators for loops, return, warning, C ++

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), , ? !

+5
3

for?

, , const_iterator, const_reverse_iterator.

- , return palindrome() ok?

, *it != *rit?

palindrome.h(14) : warning C4018: '<' : signed/unsigned mismatch

i ; std::vector::size() . i , .

, , . .begin(), - .end() - 1. , it1 < it2. - ( ) :

for (iterator it1(v.begin()), it2(v.end() - 1); it1 < it2; ++it1, --it2)

i ; .

+5

? std::equal:

template <class T>
bool palindrome(const std::vector<T> &input)
{
        return equal(input.begin(), input.begin()+input.size()/2, input.rbegin());
}
+8

for , , - , , :

typedef vector<char>::const_iterator IT;

for (IT it(vchars.begin()), end(vchars.end()); it != end; ++it)
{
    cout << *it << endl;
}

return, , , , . , false. .

: , size() ( ), i, , , i unsigned.

:

for (unsigned int i = 0; i < input.size()/2; ...)
0
source

All Articles