Reputation: 11
I have a question..How can I delete more than one elements from an one dimensional array in C++? Suppose I have an array A={1,3,5,8,9,7} and I want to delete suppose 3,5,7 from array A. Please kindly let me know if anyone knows any efficient algorithm.
Upvotes: 1
Views: 961
Reputation: 10770
Arrays are not resizable in C++. The best option for a resizable container is std::vector
which you would use as:
std::vector<int> v = {1,3,5,8,9,7};
and then to remove elements by some predicate:
auto new_end = std::remove_if(v.begin(), v.end(),
std::bind(std::less<int>(), _1, 6));
But this only shuffles the elements around your vector
so that they are all at the end. To the actually erase them, you need to call:
v.erase(new_end, v.end());
Upvotes: 7