Are gcc reverse_iterator comparison operators missing?

I have a problem using const reverse iterators on non-const containers with gcc. Well, only certain versions of gcc.

#include <vector>
#include <iostream>

using namespace std;

int main() {
    const char v0[4] = "abc";
    vector<char> v(v0, v0 + 3);

    // This block works fine
    vector<char>::const_iterator i;
    for (i = v.begin(); i != v.end(); ++i)
        cout << *i;
    cout << endl;

    // This block generates compile error with gcc 3.4.4 and gcc 4.0.1
    vector<char>::const_reverse_iterator r;
    for (r = v.rbegin(); r != v.rend(); ++r)
        cout << *r;
    cout << endl;

    return 0;
}

This program compiles OK and runs with gcc 4.2.1 (Mac Leopard) and with Visual Studio 8 and 9 (Windows) and gcc 4.1.2 (Linux).

However, there is a compilation error with gcc 3.4.4 (cygwin) and with gcc 4.0.1 (Mac Snow Leopard).

test.cpp:18: error: no match for 'operator!=' in 'r != std::vector<_Tp, _Alloc>::rend() [with _Tp = char, _Alloc = std::allocator<char>]()'

Is this a bug in earlier versions of gcc?

Due to other issues with gcc 4.2.1 on Mac, we need to use gcc 4.0.1 on Mac, so just using the new compiler is not an ideal solution for me. Therefore, I think I need to change how to use reverse iterators. Any suggestions?

+5
4

: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#280

: : , :

  • vector::reverse_iterator std::reverse_iterator<vector::iterator> vector::const_reverse_iterator std::reverse_iterator<vector::const_iterator>.
  • std::reverse_iterator , reverse_iterator<iterator> reverse_iterator<const_iterator> .

a const_reverse_iterator "rend()" , ( const) reverse_iterator.

++ 0x :

  • reverse_iterator
  • , , , const_iterator: cbegin(), cend(), crbegin() crend ​​().

const_reverse_iterator rend():

vector<char>::const_reverse_iterator r;
const vector<char>::const_reverse_iterator crend = v.rend();
for (r = v.rbegin(); r != crend; ++r)
    cout << *r;
+10

, :

return from rend() const_reverse_iterator, , :

r != static_cast<vector<char>::const_reverse_iterator>(v.rend())

, r const_reverse_iterator .

, , .

+2

, , non-const , .

for (vector<char>::const_reverse_iterator r = v.rbegin(), end_it = v.rend(); r != end_it; ++r)
    cout << *r;

.

+2

gcc, , - #include <iterator>. , , .

, reverse_iterator, (.. cout << *r;), std::copy:

std::ostream_iterator<char> output(std::cout);

// frontwards
std::copy(v.begin(), v.end(), output);

// backwards
std::copy(v.rbegin(), v.rend(), output);

copy_backwards, , , .

: : , , STLPort ( ).

+1

All Articles