Advertisement
gluk47

seminar 2023-01-27

Jan 27th, 2023
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. class C {
  2. public:
  3.     C() {};  // конструктор по умолчанию
  4.  
  5.     // C(const C&) = default;
  6.     // C(const C&) = delete;
  7.  
  8.     C(const C& c)  // конструктор копирования
  9.     : Value(c.Value)
  10.     , Other(c.Other)
  11.     {
  12.         cout << "c(copy)\n";
  13.     }
  14.  
  15.     ~C() {
  16.         delete Other;
  17.     }
  18.  
  19.     explicit C(int Value)
  20.     : Value(Value)  // список инициализации конструктора / construction initializer list
  21.     , Other(new C{string(i)})
  22.     { cout << "c(int)\n"; }
  23.  
  24.     string as_string() const {
  25.         return to_string(Value);
  26.     }
  27.  
  28.     bool operator<(const C& rhs) const {
  29.         // Value = rhs.value → не скомпилируется
  30.         // this — const C*;
  31.         return Value < rhs.Value;
  32.     }
  33.  
  34.     bool operator< (int other) const {
  35.         return Value < other;
  36.     }
  37.  
  38.     const C& operator=(const C& other) {
  39.         Value = other.Value;
  40.         return *this;
  41.     }
  42.     // C a, b, c;
  43.     // a = b = c;
  44.  
  45.     operator bool() const {
  46.         return Value != 0;
  47.     }
  48.     // if(obj) {...}
  49.  
  50.     int operator() (int x, int y) {
  51.         return Value + x + y;
  52.     }
  53.     // C obj(1);
  54.     // obj(2, 3); // 1 + 2 + 3
  55.  
  56. private:
  57.     C(const string& s)
  58.     : Value(FromString<int>(s))
  59.     { cout << "c(string)\n"; }
  60.    
  61.     int Value = 0;
  62.     C* Other = nullptr;
  63. };
  64.  
  65. void Print(const C& arg) {
  66.     cout << arg.as_string();
  67. }
  68.  
  69. int i = (void*)0;
  70. int i = NULL; // скомпилируется
  71. Print(NULL);  // Print(C{0});
  72.  
  73. C obj(42);
  74. obj < 43;
  75. f(obj);
  76.  
  77. C copy = obj;
  78. C copy(obj);
  79. C copy{obj};
  80.  
  81. C obj('!');
  82. C obj(false);
  83. C obj(4ull);
  84. C obj;  // compilation error (no matching constructor)
  85. C obj("15");  // compilation error (private)
  86.  
  87.  
  88. vector<int> v;
  89. v.push_back(addr);
  90. v.back();
  91. v.pop_back();
  92.  
  93.  
  94. std::stack<int> v;
  95. v.push() / v.pop() / v.top()
  96. LIFO = last in first out
  97.  
  98.  
  99. std::queue<int> q;
  100. v.push() / v.pop()
  101. f.front() / v.back()
  102. FIFO = first in first out
  103.  
  104.  
  105. std::priority_queue<int> pq;
  106. std::priority_queue<int, vector<int>, .....> pq;
  107. v.push() / v.pop()
  108. v.top()
  109.  
  110.  
  111. std::deque<int> d
  112. d.push_back() / push_front() — O(1) amortized
  113. d.pop_back() / pop_front() — O(1) amortized
  114. d[i] — O(1)
  115. // https://en.cppreference.com/w/cpp/container/deque
  116.  
  117. int C[40] = {};
  118.  
  119. #include <array>
  120. std::array<int, 40> arr = {1, 2};
  121. arr.size();
  122. arr[i];
  123.  
  124. mov адрес-памяти → регистр-процессора
  125.  
  126. C[i] = *(&C + sizeof(C[0]) * i)
  127.  
  128. vector<int> v; // v[i]: O(1)
  129.  
  130. std::list<int> lst;
  131. {
  132.     int value;
  133.     node* prev;
  134.     node* next;
  135. }
  136. lst.push_back(e); // v[i]: O(n)
  137.  
  138. std::unordered_set<int> us;  // хэш-таблица
  139. // хэширует.
  140. us.emplace(i) — O(1)  // amortized
  141. us.contains(i) — O(1)
  142.  
  143. std::set<int> s;  // дерево поиска
  144. s.emplace(i) — O(log n)
  145. s.contains(i) — O(log n)
  146.  
  147.  
  148. class String {
  149. public:
  150.     String(const char* str) {
  151.         data = new char[strlen(str)]; // O(n)
  152.         size_t i = 0;
  153.         for (char* c = str; *c != 0; ++c, ++i) {  // O(n)
  154.             data[i] = *c;
  155.         }
  156.      
  157.         while (*str) {
  158.             data[i++] = str++;
  159.         }
  160.     }
  161.  
  162.     String& operator+= (char c) {
  163.         data.reserve(size() * 2);
  164.         data[size()] = c;
  165.         return *this;
  166.     }
  167.  
  168.     size_t capacity() {
  169.         return size_allocated;
  170.     }
  171.  
  172.     size_t size() {
  173.         return size_used;
  174.     }
  175.  
  176.     // String s = "abc";
  177.     // s.resize(40); -> s[35] = '1';
  178.     void resize(size_t new_size) {
  179.         if (size > size_used) {
  180.             reserve(new_size);
  181.             fill(data + size_used, data + size, 0);
  182.             size_used = new_size;
  183.         } else {
  184.             size_used = new_size;
  185.         }
  186.     }
  187.  
  188.     // s.reserve(40); нельзя: s.at(35)
  189.     // можно: s += '1'
  190.     void reserve(size_t new_size) {
  191.         if (new_size > size_allocated) {
  192.             char* new_data = new char[new_size];
  193.             std::copy(data, data + size_used, new_data);
  194.             delete[] data;
  195.             data = new_data;
  196.             size_allocated = new_size;
  197.         }
  198.     }
  199.  
  200. private:
  201.     char* data = nullptr;
  202.     size_t size_used = 0;
  203.     size_t size_allocated = 0;
  204. };
  205.  
  206. String obj("abc");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement