From the course: C++ Templates and the STL

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Partitions

Partitions

- [Instructor] The partition functions rearrange a container, so the elements that meet a criteria are at the beginning. Let's take a look at an example. Here I have a working copy of partition.cpp from chapter six of the exercise files. I have a predicate function called is even tens, which checks to see if the tens place of a number is even, and we're going to use that for our predicate function here. Here we have the partition function, which simply takes the range of the vector to be modified and checks it against the predicate function. And what happens here, you notice our prime numbers now are only greater than 10 and less than 100. And when I build and run this, you'll see that all the even numbered tens place are moved to the front and the odd numbered ones are left at the end. Now you'll notice that within each partition, the elements are no longer in order. There's a stable partition version of this, and I'll build and run that. That leaves the elements in order within…

Contents