ZigZagZebra ZigZagZebra - 2 days ago 4
C++ Question

traverse in vector<vector<pair<int, int> > >

I came across a situation that I couldn't explain why. I have a

vector<vector<pair<int, int> > > parent
that indicates
parent
of a specific cell. I have following code:

// (1, 0), (1, 2), (1, 2)
// (1, 2), (-1, -1), (1, 2)
// (-1, -1), (2, 1), (-1, -1)
vector<vector<pair<int, int> > > nums = {
{make_pair(1, 0), make_pair(1, 2), make_pair(1, 2)},
{make_pair(1, 2), make_pair(-1, -1), make_pair(1, 2)},
{make_pair(-1, -1), make_pair(2, 1), make_pair(-1, -1)}};
int r = 0;
int c = 1;
while (nums[r][c] != make_pair(r, c)) {
cout << nums[r][c].first << " " << nums[r][c].second << endl; // 1, 2
r = nums[r][c].first; // 1
c = nums[r][c].second; // -1
cout << "r: " << r << " c: " << c << endl;
}


I 'm not sure why in the first iteration of
while
loop for
c = nums[r][c].second;
it returns -1 instead of 2.

Answer

In first iteration,

 r=num[0][1].first = 1.

Therefore

c=num[1][1].second = - 1.