jeudi 24 juin 2021

Bug with the STL function vector.erase(it)

I have two vectors:

vector<float> numbers;
vector<char> operators;

Without showing you the whole code, I will show you the part of the code that gives me problems:

for (vector<int>::const_iterator it = operators[next(operators.begin(), 1]; it != operators.end(); ++it) {   
    char value = *it;
    int pos = it;               // I save the position
    if (value == '*') {
        numbers[pos-1] *= numbers[pos];
        numbers.erase(it);
        operators.erase(it);
        
        it--;                   // Since my vector is decreased by one
    
    }else if (value == '/') {
        numbers[pos-1] /= numbers[pos];
        numbers.erase(it);
        operators.erase(it);
        
        it--; 
    }
}

It gives me a bunch of errors and i can't figure it out why, such as:

candidate: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::const_iterator) [with _Tp = char; _Alloc = std::allocator<char>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::vector<char> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = char*; std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const 
char*, std::vector<char> >; typename __gnu_cxx::__alloc_traits<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type>::const_pointer = const char*]
       erase(const_iterator __position)
       ^~~~~

'std::vector<int>::const_iterator {aka __gnu_cxx::__normal_iterator<const int*, std::vector<int> >}' is not derived from 'const std::move_iterator<_IteratorL>'
     for (vector<int>::const_iterator it = operazioni[next(operazioni.begin(), 1]; it != operazioni.end(); ++it) {
                                                                                                        ^

Aucun commentaire:

Enregistrer un commentaire