Search - User list
Full Version: why Some point number cannot be removed from the array
Root » Technical Discussion » why Some point number cannot be removed from the array
luotexia2310
vector pos = point(0, "P", 6);
int npts = nearpoints(0, pos, 1.5);

printf("%s\n", npts);

foreach(int npt; npts){
if(npt == 6 || npt == 1){
removevalue(npts, npt);
}
}

printf("%s\n", npts);

===========result:
{6, 1, 5, 7, 11}
{1, 5, 7, 11}


Why point 1 cannot be removed
animatrix_
Hi,

You shouldn't modify the array you are looping over when using foreach loop. You don't have to loop over the elements like that to remove values. Just do like this:

removevalue ( npts, 6 );
removevalue ( npts, 1 );

Or loop over the elements to remove:

int elements [ ] = { 6, 1 };
foreach ( int element; elements )
    removevalue ( npts, element );
Tanto
You remove 6, array is now {1,5,7,11}, next iteration is performed on the second element of the array, 5, so 1 is never iterated upon.
luotexia2310
动画师_
Hi,

You shouldn't modify the array you are looping over when using foreach loop. You don't have to loop over the elements like that to remove values. Just do like this:

removevalue ( npts, 6 );
removevalue ( npts, 1 );

Or loop over the elements to remove:

int elements [ ] = { 6, 1 };
foreach ( int element; elements )
    removevalue ( npts, element );


I understand, thank you very much!
luotexia2310
Tanto
You remove 6, array is now {1,5,7,11}, next iteration is performed on the second element of the array, 5, so 1 is never iterated upon.
thank you very much
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB