Sort using the STL write - get two

sort algorithm
Note: (1) the parameters for the iterator type
(2) default from small to large
(3) to descending order, the use of three parameters of overloaded functions, tells sort a sort case;
(4) because is the iterator type, the data must be stored in a container.

bool myCompare(int val1,int val2)
{
	return val1 > val2; //给sort一个为真的案例,他就按这个顺序调整
}
void test02()
{
	deque<int>  d1;
	d1.push_back(10);
	d1.push_back(90);
	d1.push_back(70);
	d1.push_back(60);
	d1.push_back(90);
	d1.push_back(80);
	d1.push_back(30);
	d1.push_back(40);


	sort(d1.begin(),d1.end());//从小到大排序,参数是迭代器类型
	printDeque(d1);

	sort(d1.begin(),d1.end(),myCompare);//从大到小
	printDeque(d1);
}
int main()
{
	//test01();
	test02();
	return 0;
}
Published 38 original articles · won praise 13 · views 4313

Guess you like

Origin blog.csdn.net/YanWenCheng_/article/details/104053028