while(0),, array[0]

本文详细解析了do-while(0)宏定义的使用场景及其与普通do-while循环的区别,通过实例展示了其在调试和输出信息方面的独特优势。此外,文章还探讨了typedef结构的定义方式,并提供了有关类型定义和初始化的实用技巧。

 1.

 do{ /

 code... ; /

 code...; /

}while(0)

 

Why do we use "do while(0)" : This macro define can check if the defined symbal are excueted as a whole. here is an example.

if we define BST_DEBUG(fmt...)

 

#define BST_DEBUG(fmt...)  printf("BST USI Debug %s /n", ##fmt); /

                            printf("File: %s, Line:%d /n", __FILE__, __LINE__)

 

instead of

 

#define BST_DEBUG(fmt...)  do{ /

                            printf("BST USI Debug %s /n", ##fmt); /

                            printf("File: %s, Line:%d /n", __FILE__, __LINE__); /

                            }while(0)

. We will confrant with the problem if use it like this

 

if(a>0)

    BST_DEBUG("Only one line output.../n");

 

So we can find that the latter define can output all string correctly.

-------------------------------------------------------------------------------------

2.

typedef struct mytype

{

    int type;

    int len;

    int array[0];

}

 

-----------------------------------------

3

 

 

 

 

以下是对上述 C++ 代码实现的快速排序双指针法详细执行过程的分析: #### 代码框架 ```cpp #include <iostream> using namespace std; void QuickSort(int *array, int low, int high) { if (low >= high) { return; } int i = low; int j = high; int key = array[low]; while (i < j) { while (array[j] >= key && i < j) { j--; } array[i] = array[j]; while (array[i] <= key && i < j) { i++; } array[j] = array[i]; } array[i] = key; QuickSort(array, low, i - 1); QuickSort(array, i + 1, high); } int main() { int array[] = {49, 38, 65, 97, 76, 13, 27, 49}; int length = sizeof(array) / sizeof(*array); cout << "原始序列:"; for (int i = 0; i < length; i++) { cout << array[i] << " "; } cout << endl; QuickSort(array, 0, length - 1); cout << "快排序列:"; for (int i = 0; i < length; i++) { cout << array[i] << " "; } return 0; } ``` #### 详细执行过程 1. **函数调用与初始设定**:在 `main` 函数中,定义了一个数组 `array`,并调用 `QuickSort` 函数对其进行排序。`QuickSort` 函数接收数组指针 `array` 以及排序范围的左右边界 `low` 和 `high`。初始时,`low = 0`,`high = 7`,即对整个数组进行排序。 2. **基准元素选择**:选取 `array[low]` 作为基准元素 `key`,这里 `key = 49`。同时初始化两个指针 `i` 和 `j`,分别指向数组的起始位置和结束位置,即 `i = 0`,`j = 7`。 3. **双指针移动与元素交换**: - **右指针 `j` 移动**:从右向左移动指针 `j`,只要 `array[j] >= key` 且 `i < j`,就继续向左移动。当 `array[j] < key` 时停止移动。在这个例子中,`j` 从 7 开始向左移动,直到 `j = 6` 时,`array[6] = 27 < 49`,此时停止移动。 - **元素覆盖**:将 `array[j]` 的值赋给 `array[i]`,即 `array[0] = 27`。 - **左指针 `i` 移动**:从左向右移动指针 `i`,只要 `array[i] <= key` 且 `i < j`,就继续向右移动。当 `array[i] > key` 时停止移动。在这个例子中,`i` 从 0 开始向右移动,直到 `i = 2` 时,`array[2] = 65 > 49`,此时停止移动。 - **元素覆盖**:将 `array[i]` 的值赋给 `array[j]`,即 `array[6] = 65`。 4. **指针相遇与基准元素归位**:重复上述双指针移动和元素覆盖的过程,直到 `i` 和 `j` 相遇。当 `i == j` 时,将基准元素 `key` 放到 `array[i]` 的位置,即 `array[2] = 49`。此时,基准元素左边的元素都小于等于它,右边的元素都大于等于它。 5. **递归排序子数组**:以基准元素为分界点,将数组分为左右两个子数组,分别对左右子数组进行递归排序。即调用 `QuickSort(array, low, i - 1)` 对左子数组 `[0, 1]` 进行排序,调用 `QuickSort(array, i + 1, high)` 对右子数组 `[3, 7]` 进行排序。 6. **递归终止条件**:当 `low >= high` 时,说明子数组中只有一个元素或没有元素,不需要再进行排序,递归终止。 #### 示例执行过程 原始序列:`49 38 65 97 76 13 27 49` 第一次划分后:`27 38 49 97 76 13 65 49` 递归排序左子数组 `[27 38]` 和右子数组 `[97 76 13 65 49]`,最终得到排序好的序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值