main.grid.at(cell).list.push_back(index); // add this triangles index to the end of the list of triangles for this cell.
// make sure there is a main.grid.at(cell)
}

cell++;

}

Code:

main.grid.resize((divisions)*(divisions));

Main.grid is resized to divisions squared, but this shouldn't matter. In this case divisions is 20.

When I run gdb opens, prints:

Quote:accessing element 397 from an array of size 400 and pushing back on an array of size 31
sorting triangle 31 into cell 398
accessing element 398 from an array of size 400 and pushing back on an array of size 31
sorting triangle 31 into cell 399
accessing element 399 from an array of size 400 and pushing back on an array of size 31
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check

What are the data type elements of your grid vector? My C++ is a little rusty, but is there a chance that the elements do not have a default constructor defined? My understanding is the vector class on resize will fill in the missing values using the default constructor. But if there is none defined, maybe this causes a problem?

The compiler generates defaults for all of the above. You do not need to implement them yourself if your class only contains primitives and objects by value that also satisfy the above requirements. If you have pointers or objects by value that don't satisfy those requirements, you will need to implement them yourself.

As for the code itself, it looks like the accessing of the grid vector seems fine. Are you sure that something isn't using cell after that loop to access the vector?

No, that is impossible. It is modifying the object at main.grid[cell], not the vector main.grid. You should put a printout right after the loop to double check if it crashes in the loop or if it's crashing sometime after it. Looking at that code, there doesn't seem to be any out of range accesses.