LeetCode解题----C语言基本库函数的使用
在做题的时候,借助标准库中的函数,可以使我们更加专注于算法和题目本身。库函数的熟练使用,有助于我们聚焦于思路,快速解决问题。因此,很有必要熟练库函数了。以下库函数是本人在做题中经常遇到使用到的库函数,以下介绍按使用频率从大到小排序。
1.快速排序qsort
1.1 qsort函数原型
函数原型 | void qsort(voidbase, size_t num, size_t width, int( * compare)(const void,const void*)) |
库函数 | qsort |
包含头文件 | stdlib.h |
---- | ---- |
函数输入参数 | 参数描述 |
void *base | 待排序数组首地址 |
size_t num | 数组中带排序元素数量 |
size_t width | 各元素的占用空间大小 |
int( * compare)(const void,const void) | 指向函数的指针,用于却带排序的顺序,传入的是地址 |
函数原型 | compare( (void *) & elem1, (void *) & elem2) |
compare函数的返回值 | 描述 |
<0 | elem1将被排在ele2之前 |
=0 | 位置不变 |
>0 | elem1将被排在elem2之后 |
exam: | |
从小到大排序 | int comp(const void *a, const void *b) {return *(int*)a-*(int*)b;} |
从大到小排序 | int comp(const void *a, const void *b) {return *(int*)b-*(int*)a;} |
1.2 qsort函数应用实例
1.2.1 对一维数组进行排序(从小到大排序):
#include
#include
int compare(const void *a, const void *b)
{
return *(int *)a - *(int *)b; //从小到大排序
}
int main()
{
/* 数组输入准备 */
int *nums;
int numsSize;
scanf("%d", &numsSize);
nums = (int *)malloc(numsSize * sizeof(int)); // 动态申请数组
for (int i = 0; i < numsSize; i++)
{
scanf("%d", &nums[i]); // 数组元素赋值
printf("%d\t", nums[i]); //打印排序前数组
}
/* 数组调用qsort进行排序 */
qsort(nums, numsSize, sizeof(int), compare);
/* 打印排序后数组 */
printf("\nby sorted:\n");
for (int i = 0; i < numsSize; i++)
{
printf("%d\t", nums[i]);
}
return 0;
}
1.2.2 对二维数组进行排序(依据第一维对数组进行从小到大排序):
#include
#include
int compare(const void *a, const void *b)
{
int *ap = *(int **)a;
int *bp = *(int **)b;
// 如果第一维相等,则依据第二维从小到大排序
if (ap[0] == bp[0])
return ap[1] - bp[1];
else
return ap[0] - bp[0]; // 第一维不相等,依据第一维从小到大排序
}
void printfnums(int **nums, int numsSize, int numsColSize)
{
for (int i = 0; i < numsSize; i++)
{
for (int j = 0; j < numsColSize; j++)
{
printf("%d ", nums[i][j]);
}
printf("\n");
}
}
int main()
{
int inputnums[][2] = {{1, 54}, {-4, 89}, {45, 6545}};
int numsColSize = sizeof(inputnums[0]) / sizeof(int);
int numsSize = sizeof(inputnums) / sizeof(inputnums[0]);
int **nums = (int **)malloc(numsSize * sizeof(int *));
for (int i = 0; i < numsSize; i++)
{
nums[i] = (int *)malloc(numsColSize * sizeof(int));
}
for (int i = 0; i < numsSize; i++)
{
for (int j = 0; j < numsColSize; j++)
{
nums[i][j] = inputnums[i][j];
}
}
printfnums(nums, numsSize, numsColSize);
qsort(nums, numsSize, sizeof(nums[0]), compare);
printfnums(nums, numsSize, numsColSize);
return 0;
}
1.2.3 对结构体进行排序:
#include
#include
#include
typedef struct point
{
int x;
int y;
double distance;
};
int compare(const void *a, const void *b)
{
struct point *x = (struct point *)a;
struct point *y = (struct point *)b;
if (x->distance != y->distance)
{
return x->distance - y->distance;
}
else if (x->x != y->x)
{
return x->x - y->x;
}
else //if(x->y != x->y)
{
return x->y - y->y;
}
}
void pointsprintf(struct point *points, int numsSize)
{
for (int i = 0; i < numsSize; i++)
{
printf("distance :%0.2f x :%d y :%d\n", points[i].distance, points[i].x, points[i].y);
}
}
int main()
{
int nums[][2] = {{1, 3},
{-1, -1},
{56, 4},
{-1, -2}};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int numsColSize = sizeof(nums[0]) / sizeof(int);
struct point points[numsSize];
//int *points = (int *)malloc(numsSize * sizeof(struct point));
for (int i = 0; i < numsSize; i++)
{
points[i].x = nums[i][0];
points[i].y = nums[i][1];
points[i].distance = sqrt(points[i].x * points[i].x + points[i].y * points[i].y);
}
pointsprintf(points, numsSize);
qsort(points, numsSize, sizeof(struct point), compare);
printf("\n");
pointsprintf(points, numsSize);
return 0;
}
1.2.4 对字符串进行排序:
compare 函数:
int compare(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}
调用:
qsort(products, productsSize, sizeof(char **), compare);
2. 整数与字符串的相互转换itoa/atoi
2.1 整数转字符串itoa
函数原型 | char* itoa(int value,char*string,int radix) |
库函数 | itoa |
包含头文件 | stdlib.h |
---- | ---- |
函数输入参数 | 参数描述 |
int value | 要转换的整数 |
char*string | 转换后的字符串 |
int radix | 转换进制数,如2,8,10,16 进制等 |
返回值 | 指向转换后的字符串指针 |
#include
#include // itoa 所在头文件
int main()
{
int num = 3;
char str[10];
// num:待转换的整数;str:转换后的字符串;10:转换进制数
itoa(num, str, 10);
printf("%s", str);
return 0;
}
2.2 字符串转整数atoi
函数原型 | int atoi(const char *str) |
库函数 | atoi |
包含头文件 | stdlib.h |
---- | ---- |
函数输入参数 | 参数描述 |
const char *str | 待转换字符串 |
返回值 | 转换后的整数(int型) |
#include
#include // atoi 所在库函数
int main()
{
char s[] = "22";
// s:待转换字符串;函数返回值:转换后的整数int
int num = atoi(s);
printf("%d", num);
return 0;
}
3. 字符串比较strcmp()/strncmp()
3.1 strcmp()
函数原型 | int strcmp(const char *str1, const char *str2) |
库函数 | strcmp |
包含头文件 | string.h |
---- | ---- |
函数输入参数 | 参数描述 |
const char *str1 | 待比较字符串1的指针 |
const char *str2 | 待比较字符串2的指针 |
返回值 | 如果返回值小于 0,则表示 str1 小于 str2。 如果返回值大于 0,则表示 str1 大于 str2。 如果返回值等于 0,则表示 str1 等于 str2。 |
#include
#include
int main()
{
char s1[] = "ab";
char s2[] = "aB";
if (strcmp(s1, s2) == 0)
{
printf("s1 == s2");
}
else if (strcmp(s1, s2) < 0)
{
printf("s1 < s2");
}
else if (strcmp(s1, s2) > 0)
{
printf("s1 > s2");
}
return 0;
}
3.2 strncmp()
函数原型 | int strncmp(const char *str1, const char *str2, size_t n) |
库函数 | strncmp |
包含头文件 | string.h |
---- | ---- |
函数输入参数 | 参数描述 |
const char *str1 | 待比较字符串1的指针 |
const char *str2 | 待比较字符串2的指针 |
size_t n | 最多比较前 n 个字节 |
返回值 | 如果返回值小于 0,则表示 str1 小于 str2。 如果返回值大于 0,则表示 str1 大于 str2。 如果返回值等于 0,则表示 str1 等于 str2。 |
4. 字符串复制strcpy()/strncpy()
4.1 strcpy()
函数原型 | char *strcpy(char *dest, const char *src) |
库函数 | strcpy |
包含头文件 | string.h |
---- | ---- |
函数输入参数 | 参数描述 |
char *dest | 指向用于存储复制内容的目标数组 |
const char *src | 要复制的字符串 |
返回值 | 该函数返回一个指向最终的目标字符串 dest 的指针 |
#include
#include
int main()
{
char dest[20] = "ha ";
char src[] = "haha";
strcpy(dest, src);
printf("%s", dest);
return 0;
}
4.2 strncpy()
函数原型 | char *strncpy(char *dest, const char *src, size_t n) |
库函数 | strncpy |
包含头文件 | string.h |
---- | ---- |
函数输入参数 | 参数描述 |
char *dest | 指向用于存储复制内容的目标数组 |
const char *src | 要复制的字符串 |
size_t n | 要从源中复制的字符数 |
返回值 | 该函数返回一个指向最终的目标字符串 dest 的指针 |
更多库函数的使用,可参考如下位置:
C语言库函数