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;
}

上一篇:微信小程序IDE(微信web开发者工具)安装、破解手册--转载


下一篇:轻量级工具Vite到底牛在哪——一文全知道