common question?

it's not changing in main after OBJ_NUM is decreased by 1 in function chk_brd().
but obs_x and obs_y are works true. I wanna to decrease OBJ_NUM in function and use decreased OBJ_NUM in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void chk_brd(vector<vector<int> > &obs_x, vector<vector<int> > &obs_y, 
             int* xe, int* ye, 
             int l_x, int l_y, int OBJ_NUM)
{
  int b = 0;
  for(int j = 0; j < OBJ_NUM; j++)
  {
    if(xe[j] == l_x || xe[j] == 0 || ye[j] == l_y || ye[j] == 0)
    {
      obs_x.erase(obs_x.begin()+j);
	  obs_y.erase(obs_y.begin()+j);
      b++;              
    }
  }
  OBJ_NUM -= b;    //??
}

int main()
{
 chk_brd(obs_x, obs_y, xe, ye, l_x, l_y, OBJ_NUM);  // ??
}
Last edited on
If OBJ_NUM has the same value of the number of elements in the arrays, you don't need to pass it seperately. The index should decrease too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void chk_brd(vector<vector<int> > &obs_x, vector<vector<int> > &obs_y, 
             int* xe, int* ye, 
             int l_x, int l_y)
{
  for(int j = 0; j < obs_x.size(); ++j)
  {
    if(xe[j] == l_x || xe[j] == 0 || ye[j] == l_y || ye[j] == 0)
    {
      obs_x.erase(obs_x.begin()+j);
      obs_y.erase(obs_y.begin()+j);
      --j;
    }
  }
}

int main()
{
 chk_brd(obs_x, obs_y, xe, ye, l_x, l_y);
}
thnks but I need to pass OBJ_NUM too.
Last edited on
How does OBJ_NUM relate to the size of the vectors? If you're using OBJ_NUM to iterate the vectors.

At any rate, you just need to decrement j as it's incremented at the end of the loop.
Topic archived. No new replies allowed.