【c++】sort 与 qsort 的简单使用

版权声明:来自 T2777 ,请访问 https://blog.csdn.net/T2777 一起交流进步吧 https://blog.csdn.net/T2777/article/details/87884326

先给出程序:

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

//降序排列函数
bool desc(int a, int b);
//qsort()对int的排序函数,在algorithm中定义
int qcmp1(const void*a, const void*b)
{
	return *(int *)a - *(int *)b;
}
//qsort降序函数是
int qcmp2(const void*a, const void*b)
{
	return *(int *)b - *(int *)a;
}
int main()
{
	int a[5] = { 3,4,2,1,5 };
	cout << "排序前的数组为: " << endl;
	for (int i = 0; i < 5; i++)
		cout << a[i] <<" ";
	cout << "\n";
	cout << "sort(a,a+5)后的数组为(不写第三个参数,默认为升序排列): " << endl;
	sort(a,a+5);
	for (int i = 0; i < 5; i++)
		cout << a[i] << " ";
	cout << endl;
	cout << "按照bool desc(a,b) 降序排列的数组经过 sort(a,a+5,desc) 为:" << endl;
	sort(a,a+5,desc);
	for (int i = 0; i < 5; i++)
		cout << a[i] << " ";
	cout << endl;
	cout << "使用标准库中的方法,升序为 less<data_type>(),升序数组为: " << endl;
	sort(a, a + 5, less<int>());
	for (int i = 0; i < 5; i++)
		cout << a[i] << " ";
	cout << endl;
	//greater<int>()的使用在<functional>头文件中定义,less则不在此文件中定义
	cout << "使用标准库中的方法,降序为 greater<data_type>(),降序数组为: " << endl;
	sort(a, a + 5,greater<int>());
	for (int i = 0; i < 5; i++)
		cout << a[i] << " ";
	cout << endl;
	int b[] = { 6,8,7,10,9 };
	cout << "使用qsort()快排得到对数组b的升序排序是:" << endl;
	qsort(b,5,sizeof(b[0]),qcmp1);
	for (int j = 0; j < 5; j++)
		cout << b[j] << " ";
	cout << endl;
	cout << "使用qsort()快排得到对数组b的降序排序是:" << endl;
	qsort(b, 5, sizeof(b[0]), qcmp2);
	for (int j = 0; j < 5; j++)
		cout << b[j] << " ";
	cout << endl;
	return 0;
}

bool desc(int a, int b)
{
	return a > b;
}

输出结果:

c++ 中的 sort()中可以有3个参数,当只有2个参数时,默认是升序,降序要自定义返回值为bool型的排序函数使用,它的速度一般要比qsort()更快一些,sort函数是标准库中的函数,用已知开始与结束的地址进行排序,可以用于任何容器,sort使用时,必须注明using namespace std; 或者直接打上std:: sort, 还得加上 <algorithm> , 排序的类型不限于整数,sort 是 qsort 的升级版,能用sort尽量使用sort,而不要用qsort 。

qsort 的使用过于复杂,定义:void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 

各参数分别为:1 待排序的数组的首地址,2待排序数组元素的数量,3各元素所占用空间的大小,4指向排序函数的指针  这些定义和上面程序中的例子一一对应。

这里对字符数组的用法进行一下总结:

字符数组的定义:

char s[6] = {'h','e','l','l','o','\0'};

char s[ ] = "hello"

char s[6] = "hello"

由于字符串的默认最后一位都是 '\0', 所以在定义时要增加一位存储,按照下面两种写法时不必加上 \0 空字符,编译器在编译时会自动在字符串末尾加上一个,实际上下面两个是字符串,而第一个是字符数组,因为字符串必须以 \0 结尾,而字符数组则不必须,但加上 \0 使得第一个也是一个字符串,可以用字符串的 strlen 等一系列操作。字符串strlen()不包括末尾的 ‘\0’,但是sizeof()是包括的。

 代码为:

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

//降序排列函数,用char和int得到的是相同的结果,最好用char吧
bool desc(char a, char b);

int main()
{
	char s1[5] = "dacb";
	//strlen 不包括字符串末尾的0
	cout << "strlen(s1)为: "<< strlen(s1) << endl;
	cout << "sizeof(s1)为: " << sizeof(s1) << endl;
	sort(s1, s1 + 4);
	cout << "sort(s1,s1+4)升序为(注意不能是s1+5会报错): " << endl;
	cout << s1 << endl;
	sort(s1, s1 + 4, desc);
	cout << "sort(s1,s1+4)降序(自定义desc)为: " << endl;
	cout << s1 << endl;
	sort(s1, s1 + 4, less<char>());
	cout << "sort(s1, s1 + 4, less<char>())升序可以得到: " << endl;
	cout << s1 << endl;
	sort(s1, s1 + 4, greater<char>());
	cout << "sort(s1, s1 + 4, greater<char>())降序可以得到: " << endl;
	cout << s1 << endl;

	//对字符数组与字符串的一些实验
	char s2[6] = {'h','e','l','l','o','\0'};
	cout <<"打印 s2 (char s2[6] = {'h','e','l','l','o',0})(0不能加单引号):  "<< s2 << endl;
	cout << "strlen(s2)为: "<< strlen(s2) << endl;
	char s3[3] = { 'h','e','l'};
	cout << "打印s3 ( char s3[3] = { 'h','e','l'} 没有0就不是字符串 ): " << s3 << endl;
	return 0;
}

bool desc(char a, char b)
{
	return a > b;
}

结果截图:

同时可以知道 用sizeof(s1)可以知道数组的实际长度。 

猜你喜欢

转载自blog.csdn.net/T2777/article/details/87884326