/* Osnovne podatkovne strukture v C++: - vector (seznam, v pythonu: list [], v Javi: ArrayList) - isto velja za string - dostop: v[i] O(1) - brisanje, vstavljanje na začetku (ali vmes): O(n) - brisanje, vstavljanje na koncu: O(1) - deque - dostop: v[i] O(1), ampak dražji kot vector - brisanje, vstavljanje na začetku: O(1) - brisanje, vstavljanje na koncu: O(1) - brisanje, vstavljanje vmes: O(n) - list - dostop: v[i] O(n) - brisanje, vstavljanje na začetku: O(1) - brisanje, vstavljanje na koncu: O(1) - brisanje, vstavljanje vmes: O(1) Splošne podatkovne strukture: - (dynamic) array ---> C++: vector - stack / sklad LIFO ----> C++: stack, vector, (deque, list) - queue / vrsta FIFO ----> C++: queue, deque - vrsta s prioriteto ----> C++: priority_queue (pop_heap, push_heap) - drevesa ----> C++: set, map (ordered) - hashmap ----> C++: unordered_set, unordered_map - linked list ----> C++: list */ #include #include #include #include #include #include using namespace std; int main() { int n; cin >> n; vector v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v.begin(), v.end()); // O(n log(n)) --- skoraj linearno sort(v.begin(), v.begin()+2); // [ , ) for (int i = 0; i < n; ++i) { cout << v[i]; if (i != n-1) cout << ' '; } cout << '\n'; /* vector b(1000000, 4); // zamenjaj z deque in opazuj razliko v hitrosti for (int i = 0; i < 1000000; ++i) { if (i % 5 == 0 && i % 2 != 0) { b.push_back(i); // O(1) } } cout << b.size() << endl; cout << "------------------\n"; while (!b.empty()) { cout << b[0] << endl; b.erase(b.begin()); // pozor: O(n) } */ set s = {"abc", "ghi", "def"}; s.insert("x"); s.insert("abc"); // log(n) s.erase("ghi"); // log(n) for (const string& x : s) { cout << x << ", "; } cout << endl; cout << s.size() << endl; cout << s.count("x") << endl; // log(n) map m; m["Ljubljana"] = 0; m["Maribor"] = 10; m["Kranj"] = 6; // .insert log(n) for (const auto& [k, v] : m) { cout << k << ": " << v << endl; } cout << m.count("Koper") << endl; // log(n) return 0; }