Article directory
1. Example of predefined function object - sort container elements from large to small
1. sort sorting algorithm
The C++ Standard Template Library (STL, Standard Template Library) provides the sort algorithm function, which is defined in the <algorithm> header file and is a generic algorithm;
The sort algorithm is used to sort elements in a container . This algorithm is very efficient and can sort elements within a given iterator range , and the sorting order can be defined based on a user-specified comparison function;
The user-specified comparison function is a binary predicate;
The function prototype of the default sorting rule of the std::sort algorithm is as follows:
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
- RandomIt first, RandomIt last parameters: This function accepts two random access iterators first and last , which define the sequence range that needs to be sorted. Note: This range is a front-closed and then-open interval;
- Default comparison rules: Elements in this range will be compared and sorted using the < operator by default . If a custom class does not implement the < operator overloaded function, an error may be reported;
The function prototype of the std::sort algorithm custom sorting rule is as follows:
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
- RandomIt first, RandomIt last parameters: This function accepts two random access iterators first and last , which define the sequence range that needs to be sorted; Note: This range is a front-closed and then-open interval;
- Custom comparison rule Compare comp parameter: This parameter is a binary predicate, that is, a function object that receives 2 parameters and returns a bool value; the elements in this range will be sorted using this binary predicate rule;
2. greater predefined function object
The C++ Standard Template Library (STL, Standard Template Library) provides the greater<T> predefined function object , which is a binary predicate . With the help of this function object, two values can be compared in a convenient way to determine the first value. Is it greater than the second value;
This function object is mainly used in STL algorithms to control sorting order, search conditions and other scenarios;
The greater<T> function object is defined in the <functional> header file. It accepts a generic type parameter T, which specifies the element type to be compared;
The greater<T> function object internally overloads the function call operator operator(), so that instance objects of this class can be called like ordinary functions;
2. Code examples - predefined function objects
1. Code examples
Code example:
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 创建一个 set 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
// 向 foreach 循环中传入 Lambda 表达式
for_each(myVector.begin(), myVector.end(), [](int a) {
std::cout << a << " ";
});
cout << endl;
// 将 myVector 容器中的元素按照从大到小的顺序排列
sort(myVector.begin(), myVector.end(), greater<int>());
// 向 foreach 循环中传入 Lambda 表达式
for_each(myVector.begin(), myVector.end(), [](int a) {
std::cout << a << " ";
});
cout << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
2. Execution results
Results of the :
9 5 2 7
9 7 5 2
Press any key to continue . . .