jeudi 25 février 2016

Why "unordered_map::iterator" does not provide "operator--"?

In traditiona map we can create code like this:

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, int> m;
    m[666]=222;
    m[777]=333;
    auto it = --(m.end());
    cout << (*it).first;
    return 0;
}

to move backward if needed. Yet in unordered_map for nearly same code we get a compile time error, observe:

#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
    unordered_map<int, int> m;
    m[666]=222;
    m[777]=111;
    auto it = --(m.end());
    cout << (*it).first;
    return 0;
}

prog.cpp:9:12: error: no match for 'operator--' (operand type is 'std::unordered_map<int, int>::iterator {aka std::__detail::_Node_iterator<std::pair<const int, int>, false, false>}')
  auto it = --(m.end());
            ^

Is it such difference standart-defined (and thus explained somewhere) or a compiler bug?

Aucun commentaire:

Enregistrer un commentaire