0

I'm looping my array to associate some data with another one. If the data doesn't exist, I'm removing the row. Basically if array1[i].frame != array2[i].frame (the arrays contains structure). I did that :

struct Sarray1 {int frame; /*...*/; Sarray2 sarray2};
struct Sarray2 {int frame; /*...*/;};

std::vector array1<Sarray1>;
std::vector array2<Sarray2>;

        int i = 0;
        for (auto& e : array1)
        {
            if (i >= array2.size())
                break;
            if (e.frame == array2[i].frame) 
                e.sarray2= array2[i];
            else
                array1.erase(array1.begin() + i);
            i++;
        }

Is it ok to do that way, for deleting the data? I always read that's not good to delete during a loop. Will I have some strange comportement? In don't know about the robustness of foreach loop in C++ when the size is changing.

Thanks in advance.

Dark Patate
  • 113
  • 2
  • 12
  • 2
    *Is it ok to do that way, for deleting the data?* -- Quick answer -- No. – PaulMcKenzie Jul 10 '20 at 10:38
  • 1
    dupe of [Removing item from vector, while in C++11 range 'for' loop?](https://stackoverflow.com/questions/10360461/removing-item-from-vector-while-in-c11-range-for-loop) or many more – underscore_d Jul 10 '20 at 12:41

0 Answers0