反向迭代器错误:'rcit!= std :: vector <_Tp,_Alloc> :: rend()中的'operator!='与_Tp = int,_Alloc = std :: allocator'不匹配

mun*_*ish 5 c++ iterator

代码A:

vector< int >::const_reverse_iterator rcit;
vector< int >::const_reverse_iterator tit=v.rend();
for(rcit = v.rbegin(); rcit != tit; ++rcit)
cout << *rcit << " ";
Run Code Online (Sandbox Code Playgroud)

代码B:

vector< int >::const_reverse_iterator rcit;
for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
cout << *rcit << " ";
Run Code Online (Sandbox Code Playgroud)

CODE A工作正常,但为什么CODE B通过错误:

DEV C++\vector_test.cpp 与'rcit!= std :: vector <_Tp,_Alloc> :: rend()中的'operator!='匹配_Tp = int,_Alloc = std :: allocator'.

这是我试图编写的程序.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using namespace std;
#include <vector>
using std::vector;
template< typename T > void printVector( const vector< T > &v);

template< typename T > 
void printVector( const vector< T > &v)
{
             typename vector< T >::const_iterator cit;
             for(cit = v.begin(); cit != v.end(); ++cit)
             cout << *cit << " ";
}

int main()
{
    int number;
    vector< int > v;

    cout << "Initial size of the vector : " << v.size()
         << " and capacity : " << v.capacity() << endl;
    for(int i=0; i < 3; i++)
    {
            cout << "Enter number : ";
            cin >> number;
            v.push_back(number);
    }
    cout << "Now size of the vector : " << v.size()
         << "and capacity : " << v.capacity() << endl;

    cout << "output vector using iterator notation " << endl; 
    printVector(v);

    cout << "Reverse of output ";
    vector< int >::const_reverse_iterator rcit;
    for(rcit = v.rbegin(); v.rend() != rcit ; ++rcit)
    cout << *rcit << " ";
    cin.ignore(numeric_limits< streamsize >::max(), '\n'); 
    cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

sje*_*397 8

问题是该rend方法有两种形式:

reverse_iterator rend();
const_reverse_iterator rend() const;
Run Code Online (Sandbox Code Playgroud)

在进行比较时,似乎第一个被使用(虽然我不知道为什么),并且!=没有定义用于比较'const'和'non-const'迭代器的运算符.但是,在分配给变量时,编译器可以推断出要调用的正确函数,并且一切正常.