use the lambda function to control the outputs of the sort function

The outputs:

 sort Array arr by order:
0 1 4 5 6 8 8 
 sort Array arr by reversed order:
8 8 6 5 4 1 0 

The inputs:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	vector<int> arr{1,4,8,5,0,6,8};
	sort(arr.begin(), arr.end(), [](const int& l, const int& r) { return l < r; });
	cout << " sort Array arr by order:" << endl;
	for (int i = 0; i < arr.size(); i++)
		cout << arr[i] << " ";
	cout << endl;

	sort(arr.begin(), arr.end(), [](const int& l, const int& r) { return l > r; });
	cout  << " sort Array arr by reversed order:" << endl;
	for (int i = 0; i < arr.size(); i++)
		cout << arr[i] << " ";
	cout << endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38396940/article/details/120946010