#### C++: Vector value resets efter exiting if-statement

I need to overwrite values in a 2D vector, where the new values is simply just equal to an integer I am counting up. But as soon as I exit this if-statement, the value resets to the original value? I think it may have something to do with the indexing, but I just can’t figure it out

So I fill up the vector with either -1 or 0’s

``````        vector<vector<int>> P(225, vector<int>(225, 0));

for (int i = 0; i < 225; i++) {

for (int j = 0; j < 225; j++) {

if (img.at<uchar>(i, j) >= 220) {
P[i][j] = -1;
}
else {
P[i][j] = 0;

}
}
}
``````

Where

``````img.at<uchar>(i, j)
``````

is essentially just an array I check values from, with the same size as the vector. This works fine

Then I go through the vector again, to check for all -1’s, and each time if either the vector index above or to the left of [i][j] is 0, I count up integer "bin", which I now want to place as the value on spot [i][j]

``````for (int i = 1; i < 225; i++) {
for (int j = 1; j < 225; j++) {

if ((P[i][j - 1] == 0) && (P[i - 1][j] == 0) && P[i][j] == -1) {
bin++;
P[i][j] = bin;
}
cout << P[i][j] << endl;
}

}

``````

But right after it exits the if-statement, it just forgets the new value assigned to it? I know the bin integer goes up too, I have printed that out at well, so atleast some of the vector values should be changed, when the specific situation occurs. But they all just go back to 0’s and -1’s again, as if it never went through the if-statement

I hope my explanation is understandable

Minimalistic test-program: When I run this code that uses basic iostream components, same problem occurs. I think it may be a bug from my side at this point

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

using namespace cv;

int main() {

int bin = 0;

vector<vector<int>> P(20, vector<int>(20, 0));

for (int i = 0; i < 20; i++) {

for (int j = 0; j < 20; j++) {

if (j > 15) {
P[i][j] = 0;
}
if (j <= 14 && j >= 9) {
P[i][j] = -1;
}
if (j < 9) {
P[i][j] = 0;
}

}
}

for (int i = 1; i < 20; i++) {
for (int j = 1; j < 20; j++) {

if ((P[i][j-1] == 0) && (P[i-1][j] == 0) && P[i][j] == -1) {
bin++;
P[i][j] = bin;
}
cout << P[i][j] << endl;
}

}
}
``````

Source: Windows Questions C++