从b站上黑马程序员的C++课里学到的元素逆置
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int arr[5] = { 1,2,3,4,5 }; 6 int start = 0; //起始下标 7 int end = sizeof(arr) / sizeof(arr[0]) - 1; //结束下标 = 元素个数 - 1 8 for (int i = 0; i < 5; i++) 9 { 10 cout << arr[i] << " "; 11 } 12 cout << endl; 13 while (start < end) //走到中间就不用换了 14 { //起始下标与结束下标的元素互换 15 int temp = arr[start]; 16 arr[start] = arr[end]; 17 arr[end] = temp; 18 start++; //下标更新 19 end--; 20 } 21 for (int i = 0; i < 5; i++) 22 { 23 cout << arr[i] << " "; 24 } 25 return 0; 26 }
标签:arr,下标,一维,int,元素,数组,sizeof,逆置 From: https://www.cnblogs.com/romantichuaner/p/17571072.html