Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class C {
- public:
- C() {}; // конструктор по умолчанию
- // C(const C&) = default;
- // C(const C&) = delete;
- C(const C& c) // конструктор копирования
- : Value(c.Value)
- , Other(c.Other)
- {
- cout << "c(copy)\n";
- }
- ~C() {
- delete Other;
- }
- explicit C(int Value)
- : Value(Value) // список инициализации конструктора / construction initializer list
- , Other(new C{string(i)})
- { cout << "c(int)\n"; }
- string as_string() const {
- return to_string(Value);
- }
- bool operator<(const C& rhs) const {
- // Value = rhs.value → не скомпилируется
- // this — const C*;
- return Value < rhs.Value;
- }
- bool operator< (int other) const {
- return Value < other;
- }
- const C& operator=(const C& other) {
- Value = other.Value;
- return *this;
- }
- // C a, b, c;
- // a = b = c;
- operator bool() const {
- return Value != 0;
- }
- // if(obj) {...}
- int operator() (int x, int y) {
- return Value + x + y;
- }
- // C obj(1);
- // obj(2, 3); // 1 + 2 + 3
- private:
- C(const string& s)
- : Value(FromString<int>(s))
- { cout << "c(string)\n"; }
- int Value = 0;
- C* Other = nullptr;
- };
- void Print(const C& arg) {
- cout << arg.as_string();
- }
- int i = (void*)0;
- int i = NULL; // скомпилируется
- Print(NULL); // Print(C{0});
- C obj(42);
- obj < 43;
- f(obj);
- C copy = obj;
- C copy(obj);
- C copy{obj};
- C obj('!');
- C obj(false);
- C obj(4ull);
- C obj; // compilation error (no matching constructor)
- C obj("15"); // compilation error (private)
- vector<int> v;
- v.push_back(addr);
- v.back();
- v.pop_back();
- std::stack<int> v;
- v.push() / v.pop() / v.top()
- LIFO = last in first out
- std::queue<int> q;
- v.push() / v.pop()
- f.front() / v.back()
- FIFO = first in first out
- std::priority_queue<int> pq;
- std::priority_queue<int, vector<int>, .....> pq;
- v.push() / v.pop()
- v.top()
- std::deque<int> d
- d.push_back() / push_front() — O(1) amortized
- d.pop_back() / pop_front() — O(1) amortized
- d[i] — O(1)
- // https://en.cppreference.com/w/cpp/container/deque
- int C[40] = {};
- #include <array>
- std::array<int, 40> arr = {1, 2};
- arr.size();
- arr[i];
- mov адрес-памяти → регистр-процессора
- C[i] = *(&C + sizeof(C[0]) * i)
- vector<int> v; // v[i]: O(1)
- std::list<int> lst;
- {
- int value;
- node* prev;
- node* next;
- }
- lst.push_back(e); // v[i]: O(n)
- std::unordered_set<int> us; // хэш-таблица
- // хэширует.
- us.emplace(i) — O(1) // amortized
- us.contains(i) — O(1)
- std::set<int> s; // дерево поиска
- s.emplace(i) — O(log n)
- s.contains(i) — O(log n)
- class String {
- public:
- String(const char* str) {
- data = new char[strlen(str)]; // O(n)
- size_t i = 0;
- for (char* c = str; *c != 0; ++c, ++i) { // O(n)
- data[i] = *c;
- }
- while (*str) {
- data[i++] = str++;
- }
- }
- String& operator+= (char c) {
- data.reserve(size() * 2);
- data[size()] = c;
- return *this;
- }
- size_t capacity() {
- return size_allocated;
- }
- size_t size() {
- return size_used;
- }
- // String s = "abc";
- // s.resize(40); -> s[35] = '1';
- void resize(size_t new_size) {
- if (size > size_used) {
- reserve(new_size);
- fill(data + size_used, data + size, 0);
- size_used = new_size;
- } else {
- size_used = new_size;
- }
- }
- // s.reserve(40); нельзя: s.at(35)
- // можно: s += '1'
- void reserve(size_t new_size) {
- if (new_size > size_allocated) {
- char* new_data = new char[new_size];
- std::copy(data, data + size_used, new_data);
- delete[] data;
- data = new_data;
- size_allocated = new_size;
- }
- }
- private:
- char* data = nullptr;
- size_t size_used = 0;
- size_t size_allocated = 0;
- };
- String obj("abc");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement