Reverse iterator error: no match for 'operator! = 'in' rcit! = std :: vector <_ Tp, _Alloc> :: rend () with _Tp = int, _Alloc = std :: allocator '

CODE A:

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

CODE B:

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

CODE A works fine, but why does CODE B error:

Dev c ++ \ vector_test.cpp no match for 'operator! = 'in' rcit! = std :: vector <_Tp, _Alloc> :: rend () with _Tp = int, _Alloc = std :: allocator .

This is the program I tried to write.

#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;
}
+5
source share
3 answers

The problem is in two forms of the method rend:

reverse_iterator rend();
const_reverse_iterator rend() const;

, ( ), != 'const' 'non-const' . , , , .

+8

, Xcode 4 :

#include <iostream>
#include <vector>

int main () {
    using namespace std;
    vector< int > v;

    vector< int >::const_reverse_iterator rcit;
    for(rcit = v.rbegin(); rcit != v.rend(); ++rcit)
        cout << *rcit << " ";
}

- ? ? ? , != ==? ( , , , .)

+2

!= :

for(rcit = v.rbegin(); v.rend() != rcit; ++rcit)
-1

All Articles