往常的動態(tài)數(shù)組寫法都是vector 感覺有點比美觀 現(xiàn)在提供一種新的動態(tài)數(shù)組寫法 int *a = new int[n]; 感覺這種直接操作內(nèi)存的寫法比較直觀 不過二維數(shù)組研究出來,遇到再說
#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] << ' '; }}并且,更為神奇的,結(jié)構(gòu)體數(shù)組本身就支持動態(tà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 << ' '; }}新聞熱點
疑難解答