Sequencing is always the most commonly used algorithm in data structure, and the algorithm of STL is very rich, and how to use it effectively is worth discussing. I did not find the translation of clause 31 on the Internet, so I translated it myself. --Winter
How do I sort? Let me count there are several ways.
Once the programmer needs to sort the container elements, the sort algorithm will immediately appear in his mind (some programmers may think of qsort, but after reading terms 46, they will discard the idea of using qsort and use the sort algorithm).
Sort is a very good algorithm, but when you don't really need it, it's a waste. Sometimes you don't need a complete sort (for short, full sorting). For example, you have a vector container that contains the Widget object (widget means "small pendant"), and you want to give the best 20 widgets to your best customers, all you need to do is to find out the 20 best quality widget elements, The rest does not need to be concerned with their order. What you need is a partial sort (relative to the full sort), just in the algorithm there is a veritable part of the sort function function: Partial_sort:
bool qualityCompare(const Widget& lhs, const Widget& rhs)
{
// lhs的质量不比rhs的质量差时返回true,否则返回false
}
…
partial_sort (widgets.begin(), // 把质量最好的20元素
widgets.begin() + 20, // 顺序放入widgets容器中
widgets.end(),
qualityCompare);
… // 使用 widgets...
By calling Partial_sort, the 20 elements that start in the container are the 20 best widgets you need, and in order, the quality first is widgets[0, followed by widgets [1], and so on. So you can give the quality of the first widget to your best customers, the quality of the second widget can be given to the next customer, it is convenient, more convenient is still in the back.
If you just want to give the 20 best widget gifts to your best 20 customers without having to match them, Partial_sort is a bit overqualified here. Because here, you just have to figure out these 20 elements without having to sort them out. What you need is not partial_sort, but nth_element.
The nth_element sort algorithm simply sorts an interval until the correct element is placed on the nth position you specify, that is, the point where you do the whole sort and nth_element order is that the nth position is the same element. When the Nth_element function is run, the elements that should precede position n in the full sort will not appear in front of N, and the elements in front of position n should not appear behind N. It sounds a little convoluted, mainly because I have to use my language carefully to describe the function of nth_element. Don't worry, I'll explain to you later why, now let's take a look at how nth_element the best 20 widget gifts in front of the vector container:
nth_element (widgets.begin(), // 把质量最好的20元素放在
widgets.begin() + 20, // widgets容器的前面,
widgets.end(), // 但并不关心这20个元素
qualityCompare); //本身内部的顺序
You can see that there is no difference in nature between calling the Nth_element function and calling the Partial_sort function, the only difference being that Partial_sort arranges the first 20 elements, and nth_element does not relate to their internal order. All two algorithms achieve the same function: Put the best of the 20 elements in the beginning of the vector container.
This raises an important question: what will the sorting algorithm do if the quality of the elements is the same? Suppose that 12 elements have a mass of 1 (the best level), 15 elements have a quality level of 2 (quality second), if you want to select 20 of the best widgets, choose 12 elements of mass 1, then select 15 from 8 Elements of mass 2. In the end how Nth_element and Partial_sort from 15 to choose 8, according to what? In other words, when multiple elements have the same comparison value, how does the sorting algorithm determine who is first?
For the Partial_sort and nth_element algorithms, you can't control them, but for the same elements they want to be lined up (see clause 19 for a definition of equality of two values). In our example, in the face of the need to choose from 15 levels of 2 elements to select 8 to Top 20, they will be arbitrarily selected. This also makes sense: if you ask for 20 of the best widgets, and some widgets have the same quality, when you get 20 elements that are at least as bad as the rest, you can't complain.