c++逆置数组reserve
时间: 2023-05-17 22:05:54 浏览: 141
Sure, here's a sample code for reversing an array in C:
```
#include <stdio.h>
void reverse(int arr[], int n) {
int temp;
for (int i = 0; i < n/2; i++) {
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
reverse(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
This code uses a simple swapping technique to reverse the elements of an array. The `reverse` function takes an array and its size as input, and swaps the first and last elements, then the second and second-to-last elements, and so on, until the entire array is reversed. The `main` function initializes an array, calls the `reverse` function, and then prints the reversed array.
阅读全文