往常的動態數組寫法都是vector 感覺有點比美觀 現在提供一種新的動態數組寫法 int *a = new int[n]; 感覺這種直接操作內存的寫法比較直觀 不過二維數組研究出來,遇到再說
#include <bits/stdc++.h>using namespace std;int main(){ int n; scanf("%d", &n); int *a = new int[n]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); for (int i = 0; i < n; i++) { cout << a[i] << ' '; }}并且,更為神奇的,結構體數組本身就支持動態,如以下代碼
#include <bits/stdc++.h>using namespace std;struct S{ int t; bool Operator < (S s) { return t < s.t; }};int main(){ int n; scanf("%d", &n); S s[n]; for (int i = 0; i < n; i++) { scanf("%d", &s[i].t); } sort(s, s + n); for (int i = 0; i < n; i++) { cout << s[i].t << ' '; }}新聞熱點
疑難解答