From the course: C++ Templates and the STL

Unlock this course with a free trial

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

Sorting

Sorting

- [Instructor] The STL provides a few different options for sorting a container. Here I have a working copy of sort.cpp from chapter six of the exercise files. And, you'll notice that I have a comparison operator here for comparing two values, and we're going to start by using the default comparison in Sort, and I'll be sorting the integers here. So, I have two vectors: I have integers and I have doubles. Integers are prime numbers under a hundred that are unsorted and the doubles numbers we'll get to that in a little bit. So, when I build and run here, you'll see the Sort function with just a range in it from the begin and end of vector one. It sorts these numbers with the default comparison operator. And, if I want to use my own comparison operator, I can say mycomp, int, and build and run, and I get exactly the same result. Of course, I can turn around the comparison, make it greater than rather than less than, and now, my vector will be sorted in the opposite order. So, this all…

Contents