reverse iterator

closed account (1vf9z8AR)
Reverse iterator is giving weird errors without showing which line has the error.
const_ won't work as given on stack overflow.

Part of code which I think has the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  map<int,int>::reverse_iterator it;
    for(it=m.rbegin();it!=m.rend();++it)
    {
        if(it->second<pos)
        {
            count+=pos-it->second;
            map<int,int>::reverse_iterator i;
            for(i=it+1;i!=m.rend();++i)
            {
                if(i->second-pos>0)
                    count+=i->second-pos;
            }
            cout<<count;
            return 0;
        }
        if(it->second>pos)
            pos=it->second;
    }


Error message-
 
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0, from /usr/include/c++/5/bits/char_traits.h:39, from /usr/include/c++/5/ios:40, from /usr/include/c++/5/ostream:38, from /usr/include/c++/5/iostream:39, from /hackerearth/CPP14_f89e_0633_f143_e667/s_0dbf_a7df_dcc3_6244.cpp:1: /usr/include/c++/5/bits/stl_iterator.h: In instantiation of ‘std::reverse_iterator<_Iterator> std::reverse_iterator<_Iterator>::operator+(std::reverse_iterator<_Iterator>::difference_type) const [with _Iterator = std::_Rb_tree_iterator<std::pair<const int, int> >; std::reverse_iterator<_Iterator>::difference_type = long int]’: /hackerearth/CPP14_f89e_0633_f143_e667/s_0dbf_a7df_dcc3_6244.cpp:23:22: required from here /usr/include/c++/5/bits/stl_iterator.h:233:41: error: no match foroperator-’ (operand types are ‘const std::_Rb_tree_iterator<std::pair<const int, int> >’ and ‘std::reverse_iterator<std::_Rb_tree_iterator<std::pair<const int, int> > >::difference_type {aka long int}’) { return reverse_iterator(current - __n); } ^ /usr/include/c++/5/bits/stl_iterator.h:328:5: note: candidate: template<class _Iterator> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) operator-(const reverse_iterator<_Iterator>& __x, ^ /usr/include/c++/5/bits/stl_iterator.h:328:5: note: template argument deduction/substitution failed: /usr/include/c++/5/bits/stl_iterator.h:233:41: note: ‘const std::_Rb_tree_iterator<std::pair<const int, int> >’ is not derived from ‘const std::reverse_iterator<_Iterator>’ { return reverse_iterator(current - __n); } ^ /usr/include/c++/5/bits/stl_iterator.h:380:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&) operator-(const reverse_iterator<_IteratorL>& __x, ^ /usr/include/c++/5/bits/stl_iterator.h:380:5: note: template argument deduction/substitution failed: /usr/include/c++/5/bits/stl_iterator.h:233:41: note: ‘const std::_Rb_tree_iterator<std::pair<const int, int> >’ is not derived from ‘const std::reverse_iterator<_Iterator>’ { return reverse_iterator(current - __n); } ^ /usr/include/c++/5/bits/stl_iterator.h:1138:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&) operator-(const move_iterator<_IteratorL>& __x, ^ /usr/include/c++/5/bits/stl_iterator.h:1138:5: note: template argument deduction/substitution failed: /usr/include/c++/5/bits/stl_iterator.h:233:41: note: ‘const std::_Rb_tree_iterator<std::pair<const int, int> >’ is not derived from ‘const std::move_iterator<_Iterator>’ { return reverse_iterator(current - __n); } ^ /usr/include/c++/5/bits/stl_iterator.h:1145:5: note: candidate: template<class _Iterator> decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&) operator-(const move_iterator<_Iterator>& __x, ^ /usr/include/c++/5/bits/stl_iterator.h:1145:5: note: template argument deduction/substitution failed: /usr/include/c++/5/bits/stl_iterator.h:233:41: note: ‘const std::_Rb_tree_iterator<std::pair<const int, int> >’ is not derived from ‘const std::move_iterator<_Iterator>’ { return reverse_iterator(current - __n); } ^
Last edited on
std::map iterators (reverse or otherwise) are bidirectional. As such, they only support increments and decrements. They don't support + or -.
1
2
3
4
5
6
7
8
9
10
11
std::map<int, int> m;
//...
auto it = m.begin();
it++; //OK
++it; //OK
it--; //OK
--it; //OK
auto &kv = *it; //OK.
auto it2 = it + 3; //Error.
auto &kv2 = it[3]; //Error.
auto it3 = it - 3; //Error. 
It's "i = it + 1" causing the problem. Try this for lines 7 and 8 above:

1
2
            map<int,int>::reverse_iterator i = it;
            for(++i; i != m.rend(); ++i)

Note that it would probably be better to post the error outside of an output tag so you get some line wrapping!
Last edited on
Topic archived. No new replies allowed.