代码之家  ›  专栏  ›  技术社区  ›  Brian Hooper

反向迭代器无法编译

c++
  •  5
  • Brian Hooper  · 技术社区  · 15 年前

    #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;
        }
    }
    

    试图编译它会产生以下结果。。。

    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> > >&)
    

    有人知道我该怎么做吗?

    4 回复  |  直到 15 年前
        1
  •  19
  •   CB Bailey    15 年前

    你需要使用 const_reverse_iterator ( print const a )以及 a.rbegin() a.rend() 而不是 begin() end() .

        2
  •  3
  •   rubenvb    15 年前

    可能导致代码出现问题的两件事:

    1. 你的 print() 函数已声明 const ,因此您将(可能需要或应该)使用 const_reverse_iterator .

    2. 你在创造一个 reverse_iterator normal_iterator ( std::vector<T>::begin() )

        3
  •  2
  •   Michał Trybus    15 年前

    颠倒 rbegin rend

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

    编辑(完整性)

        4
  •  1
  •   greedy52    8 年前

    以上答案已经指出 const_reverse_iterator rbegin / rend

    除此之外,一个好的做法是 crbegin crend (在C++ 11中引入)明确指示您请求迭代器的const版本,因为 print 函数是常量。