Cleaning up an STL list/vector of pointers

后端 未结 15 1382
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 21:41

What is the shortest chunk of C++ you can come up with to safely clean up a std::vector or std::list of pointers? (assuming you have to call delet

15条回答
  •  心在旅途
    2020-11-28 22:37

    Since we are throwing down the gauntlet here... "Shortest chunk of C++"

    static bool deleteAll( Foo * theElement ) { delete theElement; return true; }
    
    foo_list . remove_if ( deleteAll );
    

    I think we can trust the folks who came up with STL to have efficient algorithms. Why reinvent the wheel?

提交回复
热议问题