The reverse iterator will not compile

I am trying to compile a reverse iterator, but my attempts to do this give a terrible mess. Minimal code example ...

#include <iostream>
#include <vector>
#include <algorithm>

class frag {
    public:
        void print (void) const;
    private:
        std::vector<int> a;
};

void frag::print (void) const
{
    for (std::vector<int>::reverse_iterator iter = a.begin ();
         iter                                   != a.end ();
         ++iter) {
        std::cout << *iter << std::endl;
    }
}

and trying to compile it produces the following ...

In file included from /usr/include/c++/4.4/bits/stl_algobase.h:69,
             from /usr/include/c++/4.4/bits/char_traits.h:41,
             from /usr/include/c++/4.4/ios:41,
             from /usr/include/c++/4.4/ostream:40,
             from /usr/include/c++/4.4/iostream:40,
             from frag.cpp:1:
/usr/include/c++/4.4/bits/stl_iterator.h: In constructor ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&) [with _Iter = __gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >, _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >]’:
frag.cpp:14:   instantiated from here
/usr/include/c++/4.4/bits/stl_iterator.h:134: error: no matching function for call to ‘__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(__gnu_cxx::__normal_iterator<const int*, std::vector<int, std::allocator<int> > >)’
/usr/include/c++/4.4/bits/stl_iterator.h:686: note: candidates are: __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator(const _Iterator&) [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]
/usr/include/c++/4.4/bits/stl_iterator.h:683: note:                 __gnu_cxx::__normal_iterator<_Iterator, _Container>::__normal_iterator() [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]
/usr/include/c++/4.4/bits/stl_iterator.h:669: note:                 __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)

Yesterday there was a question on this, but I don’t think it’s the same, that it’s not a template. If a vector is declared locally in the same way, it is quite satisfied. (g ++ on Ubuntu 10.4).

Does anyone know what I should do?

+5
source share
4 answers

You need to use const_reverse_iterator( print- this is a function const, therefore athere is const) and a.rbegin()and a.rend()not begin()and end().

+18
source

, :

  • print() ​​ const, , , const_reverse_iterator.

  • reverse_iterator normal_iterator (std::vector<T>::begin())

+3

. . rbegin rend:

for (std::vector<int>::reverse_iterator iter = a.rbegin(); iter != a.rend(); ++iter);

EDIT ( ) , const_reverse_iterator.

+2

, const_reverse_iterator rbegin/rend.

crbegin crend ( ++ 11), , const, print .

+1

All Articles