Advertisement
gluk47

seminar 2023-02-17

Feb 17th, 2023
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.74 KB | None | 0 0
  1. bool hasNextOrganic = currentOffset.NextOrganic != Config.SerpResults.end();
  2. bool hasRemainingWidth = currentOffset.RemainingWidth != 0;
  3. bool hasDelimeterOnNextOrganic = currentOffset.NextOrganic->GetType() == Blender::Delimiter;
  4. if (!hasNextOrganic || !hasRemainingWidth ||
  5.     hasDelimeterOnNextOrganic) {
  6.  
  7. -----
  8.  
  9. https://leetcode.com/problems/integer-to-roman/
  10.  
  11. For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II.
  12.  The number 27 is written as XXVII, which is XX + V + II.
  13.  
  14. Roman numerals are usually written largest to smallest from left to right.
  15.  However, the numeral for four is not IIII. Instead, the number four is written as IV.
  16.    Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX.
  17.    There are six instances where subtraction is used:
  18.  
  19.    I can be placed before V (5) and X (10) to make 4 and 9.
  20.    X can be placed before L (50) and C (100) to make 40 and 90.
  21.    C can be placed before D (500) and M (1000) to make 400 and 900.
  22.  
  23. Given an integer, convert it to a roman numeral.
  24.      
  25. Symbol       Value
  26. I             1
  27. V             5
  28. X             10
  29. L             50
  30. C             100
  31. D             500
  32. M             1000
  33.  
  34. III -> IV
  35. VIII -> IX
  36. XXX -> XL
  37.  
  38. Input: num = 1994
  39. Output: "MCMXCIV"
  40. Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
  41.  
  42. string to_roman(int decimal) {
  43.     const string digits = "IVXLCDM";
  44.     int len = 0;
  45.     int pow = 1;
  46.    for (int decimal_copy = decimal; decimal_copy; decimal_copy /= 10) {
  47.         ++len;
  48.         pow *= 10;
  49.    }
  50.     string roman;
  51.     while (decimal) {
  52.         int cur_n = decimal / pow;
  53.         const size_t digit_i = (len - 1) * 2;
  54.         const char one = digits[digit_i];
  55.         if (cur_n == 4 || cur_n == 9) {
  56.             roman += one;
  57.             roman += digits[digit_i + (cur_n > 5)];
  58.        }
  59.         if (cur_n >= 5) {
  60.             roman += digits[digit_i + 1];
  61.             cur_n -= 5;
  62.        }
  63.        
  64.        for (size_t i = 0; i < cur_n; i++) {
  65.            roman.push_back(one);
  66.        }
  67.        
  68.         decimal %= pow;
  69.         pow /= 10;
  70.         --len;
  71.    }
  72.    
  73.    return roman;
  74. }
  75.  
  76. ---
  77.  
  78. template <typename T>
  79. class UniquePtr {
  80. public:
  81.     T& operator*() {
  82.         return *ptr;
  83.    }
  84.     const T& operator*() const {
  85.         return *ptr;
  86.    }
  87.  /*
  88.     bool operator&& (const T& value) const {
  89.         return ptr && static_cast<bool>(value);
  90.    }
  91.  */
  92.     operator bool() const {
  93.         return ptr;
  94.    }
  95.  
  96.     T* ptr = nullptr;
  97. };
  98.  
  99. int main() {
  100.     UniquePtr<int> p;
  101.     cout << (p.ptr && *p.ptr) << "\n";
  102.     cout << (p && *p) << "\n";
  103. }
  104.  
  105. ----
  106.  
  107. template <typename T>
  108. struct ComparatorByIndex {
  109.     bool operator() (size_t l, size_t r) const {
  110.         return data[l] < data[r];
  111.     }
  112.     const vector<T>& data;
  113. };
  114.  
  115. template <typename T, typename Transform = std::identity>
  116. ostream& PrintContainer(ostream& str, const vector<T>& data, const Transform& indexer = Transform{}) {
  117.     str << "[";
  118.     size_t i = 0;
  119.     if (i < data.size())
  120.         str << data[indexer(i++)];
  121.     while (i < data.size())
  122.         str << ", " << data[indexer(i++)];
  123.     return str << "]";
  124. }
  125.  
  126. template <typename T>
  127. ostream& operator<< (ostream& str, const vector<T>& data) {
  128.     return PrintContainer(str, data);
  129. }
  130.  
  131. int main() {
  132.     const vector<int> shuffled = {2, 4, 3, 1};
  133.  
  134.     vector<size_t> indexes(shuffled.size());
  135.     std::iota(indexes.begin(), indexes.end(), 0);
  136.     // indexes: {0, 1, 2, 3}
  137.    
  138.     // lambda capture
  139.     []  // нельзя использовать локальные переменные  <----
  140.     [&indexes]  // по ссылке
  141.     [indexes]  // по значению
  142.     [&shuffled, indexes]
  143.     [&]  // доступны все локальные переменные по ссылке  <----
  144.     [=]  // захватить все переменные по значению
  145.     [this]
  146.     [indexes, &]
  147.     [b = indexes.begin(), e = indexes.end()]
  148.    (int l){
  149.         sort(b, e);
  150.         cout << l << "\n";
  151.    };
  152.  
  153.     ComparatorByIndex<int> compare{shuffled};
  154.     sort(indexes.begin(), indexes.end(), compare);
  155.     cout << "shuffled: " << shuffled << "\n"
  156.          << " indexes: " << indexes << "\n"
  157.          << "  sorted: ";
  158.     PrintContainer(cout, shuffled, [&indexes](size_t i){ return indexes[i]; }) << "\n";
  159. }
  160.  
  161. [2, 4, 3, 1]
  162. [3, 0, 2, 1]
  163. [1, 2, 3, 4]
  164. -----
  165.  
  166. 1) Не использовать comparator
  167. bool operator< (const Student& l, const Student& r) {.....}
  168. bool operator<=> (const Student& l, const Student& r) {.....}
  169.  
  170. 2) bool comparator(const Student& l, const Student& r) {....}
  171.  
  172. 3) внутри sort_students объявить лямбда-функцию
  173. [](const Student& l, const Student& r){ return false; }
  174.  
  175. 4) Объявить функтор
  176. struct Comparator {
  177.     bool operator() (const Student& l, const Student& r) const {}
  178.    
  179.    bool ascending = true
  180. };
  181.  
  182. void sort_students(const vector<Student>& students) {
  183.     // 3
  184.     auto comparator = [](const Student& l, const Student& r){....};
  185.  
  186.     // 4 Comparator comparator
  187.     std::sort(students.begin(), students.end(), comparator);
  188.     // comparator(l, r)
  189.  
  190.     set<Student, Comparator> sorted;
  191.     set<Student, Comparator> sorted {Comparator{true}};
  192.     set<Student, decltype(comparator)> sorted;
  193. }
  194. ----
  195.  
  196. template <typename T>
  197. class TMatrix {
  198. public:
  199.     struct TLineView {
  200.         T& operator[](size_t column) {
  201.             return data_[line * columns_ + column];
  202.        }
  203.         T* data_;
  204.         size_t line, columns_;
  205.    };
  206.     TLineView& operator[](size_t line) {
  207.         return TLineView{data_, line, columns_};
  208.    }
  209.  
  210.     // c++23
  211.    T& operator[](size_t line, size_t columns) {
  212.        return data_[line * columns_ + column];
  213.    }
  214.    const T& operator[](size_t line, size_t columns) const {
  215.        return data_[line * columns_ + column];
  216.    }
  217.     // m[1, 0];
  218.  
  219.     T& operator()(size_t line, size_t columns) {
  220.        return data_[line * columns_ + column];
  221.    } // m(1, 0)
  222.  
  223. private:
  224.     size_t columns_ = 0;
  225.     size_t size_ = 0;
  226.     T* data_;
  227. };
  228.  
  229. TMatrix<int> m;
  230. m[1][0];
  231. ----
  232.  
  233. template <typename T>
  234. struct TVector {
  235.     TVector(size_t n)
  236.    : data(new T[n])
  237.    {}
  238.  
  239.     friend ostream& operator<< (ostream& str, const TVector<T>& v);
  240.     // {можно писать тело тут}
  241.  
  242.     TVector& operator+= (const std::vector<T>& v) {
  243.         if (size != v.size())
  244.             return *this;
  245.         for (....)
  246.             ....
  247.         return *this;
  248.    }
  249.  
  250.     explicit operator std::string() const {
  251.         // return [1,2,3,4]
  252.    }
  253.  
  254.    explicit operator bool() const {
  255.         return size != 0 && data;
  256.    }
  257.  
  258.     bool operator!() const {
  259.         return size == 0 || data == nullptr;
  260.    }  // !!a
  261.  
  262.     TVector& operator=(const TVector& rhs) = default;
  263.     TVector& operator=(const TVector& rhs) = delete;
  264.  
  265.     TVector& operator=(TVector&& rhs) {
  266.         if (&rhs == this)
  267.             return *this;
  268.      
  269.         (*this) = rhs;
  270.         rhs.data = nullptr;
  271.         rhs.size = 0;
  272.         return *this;
  273.    }
  274.  
  275.    TVector& operator=(const TVector& rhs) {
  276.         if (&rhs == this)
  277.             return *this;
  278.  
  279.         delete[] data;
  280.         data = rhs.data;  // бага
  281.         size = rhs.size;
  282.         return *this;
  283.    }
  284.  
  285. private:
  286.     // TVector& operator=(const TVector& rhs);
  287.  
  288.     size_t size;
  289.     T* data = nullptr;
  290. };
  291.  
  292. TVector<int> a = 4;
  293. TVector<int> a {4};
  294. TVector<int> a (4);
  295.  
  296. auto serialized = static_cast<std::string>(a);
  297. TVector<int> b = serialized;
  298. if (a) {....}
  299. -----
  300.  
  301. vector<int> a, b;
  302.  
  303. int64_t operator* (int a, int b);
  304.  
  305. int64_t operator* (const vector<int>& a, const vector<int>& b) {
  306.     int64_t prod = 0;
  307.     if (a.size() != b.size())
  308.         throw std::runtime_error();
  309.     for (size_t i = 0; i < a.size(); ++i)
  310.         prod += static_cast<int64_t>(a[i]) * b[i];
  311.     return prod;
  312. }
  313.  
  314. vector<int> operator+ (const vector<int>& a, const vector<int>& b) {}
  315. vector<int>& operator+= (vector<int>& a, const vector<int>& b) {
  316.     return a;
  317. }
  318.  
  319. std::strong_order operator<=>(const vector<int>& a, const vector<int>& b) {
  320.     return -1; // a < b
  321.     return 0;  // a == b
  322.     return +1; // a > b
  323. }
  324.  
  325. bool operator== (const vector<int>& a, const vector<int>& b) {
  326.     if (a.size() != b.size())
  327.         return false;
  328.    (a <=> b) == 0;
  329. }
  330.  
  331. bool operator< (const vector<int>& a, const vector<int>& b) {
  332.     ....
  333.     for (i = 0; i < a.size(); i++) 
  334.         if (a[i] != b[i])
  335.            return a[i] < b[i];
  336.     ....
  337. }
  338.  
  339. bool operator> (const vector<int>& a, const vector<int>& b) {
  340.     return (b < a);
  341. }
  342.  
  343. vector<vector<int>> matrix = {a, b};
  344. sort(matrix.begin(), matrix.end());
  345.  
  346. cout << (a * b) << "\n";
  347.  
  348. a = a + b;
  349. (a += b) += c;
  350.  
  351. &: a & b; &b; int&;
  352. 1 << 3 -> 0b1000 = 8
  353. -----
  354.  
  355. template <typename T>
  356. struct TVector {
  357.     TVector(size_t n)
  358.    : data(new T[n])
  359.    {}
  360.  
  361.     friend ostream& operator<< (ostream& str, const TVector<T>& v);
  362.     // {можно писать тело тут}
  363.  
  364. private:
  365.     size_t size;
  366.     T* data = nullptr;
  367. };
  368.  
  369. ostream& operator<< (ostream& str, const TVector<T>& v) {
  370.     str << "[";
  371.     size_t i = 0;
  372.     if (i < v.size)
  373.         str << v.data[i++];
  374.     while (i < v.size)
  375.         str << ", " << v.data[i++];
  376.     return str << "]";
  377. }
  378.  
  379. struct Iterator {
  380.     // postfix: i++
  381.     Iterator operator++(int) const;
  382.    // prefix ++i
  383.    Iterator& operator++() const;
  384.    
  385.    // auto j = -i;
  386.    Iterator operator-() const {}
  387.    // i - j
  388.    Iterator operator-(const Iterator&) const {}
  389. };
  390.  
  391. template <typename T>
  392. std::ostream& operator<< (std::ostream& str, const vector<T>& v) {
  393.     str << "[";
  394.     size_t i = 0;
  395.     if (i < v.size())
  396.         str << v[i++];
  397.     while (i < v.size())
  398.         str << ", " << v[i++];
  399.     return str << "]";
  400. }
  401.  
  402. vector<int> v = {1,2,3};
  403. cout << v << "\n";
  404.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement