往常的動(dòng)態(tài)數(shù)組寫(xiě)法都是vector 感覺(jué)有點(diǎn)比美觀 現(xiàn)在提供一種新的動(dòng)態(tài)數(shù)組寫(xiě)法 int *a = new int[n]; 感覺(jué)這種直接操作內(nèi)存的寫(xiě)法比較直觀 不過(guò)二維數(shù)組研究出來(lái),遇到再說(shuō)
#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ù)組本身就支持動(dòng)態(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 << ' '; }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注