Search code examples
c++vectoriteratorerase

erasing elements from a multidimensional vector


I'm trying to erase some old data from a 3D vector, using iterator. Here is a piece of my code, related to this:

vector< vector<vector <int> > > vol;
vector< vector< vector<int> > >::iterator row;
vector< vector<int> >::iterator col;
vector<int>::iterator dep;

for (row = this->vol.begin(); row != this->vol.end(); ++row)
{
    for (col = row->begin(); col != row->end(); ++col)
    {
        for (dep = col->begin(); dep != col->end(); ++dep)
        {
        if ( *dep <= date - 10) {

            dep = this->vol.erase( dep );
        }
    }
}

but I get the compiler error :

no matching function for call to ‘std::vector<std::vector<std::vector<int> > >::erase(std::vector<int>::iterator&)’

What am I doing wrong?

Thank you


Solution

  • You are trying to call erase on vol which to overal container. What you need to do is call erase on the vector that dep belongs to. Since dep points to an element of the vector that is pointed to by col what you need is

    if ( *dep <= date - 10) {
        dep = col->erase( dep );
    }