不同长度的字符串数组排序

#include<iostream>
#include<string.h>
#include<algorithm>

int myStrCmp(const void *lhs, const void  *rhs)
{
	const char *lc = *(char **)lhs;            //数组元素是char *,而qsort中第四个函数原型是int (*) (const void *, const void *),所以传进来的参数实际是指向指针的指针
	const char *rc = *(char **)rhs;          //所以必须使用 *(char **)作一次解引用,直接const char *lc = (char *)lhs是错误的

	int i = strcmp(lc, rc);

	std::cout << lc << "   "<< rc << std::endl;
	std::cout << i << std::endl;

	return i;
}


int main()
{
	char *a[5] = {"absdc","vscd","aac","ddddddddddddd","tssre"};

	qsort(a, 5, sizeof(char *), myStrCmp);
	

	for(int i = 0; i < 5; i++)
	{
		std::cout << a[i] << std::endl;
	}
	
	return 0;
}


你可能感兴趣的:(不同长度的字符串数组排序)