Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool hasNextOrganic = currentOffset.NextOrganic != Config.SerpResults.end();
- bool hasRemainingWidth = currentOffset.RemainingWidth != 0;
- bool hasDelimeterOnNextOrganic = currentOffset.NextOrganic->GetType() == Blender::Delimiter;
- if (!hasNextOrganic || !hasRemainingWidth ||
- hasDelimeterOnNextOrganic) {
- -----
- https://leetcode.com/problems/integer-to-roman/
- 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.
- The number 27 is written as XXVII, which is XX + V + II.
- Roman numerals are usually written largest to smallest from left to right.
- However, the numeral for four is not IIII. Instead, the number four is written as IV.
- 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.
- There are six instances where subtraction is used:
- I can be placed before V (5) and X (10) to make 4 and 9.
- X can be placed before L (50) and C (100) to make 40 and 90.
- C can be placed before D (500) and M (1000) to make 400 and 900.
- Given an integer, convert it to a roman numeral.
- Symbol Value
- I 1
- V 5
- X 10
- L 50
- C 100
- D 500
- M 1000
- III -> IV
- VIII -> IX
- XXX -> XL
- Input: num = 1994
- Output: "MCMXCIV"
- Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
- string to_roman(int decimal) {
- const string digits = "IVXLCDM";
- int len = 0;
- int pow = 1;
- for (int decimal_copy = decimal; decimal_copy; decimal_copy /= 10) {
- ++len;
- pow *= 10;
- }
- string roman;
- while (decimal) {
- int cur_n = decimal / pow;
- const size_t digit_i = (len - 1) * 2;
- const char one = digits[digit_i];
- if (cur_n == 4 || cur_n == 9) {
- roman += one;
- roman += digits[digit_i + (cur_n > 5)];
- }
- if (cur_n >= 5) {
- roman += digits[digit_i + 1];
- cur_n -= 5;
- }
- for (size_t i = 0; i < cur_n; i++) {
- roman.push_back(one);
- }
- decimal %= pow;
- pow /= 10;
- --len;
- }
- return roman;
- }
- ---
- template <typename T>
- class UniquePtr {
- public:
- T& operator*() {
- return *ptr;
- }
- const T& operator*() const {
- return *ptr;
- }
- /*
- bool operator&& (const T& value) const {
- return ptr && static_cast<bool>(value);
- }
- */
- operator bool() const {
- return ptr;
- }
- T* ptr = nullptr;
- };
- int main() {
- UniquePtr<int> p;
- cout << (p.ptr && *p.ptr) << "\n";
- cout << (p && *p) << "\n";
- }
- ----
- template <typename T>
- struct ComparatorByIndex {
- bool operator() (size_t l, size_t r) const {
- return data[l] < data[r];
- }
- const vector<T>& data;
- };
- template <typename T, typename Transform = std::identity>
- ostream& PrintContainer(ostream& str, const vector<T>& data, const Transform& indexer = Transform{}) {
- str << "[";
- size_t i = 0;
- if (i < data.size())
- str << data[indexer(i++)];
- while (i < data.size())
- str << ", " << data[indexer(i++)];
- return str << "]";
- }
- template <typename T>
- ostream& operator<< (ostream& str, const vector<T>& data) {
- return PrintContainer(str, data);
- }
- int main() {
- const vector<int> shuffled = {2, 4, 3, 1};
- vector<size_t> indexes(shuffled.size());
- std::iota(indexes.begin(), indexes.end(), 0);
- // indexes: {0, 1, 2, 3}
- // lambda capture
- [] // нельзя использовать локальные переменные <----
- [&indexes] // по ссылке
- [indexes] // по значению
- [&shuffled, indexes]
- [&] // доступны все локальные переменные по ссылке <----
- [=] // захватить все переменные по значению
- [this]
- [indexes, &]
- [b = indexes.begin(), e = indexes.end()]
- (int l){
- sort(b, e);
- cout << l << "\n";
- };
- ComparatorByIndex<int> compare{shuffled};
- sort(indexes.begin(), indexes.end(), compare);
- cout << "shuffled: " << shuffled << "\n"
- << " indexes: " << indexes << "\n"
- << " sorted: ";
- PrintContainer(cout, shuffled, [&indexes](size_t i){ return indexes[i]; }) << "\n";
- }
- [2, 4, 3, 1]
- [3, 0, 2, 1]
- [1, 2, 3, 4]
- -----
- 1) Не использовать comparator
- bool operator< (const Student& l, const Student& r) {.....}
- bool operator<=> (const Student& l, const Student& r) {.....}
- 2) bool comparator(const Student& l, const Student& r) {....}
- 3) внутри sort_students объявить лямбда-функцию
- [](const Student& l, const Student& r){ return false; }
- 4) Объявить функтор
- struct Comparator {
- bool operator() (const Student& l, const Student& r) const {}
- bool ascending = true
- };
- void sort_students(const vector<Student>& students) {
- // 3
- auto comparator = [](const Student& l, const Student& r){....};
- // 4 Comparator comparator
- std::sort(students.begin(), students.end(), comparator);
- // comparator(l, r)
- set<Student, Comparator> sorted;
- set<Student, Comparator> sorted {Comparator{true}};
- set<Student, decltype(comparator)> sorted;
- }
- ----
- template <typename T>
- class TMatrix {
- public:
- struct TLineView {
- T& operator[](size_t column) {
- return data_[line * columns_ + column];
- }
- T* data_;
- size_t line, columns_;
- };
- TLineView& operator[](size_t line) {
- return TLineView{data_, line, columns_};
- }
- // c++23
- T& operator[](size_t line, size_t columns) {
- return data_[line * columns_ + column];
- }
- const T& operator[](size_t line, size_t columns) const {
- return data_[line * columns_ + column];
- }
- // m[1, 0];
- T& operator()(size_t line, size_t columns) {
- return data_[line * columns_ + column];
- } // m(1, 0)
- private:
- size_t columns_ = 0;
- size_t size_ = 0;
- T* data_;
- };
- TMatrix<int> m;
- m[1][0];
- ----
- template <typename T>
- struct TVector {
- TVector(size_t n)
- : data(new T[n])
- {}
- friend ostream& operator<< (ostream& str, const TVector<T>& v);
- // {можно писать тело тут}
- TVector& operator+= (const std::vector<T>& v) {
- if (size != v.size())
- return *this;
- for (....)
- ....
- return *this;
- }
- explicit operator std::string() const {
- // return [1,2,3,4]
- }
- explicit operator bool() const {
- return size != 0 && data;
- }
- bool operator!() const {
- return size == 0 || data == nullptr;
- } // !!a
- TVector& operator=(const TVector& rhs) = default;
- TVector& operator=(const TVector& rhs) = delete;
- TVector& operator=(TVector&& rhs) {
- if (&rhs == this)
- return *this;
- (*this) = rhs;
- rhs.data = nullptr;
- rhs.size = 0;
- return *this;
- }
- TVector& operator=(const TVector& rhs) {
- if (&rhs == this)
- return *this;
- delete[] data;
- data = rhs.data; // бага
- size = rhs.size;
- return *this;
- }
- private:
- // TVector& operator=(const TVector& rhs);
- size_t size;
- T* data = nullptr;
- };
- TVector<int> a = 4;
- TVector<int> a {4};
- TVector<int> a (4);
- auto serialized = static_cast<std::string>(a);
- TVector<int> b = serialized;
- if (a) {....}
- -----
- vector<int> a, b;
- int64_t operator* (int a, int b);
- int64_t operator* (const vector<int>& a, const vector<int>& b) {
- int64_t prod = 0;
- if (a.size() != b.size())
- throw std::runtime_error();
- for (size_t i = 0; i < a.size(); ++i)
- prod += static_cast<int64_t>(a[i]) * b[i];
- return prod;
- }
- vector<int> operator+ (const vector<int>& a, const vector<int>& b) {}
- vector<int>& operator+= (vector<int>& a, const vector<int>& b) {
- return a;
- }
- std::strong_order operator<=>(const vector<int>& a, const vector<int>& b) {
- return -1; // a < b
- return 0; // a == b
- return +1; // a > b
- }
- bool operator== (const vector<int>& a, const vector<int>& b) {
- if (a.size() != b.size())
- return false;
- (a <=> b) == 0;
- }
- bool operator< (const vector<int>& a, const vector<int>& b) {
- ....
- for (i = 0; i < a.size(); i++)
- if (a[i] != b[i])
- return a[i] < b[i];
- ....
- }
- bool operator> (const vector<int>& a, const vector<int>& b) {
- return (b < a);
- }
- vector<vector<int>> matrix = {a, b};
- sort(matrix.begin(), matrix.end());
- cout << (a * b) << "\n";
- a = a + b;
- (a += b) += c;
- &: a & b; &b; int&;
- 1 << 3 -> 0b1000 = 8
- -----
- template <typename T>
- struct TVector {
- TVector(size_t n)
- : data(new T[n])
- {}
- friend ostream& operator<< (ostream& str, const TVector<T>& v);
- // {можно писать тело тут}
- private:
- size_t size;
- T* data = nullptr;
- };
- ostream& operator<< (ostream& str, const TVector<T>& v) {
- str << "[";
- size_t i = 0;
- if (i < v.size)
- str << v.data[i++];
- while (i < v.size)
- str << ", " << v.data[i++];
- return str << "]";
- }
- struct Iterator {
- // postfix: i++
- Iterator operator++(int) const;
- // prefix ++i
- Iterator& operator++() const;
- // auto j = -i;
- Iterator operator-() const {}
- // i - j
- Iterator operator-(const Iterator&) const {}
- };
- template <typename T>
- std::ostream& operator<< (std::ostream& str, const vector<T>& v) {
- str << "[";
- size_t i = 0;
- if (i < v.size())
- str << v[i++];
- while (i < v.size())
- str << ", " << v[i++];
- return str << "]";
- }
- vector<int> v = {1,2,3};
- cout << v << "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement