Removing Elements from a 2D Vector

Hello,

I am writing a program for a Sudoku puzzle using Constraint Satisfaction. I have created a 2D vector to store the domains of possible values:

vector<vector<int>> domains;

The 2D vector is in a struct.

Long story short, the domains are filled with 0 values, and I need to remove any instance of the elements that are equal to 0. How do I remove an element in a 2D array?

1
2
3
4
5
6
7
8
9
10
11
12
13
 for(int i = 0; i < mainBoard.domains.size(); i++)
    {
        if(!mainBoard.isFixedValue[i])
        {
            for(int j = 0; j < 9; j++)
            {
                if(mainBoard.domains[i][j] == 0)
                {
                  mainBoard.domains[i].erase(mainBoard.domains[j].begin() + j);
                }
            }
        }
    }


When I do this, the program crashes during runtime with an error:

Cygwin_exception::open_stackdumpfile: Dumping stack trace to Soduku2.exe.stackdump


PLEASE HELP!!
Last edited on
Why that magic number 9?

What happens when you erase the first element (how many elements do you have left)?

The 9 is because there is a maximum number of 9 values that can be stored as domain values, 1-9 (possible Sudoku values). I suppose I could have the limit be the size of mainBoard.domains[i].size(). Let me see what happens, or if it succeeds, when I delete the beginning element.
Last edited on
Yes but why are you using this constant in a for() loop that removes elements? If you erase one elements you will only have 8 elements in the vector. If you then try to access the 9th element you will be accessing the vector out of range.

Topic archived. No new replies allowed.