C/C++常用接口实现【持续更新】
目录
python和c/c++中负数取模的差异
// python中的负数取余其实行为和C++,JAVA中的不一样
// python中
// -2 % 6 = 4,相当于 (6 - (2 % 6)) % 6
// -12 % 4 = 0 相当于 (4 - (12 % 4)) % 4
// c/c++中
// -1 % 2 = -1
内存接口
判断大小端
- 一般方法
bool IsSmallEndian()
{
int num = 1;
char *p = (char *)#
if (*p == 1) {
printf("小端\n");
return true;
}
return false;
}
- union
bool IsSmallEndian()
{
union Un {
char c;
int i;
} u;
u.i = 1;
if (u.c == 1) {
printf("小端\n");
return false;
}
printf("大端\n");
return true;
}
数组类
二分搜索
- 正常二分搜索
int search(int* nums, int numsSize, int target)
{
int mid;
int left = 0, right = numsSize - 1;
while (left <= right) {
mid = (right - left) / 2 + left;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
int searchInsert(int* nums, int numsSize, int target)
{
int mid;
int left = 0, right = numsSize - 1;
while (left <= right) {
mid = (right - left) / 2 + left;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
- 在排序数组中查找元素的第一个和最后一个位置
/* 求数组中第一个与target相等的元素 */
int GetLeft(int* nums, int numsSize, int target)
{
int mid;
int left = 0, right = numsSize - 1;
while (left <= right) {
mid = (right - left) / 2 + left;
if (nums[mid] >= target) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left < numsSize ? (nums[left] == target ? left : -1) : -1;
}
/* 求数组中最后一个与target相等的元素 */
int GetRight(int* nums, int numsSize, int target)
{
int mid;
int left = 0, right = numsSize - 1;
while (left <= right) {
mid = (right - left) / 2 + left;
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return right >= 0 ? (nums[right] == target ? right : -1) : -1;
}
求数组最大值
int GetMax(int nums[], int left, int right)
{
if (left >= right) return nums[left];
int mid = left + ((right - left) >> 1);
int leftMax = GetMax(nums, left, mid);
int rightMax = GetMax(nums, mid + 1, right);
return fmax(leftMax, rightMax);
}
交换两个数
- 直接交互
void Swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
- 在数组中交换
void SwapNums(int *nums, int l, int r)
{
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
在区间[left, right]中取随机数
// stdlib.h头文件中有宏 #define RAND_MAX 0x7fff
// rand产生一个0-0x7fff的随机数,即最大是32767的一个数
int randId = rand() % (right - left + 1) + left;
排序数组
比较器——比较数组中的元素
// 对1维数组排序: int nums[3] = {3, 1, 2};
int Cmp(void *a, void *b)
{
return *((int *)a) - *((int *)b);
}
// 对2维数组排序, 以第1个排: int nums[3][2] = {[3, 4], [2, 3], [5, 1]};
int CmpByFirst(void *a, void *b)
{
int *nA = *(int **)a;
int *nB = *(int **)b;
return nA[0] - nB[0];
}
// 对2维数组排序, 以第2个排: int nums[3][2] = {[3, 4], [2, 3], [5, 1]};
int CmpBySecond(void *a, void *b)
{
int *nA = *(int **)a;
int *nB = *(int **)b;
return nA[1] - nB[1];
}
归并和快速排序
- 归并排序
void Merge(int nums[], int left, int mid, int right)
{
int *help = malloc(sizeof(int) * (right - left + 1));
int leftId = left, rightId = mid + 1;
int id = 0;
while (leftId <= mid && rightId <= right) {
if (nums[leftId] <= nums[rightId]) {
help[id] = nums[leftId++];
} else {
help[id] = nums[rightId++];
}
id++;
}
if (leftId <= mid) {
help[id++] = nums[leftId++];
}
// memcpy(&help[id], &nums[leftId], sizeof(int) * (mid - leftId + 1));
if (rightId < right) {
help[id++] = nums[rightId++];
}
// memcpy(&help[id], &nums[rightId], sizeof(int) * (right - rightId + 1));
for (id = 0; id < right - left + 1; id++) {
nums[left + id] = help[id];
}
// memcpy(&nums[left], &help[0], sizeof(int) * (right - left + 1));
free(help);
}
void Process(int nums[], int left, int right)
{
if (left >= right) {
return;
}
int mid = left + ((right - left) >> 1);
Process(nums, left, mid);
Process(nums, mid + 1, right);
Merge(nums, left, mid, right);
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
Process(nums, 0, numsSize - 1);
*returnSize = numsSize;
return nums;
}
- 快排
int g_pos[2];
void Swap(int *nums, int l, int r)
{
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
}
/*
1. 找到k所在的pos
2. k的左边都比k小
3. k的右边都比k大
*/
void FindPos(int *nums, int left, int right, int k)
{
int less = left - 1;
int more = right + 1;
int idx = left;
while (idx > less && idx < more) {
if (nums[idx] < k) {
Swap(nums, less + 1, idx);
less++;
idx++;
} else if (nums[idx] > k) {
Swap(nums, idx, more - 1);
more--;
} else {
idx++;
}
}
g_pos[0] = less;
g_pos[1] = more;
}
void Process(int *nums, int left, int right)
{
if (left >= right) {
return;
}
int randIdx = rand() % (right - left + 1) + left;
Swap(nums, randIdx, right);
FindPos(nums, left, right, nums[right]);
Process(nums, left, g_pos[0]);
Process(nums, g_pos[1], right);
}
int* sortArray(int* nums, int numsSize, int* returnSize)
{
Process(nums, 0, numsSize - 1);
*returnSize = numsSize;
return nums;
}
打印数组
void PrintfNums(int nums[], int numsSize)
{
printf("......PrintfNums......\n");
for (int i = 0; i < numsSize; ++i) {
printf("%d ", nums[i]);
}
printf("\n");
}
hash表——UT_HASH
typedef struct HashTable {
int key;
int value;
UT_hash_handle hh;
} HashTable;
HashTable *g_hash;
HashTable* Find(int n)
{
HashTable *tmp = NULL;
HASH_FIND_INT(g_hash, &n, tmp);
return tmp;
}
void AddNode(int n)
{
HashTable *tmp = Find(n);
if (tmp == NULL) {
tmp = malloc(sizeof(HashTable));
tmp->key = n;
tmp->value = 1;
HASH_ADD_INT(g_hash, key, tmp);
} else {
tmp->value++;
}
}
void FreeMem()
{
HashTable *cur, *next;
HASH_ITER(hh, g_hash, cur, next) {
HASH_DEL(g_hash, cur);
free(cur);
}
}