Example 1 – Unfortunate exceptions and performance problems

Let's say we need a function that moves the first n elements from the front of a container to the back, like this:

Moving the first three elements to back of a range

Approach 1 – Use a traditional for-loop:

A very naive approach would be to copy the first n elements to the back while iterating over them and then erasing the first n elements:

Allocating and deallocating in order to move elements to back of range

Here's the corresponding implementation:

template <typename Container> ...

Get C++ High Performance 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.