Chapter 2. Predicates, Part 1: What remove() Removes

Difficulty: 4

This Item lets you test your standard algorithm skills. What does the standard library algorithm remove() actually do, and how would you go about writing a generic function to remove only the third element in a container?

  1. What does the std::remove() algorithm do? Be specific.

  2. Write code that eliminates all values equal to 3 from a std::vector<int>.

  3. A programmer working on your team wrote the following alternative pieces of code to remove the n-th element of a container.

     // Method 1: Write a special-purpose // remove_nth algorithm. // template<typename FwdIter> FwdIter remove_nth( FwdIter first, FwdIter last, size_t n ) { /* ... */ } // Method 2: Write a function object which returns ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.