Advertisement
gluk47

seminar 2023-01-20

Jan 20th, 2023
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. // sem 2
  2. //Hello world
  3.  
  4. // vector<int> v = {1, 2, 3};
  5. // cout << v << "\n";
  6.  
  7. struct string {
  8.     size_t capacity;
  9.  
  10.     union {
  11.         struct {
  12.             size_t size;
  13.             char* data;
  14.         } long_data;
  15.  
  16.         char[16] short_data;
  17.     } data;
  18. };
  19.  
  20. struct SelfCounting {
  21.     SelfCounting() {
  22.         data = 0;
  23.         cout << "SelfCounting()\n";
  24.     }
  25.  
  26.     SelfCounting(int d) {
  27.         cout << "SelfCounting(int)\n";
  28.         data = d;
  29.     }
  30.  
  31.     SelfCounting(const SelfCounting& rhs) {
  32.         cout << "SelfCounting(copy)\n";
  33.         data = rhs.data;
  34.     }
  35.  
  36.     int data;
  37. };
  38.  
  39. ostream& operator<< (ostream& str, const SelfCounting& sc) {
  40.     return str << sc.data;
  41. }
  42.  
  43. SelfCounting by_val(SelfCounting obj) {
  44.     cout << obj << "\n";
  45.     return obj;
  46. }
  47.  
  48. SelfCounting by_ref(const SelfCounting& obj) {
  49.     cout << obj << "\n";
  50.     return obj;
  51. }
  52.  
  53. // itoa(45) -> string "45"
  54. // itoa(12345) -> "12345"
  55. // itoa(-443) -> "-443"
  56. // '0' + 5 == '5'
  57. string itoa(int value) {
  58.     std::string result = "";
  59.     result.reserve(15);
  60.     bool minus_flag = false;
  61.     if (value < 0) {
  62.         minus_flag = true;
  63.         value *= -1;
  64.     }
  65.     while (value > 0) {
  66.         result += '0' + value % 10;
  67.         value /= 10;
  68.     }
  69.     // return {result.rbegin(), result.rend()}
  70.     if (minus_flag) {
  71.         result += "-";
  72.     }
  73.     std::reverse(result.begin(), result.end());
  74.     result.shrink_to_fit();
  75.     return result;
  76. }
  77.  
  78. template <typename T>
  79. ostream& operator<< (ostream& os, const vector<T>& v) {
  80.     // [1, 2, 3]
  81.     os << "[";
  82.     for (std::size_t i = 0; i < v.size() - 1; ++i) {
  83.         os << v[i] << ", ";
  84.     }
  85.     if (!v.empty()) {
  86.         os << v[v.size() - 1];
  87.     }
  88.     return os << "]";
  89. }
  90.  
  91. void dangling() {
  92.     int c_v[3] = {1,2,3}
  93.  
  94.     vector<int> v = {1, 2, 3};
  95.     v.reserve(4);
  96.  
  97.     int& f = v[0];
  98.     f = 4;
  99.     cout << v;  // 4, 2, 3
  100.  
  101.     v.push_back(5);
  102.     cout << f;  //<
  103.  
  104.     int* p = &v[0];
  105.     cout << *p;
  106.  
  107.     for (i = v.begin(); i != v.end(); ) {
  108.         if (*i == 42)
  109.             i = v.erase(i);
  110.         else
  111.             i++;
  112.     }
  113. }
  114.  
  115.  
  116. void matrix_ops() {
  117.     vector<vector<int>> matrix = {
  118.         {1, 2, 3},
  119.         {4, 5, 6},
  120.     };
  121.  
  122.     cout << matrix << "\n";
  123.     for (const auto& row : matrix) {
  124.         for (int e : row)
  125.             cout << e << " ";
  126.         cout << "\n";
  127.     }
  128.  
  129.     for (size_t i = 0; i < matrix.size(); ++i) {
  130.         for (size_t j = 0; j < matrix[i].size(); ++j)
  131.             cout << matrix[i][j] << " ";
  132.         cout << "\n";
  133.     }
  134. }
  135.  
  136. void charmap() {
  137.     for (int i = 0; i < 256; i++)
  138.         cout << i << ": " << static_cast<char>(i) << "\n";
  139.    
  140.     char c;
  141.     'A' <= c && c <= 'Z';
  142. }
  143.  
  144. void charmap_pretty() {
  145.     using std::hex, std::cout;
  146.     const size_t cols = 16;
  147.     cout << "  |";
  148.     for (size_t  c = 0; c < cols; c++)
  149.         cout << hex << c << " ";
  150.     cout << "\n--+-";
  151.     for (size_t c = 0; c < cols; c++)
  152.         cout << "--";
  153.  
  154.     for (unsigned char c = 0; c < 255; c++) {
  155.         if ((c & 0xF) == 0) {
  156.             cout << "\n" << hex << (c >> 4) << " | ";
  157.         }
  158.         cout << c << " ";
  159.     }
  160.     cout << "\n";
  161. }
  162.  
  163. struct Date {
  164.     int year;
  165.     int month;
  166.     uint8_t day;
  167. };
  168.  
  169. void print_date(const Date& d) {
  170.     cout << d.year << "-" << d.month << "-" << d.day << "\n";
  171. }
  172.  
  173. string& concat(const string& l, const string& r) {
  174.     string result = l;
  175.     result += r;
  176.     return result;
  177. }
  178.  
  179. void structs() {
  180.     Date ub;
  181.     print_date(ub);
  182.     print_date({2023});
  183.     print_date({2023, 1});
  184.     print_date({2023, 1, 20});
  185.     print_date({.month = 1});
  186.     print_date({.month = 1, .day = 20});
  187. }
  188.  
  189. bool is_even(int i) {
  190.     return i % 2 == 0;
  191. }
  192.  
  193. bool date_year_less(const Date& l, const Date& r) {
  194.     return l.year < r.year;
  195. }
  196.  
  197. void evens(const vector<int>& data) {
  198.     size_t count = std::count_if(data.begin(), data.end(), is_even);
  199.  
  200.     cout << count << "\n";
  201. }
  202.  
  203. int do_pref_sum(int i) {
  204.     static int p = 0;
  205.     p += i;
  206.     return p;
  207. }
  208.  
  209. void prefix_sum(const vector<int> v) {
  210.     vector<int> prefs;
  211.     prefs.resize(v.size());
  212.     std::transform(v.rbegin(), v.rend(), prefs.rbegin(), do_pref_sum);
  213.  
  214.     cout << v << "\n" << prefs << "\n";
  215. }
  216.  
  217. int main(int, const char**) {
  218.     prefix_sum({1, 2, 3});
  219.  
  220.     return 0;
  221.  
  222.     vector<Date> dates = {
  223.         {2023, 1, 1},
  224.         {2022, 1, 10},
  225.     };
  226.  
  227.     cout << dates << "\n";
  228.     std::sort(dates.begin(), dates.end(), date_year_less);
  229.     cout << dates << "\n";
  230.  
  231.     return 0;
  232. }
  233.  
  234.  
  235. // vector<vector<int>> m;
  236. // cout << m << "\n";
  237. // [[1,2,3],[4,5,6]]
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement