在这里,我们将看到如何使用C ++中的STL函数反转数组。因此,如果数组类似于A = [10,20,30,40,50,60],则输出将为B = [60,50,40,30,20,10]。相反,我们reverse()
在头文件<algorithm>中有一个称为present的函数。代码如下-
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int n = sizeof(arr) / sizeof(arr[0]); cout << "反向排列数组: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; reverse(arr, arr + n); cout << "\nArray after reverse: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; }
输出结果
反向排列数组: 10 20 30 40 50 60 Array after reverse: 60 50 40 30 20 10