我已經嘗試使用指針重新排列數組中的數字,但我實際上已經實現了它,但是我以一個可怕的代碼結束了,我知道可能有更好的方法來做到這一點,弄明白了。我只想在我的代碼上輸入內容。 另外我知道我的整數的名字不是最好的,所以請不要評論我。使用指針重新排列數組中的數字
#include <iostream>
using namespace std;
void Fill(int a[], int b) {
for (int i = 0; i < b; i++)
*(a + i) = rand() % 100;
}
void Print(int a[], int b) {
for (int i = 0; i < b; i++)
cout << *(a + i) << " ";
}
void swap(int a[], int b, int c[]) {
for (int i = 0; i < b; i++) {
*(c + (b - i - 1)) = *(a + i);
}
for (int i = 0; i < b; i++) {
*(a + i) = *(c + i);
}
for (int i = 0; i < b; i++) {
cout << *(a + i) << " ";
}
}
int main() {
int hello1[10], goodbye[10];
Fill(hello1, 10);
Print(hello1, 10);
cout << endl;
swap(hello1, 10, goodbye);
cin.get();
cin.get();
return 0;
}
你爲什麼不只是使用指數,這是同樣的事情更短的形式! –