Ways of deleting pointers in a vector [C++ ]

  c++, pointers, reference, vector
std::vector<Circle*> circles

for(int i=0; i<circles.size(); i++)    // 1
    delete circles[i];

for(auto & circle : circles)           // 2
    delete circle;

for(Circle * circle : circles)         // 3
    delete circle;

 for(Circle *& circle : circles){      // 4
    delete circle;

If I write it the first way, CLion IDE suggests me to use the second way so I guess they’re the same. I’m not really sure why there’s a reference next to auto and if any of methods 3 or 4 are correct as well? I would guess 4 is also same as 1 and 2.

Source: Windows Questions C++

LEAVE A COMMENT