1

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.

2
  • 4
    There's always std::remove_if. Commented Oct 7, 2013 at 15:51
  • 3
    You cannot "delete eleemnts from an array". Arrays have fixed size. You should use a standard library container instead. Commented Oct 7, 2013 at 15:57

1 Answer 1

7

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.