Advertisement
nq1s788

сет

Jan 18th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     /// set -- элементы без повторений
  10.     /// быстро добавлять элемент -- O(log n) ||| n = 1000 logn примерно 7, n == 100000 около 20, 100000000 около 30
  11.     /// быстро удалять O(log n)
  12.     /// за O(1) (очень быстро) находить минимум в сете
  13.  
  14.     set<int> a;
  15.     a.insert(5); //добавление элемента
  16.     a.insert(3);
  17.     a.insert(7);
  18.     a.insert(3);
  19.     for (auto e : a) { //выводим сет циклом
  20.         cout << e << ' ';
  21.     }
  22.     cout << '\n';
  23.     cout << *a.begin(); //берем первый элемент -- минимум
  24.     cout << '\n';
  25.     a.erase(3); //удаление элемента
  26.     for (auto e : a) {
  27.         cout << e << ' ';
  28.     }
  29.     cout << '\n';
  30.     if (a.count(6)) {
  31.         cout << "есть 6\n";
  32.     }
  33.     if (a.count(5)) {
  34.         cout << "есть 5\n";
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement