Advertisement
CaminhoneiroHell

stl cheat sheet

Oct 6th, 2021
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 42.27 KB | None | 0 0
  1. // SymbolMangling.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3. // To avoid symbol mangling use extern "C" when declaring your function
  4. #include "pch.h"
  5. #include "stdio.h"
  6. #include <iostream>
  7. #include <typeinfo>
  8. #include <cstdio>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <cmath>
  12. #include <array>
  13. #include <numeric>
  14. #include <map>
  15. #include <ostream>
  16. #include <memory>
  17. #include <valarray>
  18. #include <string>
  19. #include <cassert>
  20. #include <stdexcept>
  21. #include <exception>
  22.  
  23. using std::cin;
  24. using std::cout;
  25. using std::endl;
  26. using std::string;
  27. using std::abs;
  28. using std::vector;
  29. using std::array;
  30. using std::to_string;
  31. std::ostream operator<<(const std::ostream& lhs, const string& cs);
  32.  
  33.  
  34. //*****************DATA STRUCTURES***************//
  35.  
  36. //***Vector
  37. //
  38. //**Popular methods
  39. //
  40. //push_back()               0(1) = constant
  41. //[] brackets operator      0(1) = constant
  42. //size()                    0(1) = constant
  43.  
  44. void Vector()
  45. {
  46.     vector<int> v;                  //v{}
  47.     cout << v.size() << "\n";       //output 0
  48.     v.push_back(20);            //v = {20}
  49.     v.push_back(10);            //v = {20,10}
  50.     v.push_back(10);            //v = {20,10, 10}
  51.     cout << v[1];                   //output 10
  52.     cout << v.size();               //output 3
  53. };
  54.  
  55.  
  56. //***Set
  57. //
  58. //**Popular methods
  59. //
  60. //insert()              0(logn)
  61. //find()                0(logn)
  62. //size()                0(1) = constant
  63.  
  64. #include <set>
  65. using std::set;
  66. void Set()
  67. {
  68.     set<int> s;                                 //s{}
  69.     cout << s.size();                           //outputs 0
  70.     s.insert(20);                               //s = {20}
  71.     s.insert(10);                               //s = {10, 20}
  72.     s.insert(10);                               //s = {10, 20}
  73.     auto it = s.find(10);                       //it is an iterator that points to 10
  74.     cout << (it != s.end() ? " FOUND" : " ");   //outputs FOUND
  75.     cout << s.size();                           //output 2
  76. }
  77.  
  78.  
  79. //***Unordered Set
  80. //
  81. //**Popular methods
  82. //
  83. //insert()              0(1)
  84. //find()                0(1)
  85. //size()                0(1) = constant
  86.  
  87.  
  88. #include <unordered_set>
  89. using std::unordered_set;
  90. void UnorderedSet()
  91. {
  92.     unordered_set<int> s;                           //s{}
  93.     cout << s.size();                               //outputs 0
  94.     s.insert(20);                               //s = {20}
  95.     s.insert(10);                               //s = {10, 20}  //Could be in any order//
  96.     s.insert(10);                               //s = {10, 20}
  97.     auto it = s.find(10);                       //it is an iterator that points to 10
  98.     cout << (it != s.end() ? " FOUND" : " ");       //outputs FOUND
  99.     cout << s.size();                               //output 2
  100. }
  101.  
  102.  
  103. //***Map
  104. //
  105. //**Popular methods
  106. //
  107. //insert()                  0(logn) insert using make_pair
  108. //find()                    0(logn) returns pair
  109. //[] brackets operations    0(logn) returns ref to value
  110. //size()                    0(1)
  111.  
  112. using std::map;
  113. using std::make_pair;
  114.  
  115. void Map()
  116. {
  117.     map<int, int> m;                            //m={}
  118.     cout << m.size();                           //outputs 0
  119.     m.insert(make_pair(20, 1));                 //m={(20,1)}
  120.     m.insert(make_pair(10, 1));                 //m={(10,1), (20,1)}
  121.     m[10]++;                                    //m={(10,2), (20,1)}
  122.     auto it = m.find(10);                   //it is an iterator that points to (10, 2)
  123.     cout << (it != m.end() ? it->second : 0);   //outputs 2
  124.     auto it2 = m.find(20);              //it is an iterator that points to (20, 1)
  125.     cout << (it2 != m.end() ? it2->first : 0);  //outputs 20
  126.     cout << m.size();                           //outputs 2    
  127. }
  128.  
  129.  
  130. //***Unordered Map
  131. //
  132. //**Popular methods
  133. //
  134. //insert()                  0(1)    insert using make_pair
  135. //find()                    0(1)    returns pair
  136. //[] brackets operations    0(1)    returns ref to value
  137. //size()                    0(1)
  138.  
  139. #include<unordered_map>
  140. using std::unordered_map;
  141. void UnorderedMap()
  142. {
  143.     unordered_map<int, int> m;                  //m={}
  144.     cout << m.size();                           //outputs 0
  145.     m.insert(make_pair(20, 1));                 //m={(20,1)}
  146.     m.insert(make_pair(10, 1));                 //m={(10,1), (20,1)}    //Could in any order
  147.     m[10]++;                                    //m={(10,2), (20,1)}
  148.     auto it = m.find(10);                   //it is an iterator that points to (10, 2)
  149.     cout << (it != m.end() ? it->second : 0);   //outputs 2
  150.     auto it2 = m.find(20);              //it is an iterator that points to (20, 1)
  151.     cout << (it2 != m.end() ? it2->first : 0);  //outputs 20
  152.     cout << m.size();                           //outputs 2    
  153. }
  154.  
  155. //**************STACK
  156. //          LIFO data structure
  157. //**Popular methods
  158. //
  159. //push()            0(1)*  the star is because push complexity is acctually equivalent to the push_back on the line container
  160. //pop()             0(1)
  161. //top()             0(1)
  162. //size()            0(1)
  163.  
  164. #include<stack>
  165.  
  166. using std::stack;
  167.  
  168. void Stack()
  169. {
  170.     stack<int> s;
  171.     cout << s.size(); //Outputs 0
  172.     s.push(1);
  173.     s.push(2);
  174.     cout << s.top(); //outputs 2
  175.     s.pop();
  176.     cout << s.top(); //outputs 1
  177.     cout << s.size();//outputs 1
  178. };
  179.  
  180. //**************QUEUE
  181. //          FIFO data structure
  182. //**Popular methods
  183. //
  184. //push()            0(1)*
  185. //pop()             0(1)
  186. //front()           0(1)
  187. //size()            0(1)
  188.  
  189. #include <queue>
  190. using std::queue;
  191.  
  192. void Queue()
  193. {
  194.     queue<int>q;
  195.     cout << q.size();//0
  196.     q.push(1);
  197.     q.push(2);
  198.     cout << q.front();//1
  199.     q.pop();
  200.     cout << q.front();//2
  201.     cout << q.size();//1
  202. };
  203.  
  204. //**************PRIORITY QUEUE
  205. //Excentially a Heap
  206. //
  207. //**Popular methods
  208. //
  209. //push()            0(logn) When you call push you make 1 call to push_back(), 1 call to push_heap()
  210. //pop()             0(logn) 1 call to pop_heap(), 1 call to pop_back()
  211. //top()             0(1)
  212. //size()            0(1)
  213.  
  214. #include <concurrent_priority_queue.h>
  215. using std::priority_queue;
  216.  
  217. void PriorityQueue()
  218. {
  219.     priority_queue<int> pq;
  220.     cout << pq.size();  //0
  221.     pq.push(3);
  222.     pq.push(1);
  223.     pq.push(2);
  224.     cout << pq.top();   //3
  225.     pq.pop();
  226.     cout << pq.top();   //2 - It shows the number by priority greater to lower.
  227.     cout << pq.size();  //2
  228. };
  229.  
  230.  
  231. //***Multi Set
  232. //
  233. //**Popular methods
  234. //
  235. //insert()              0(logn)
  236. //find()                0(logn)
  237. //size()                0(1)
  238.  
  239. using std::multiset;
  240.  
  241. void MultiSet()
  242. {
  243.     multiset<int> ms;                               //s{}
  244.     cout << ms.size();                              //outputs 0
  245.     ms.insert(1);                               //s = {1}
  246.     ms.insert(2);                               //s = {1, 2}   
  247.     ms.insert(1);                               //s = {1, 1, 2} //Auto sort order
  248.     auto it = ms.find(1);
  249.     cout << (it != ms.end() ? " FOUND" : " ");      //outputs FOUND
  250.     cout << ms.size();                              //output 3
  251. };
  252.  
  253. //***MultiMap
  254. //
  255. //**Popular methods
  256. //
  257. //insert()                  0(logn) insert using make_pair
  258. //find()                    0(logn) returns pair
  259. //size()                    0(1)
  260. using std::multimap;
  261. void MultiMap()
  262. {
  263.     multimap<int, int> mm;                  //m={}
  264.     cout << mm.size();                      //outputs 0
  265.     mm.insert({ 1,2 });                     //mm={(1,2)}
  266.     mm.insert({ 2,4 });                     //mm={(1,2), (2,4)}
  267.     mm.insert({ 1,3 });                     //mm={(1,2), (1,3), (2,4)}  //Auto sort order
  268.     auto it = mm.find(2);
  269.     cout << (it != mm.end() ? it->second : 0);  //outputs 4
  270.     cout << mm.size();                          //outputs 3    
  271. }
  272.  
  273. //*****************ALGORITHMS***************//
  274. //
  275. //
  276. //
  277. //
  278. //*** std::sort
  279. //  This is an algorithm with linearithmic time
  280. //complexity [0(nlogn)] that you can use to order the
  281. //elements of container from begin to end. The ordering by default is ascendiing for numeric values
  282. //and lexicographical ascending for string values
  283. //Ordering of equivalent elements are guaranteed to keep there same order.
  284.  
  285. void Sort() {
  286.     vector<int> v{ 3,5,1,2,4,5,3,0 };
  287.     sort(begin(v), end(v)); //Sort in order
  288.     sort(begin(v), begin(v) + 2); //Sort in order until the third element
  289.  
  290.     //Test
  291.     /*for (int i = 0; i < v.size(); i++)
  292.         cout << v[i];*/
  293. }
  294.  
  295.  
  296. //*** std::binary_search
  297. //  This is an algorithm with logarithmic time
  298. //complexity [0(nlogn)] that will return true if the
  299. //value searched for exists in the sorted sequence
  300. //or at least partitioned withg respect to the value searched for
  301. void BinarySearch() {
  302.     vector<int> v = { 1,3,5,7 };
  303.     cout << (binary_search(begin(v), end(v), 3) ? "Found" : "NotFound"); //Outputs found
  304.     cout << (binary_search(begin(v), end(v), 4) ? "Found" : "NotFound"); //Outputs notfound
  305. }
  306.  
  307. //*** std::lower_bound
  308. //  This is an algorithm with logarithmic time
  309. //complexity [0(logn)] that will return an iterator to
  310. //the first element equal to or greater than a given
  311. //value in a sorted sequence.
  312. //
  313. //In resume: "first equal to or greater than"
  314.  
  315.  
  316. void LowerBound() {
  317.     vector<int> v = { 1,3,3,5,7 };
  318.  
  319.     auto it = lower_bound(begin(v), end(v), 3);
  320.     auto it2 = lower_bound(begin(v), end(v), 4);
  321.     auto it3 = lower_bound(begin(v), end(v), 8);
  322.  
  323.     cout << (it != end(v) ? to_string(*it) : "Not Found"); //outputs 3
  324.     cout << (it2 != end(v) ? to_string(*it2) : "Not Found"); //outputs 5
  325.     cout << (it3 != end(v) ? to_string(*it3) : "Not Found"); //outputs Not Found
  326.  
  327.  
  328.     cout << distance(begin(v), it); //outputs 1
  329. };
  330.  
  331.  
  332. //*** std::upper_bound
  333. //  This is an algorithm with logarithmic time
  334. //complexity [0(logn)] that will return an iterator to
  335. //the first element greater than a given
  336. //value in a sorted sequence.
  337. //
  338. //In resume: "first greater than"
  339. void UpperBound() {
  340.     vector<int> v = { 1,3,3,5,7 };
  341.  
  342.     auto it = upper_bound(begin(v), end(v), 3);
  343.     auto it2 = upper_bound(begin(v), end(v), 4);
  344.     auto it3 = upper_bound(begin(v), end(v), 8);
  345.  
  346.     cout << (it != end(v) ? to_string(*it) : "Not Found"); //outputs 5
  347.     cout << (it2 != end(v) ? to_string(*it2) : "Not Found"); //outputs 5
  348.     cout << (it3 != end(v) ? to_string(*it3) : "Not Found"); //outputs Not Found
  349. };
  350.  
  351. //***First less than
  352. //Create an algorithm to fill the quest above...
  353. template<typename ForwardIterator, typename T>
  354. ForwardIterator first_less_than(ForwardIterator first, ForwardIterator last, T value) {
  355.     auto it = lower_bound(first, last, value);
  356.     return (it == first ? last : --it);
  357. }
  358.  
  359. void LessBound() {
  360.     vector<int> v = { 1,3,3,5,7 };
  361.  
  362.     auto it = first_less_than(begin(v), end(v), 3);
  363.     auto it2 = first_less_than(begin(v), end(v), 4);
  364.     auto it3 = first_less_than(begin(v), end(v), 8);
  365.  
  366.     cout << (it != end(v) ? to_string(*it) : "Not Found"); //outputs 1
  367.     cout << (it2 != end(v) ? to_string(*it2) : "Not Found"); //outputs 3
  368.     cout << (it3 != end(v) ? to_string(*it3) : "Not Found"); //7
  369. }
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376. //Hacker Rank:
  377. void passByReference(int* x)
  378. {
  379.     *x = 66;
  380. }
  381.  
  382. //****PRINT TYPES
  383. void PrintTypesOperation()
  384. {
  385.  
  386.     //Int("%d") : 32 Bit integer
  387.     //Long("%ld") : 64 bit integer
  388.     //Char("%c") : Character type
  389.     //Float("%f") : 32 bit real value
  390.     //Double("%lf") : 64 bit real value
  391.  
  392.     //Input:
  393.     //458873627 37274594125879112 g -79720.555 7441739.542437971
  394.  
  395.     int a; long b; char c; float d; double e;
  396.     cin >> a >> b >> c >> d >> e;
  397.     cout << a << endl;
  398.     cout << b << endl;
  399.     cout << c << endl;
  400.     cout.precision(3);
  401.     cout << std::fixed << d << endl;
  402.     cout.precision(9);
  403.     cout << std::fixed << e << endl;
  404. }
  405.  
  406. //****BOOLEANS
  407. void BooleanOperations(){
  408. //  int n;
  409. //  cin >> n;
  410. //  string name1 = "One", name2 = "Two", name3 = "Three", name4 = "Four",
  411. //      name5 = "Five", name6 = "Six", name7 = "Seven", name8 = "Eight", name9 = "Nine",
  412. //  MessageOutput1 = "Greater than nine";
  413. //  if (1 <= n && n <= 9)
  414. //  {
  415. //      switch (n)
  416. //      {
  417. //      case 1:
  418. //          cout << name1;
  419. //          break;
  420. //      case 2:
  421. //          cout << name2;
  422. //          break;
  423. //      case 3:
  424. //          cout << name3;
  425. //          break;
  426. //
  427. //      case 4:
  428. //          cout << name4;
  429. //          break;
  430. //
  431. //      case 5:
  432. //          cout << name5;
  433. //          break;
  434. //
  435. //      case 6:
  436. //          cout << name6;
  437. //          break;
  438. //
  439. //      case 7:
  440. //          cout << name7;
  441. //          break;
  442. //
  443. //      case 8:
  444. //          cout << name8;
  445. //          break;
  446. //
  447. //      case 9:
  448. //          cout << name9;
  449. //          break;
  450. //      default:
  451. //          cout << "Error";
  452. //      }
  453. //  }
  454. //  else if (n > 9)
  455. //      cout << MessageOutput1;
  456. };
  457.  
  458. //****LOOPS
  459. void LoopOperation()
  460. {
  461.     int a, b;
  462.  
  463.     cin >> a >> b;
  464.  
  465.     for (; a < (b + 1); ++a)
  466.     {
  467.         //cout << a << "\n\n\n\n";
  468.         // int *n_ptr{ &n };
  469.         //cout << *n_ptr;
  470.  
  471.         switch (a)
  472.         {
  473.             case 1:
  474.                 cout << "one" << "\n";
  475.                 break;
  476.             case 2:
  477.                 cout << "two" << "\n";
  478.                 break;
  479.             case 3:
  480.                 cout << "three" << "\n";
  481.                 break;
  482.  
  483.             case 4:
  484.                 cout << "four" << "\n";
  485.                 break;
  486.  
  487.             case 5:
  488.                 cout << "five" << "\n";
  489.                 break;
  490.  
  491.             case 6:
  492.                 cout << "six" << "\n";
  493.                 break;
  494.  
  495.             case 7:
  496.                 cout << "seven" << "\n";
  497.                 break;
  498.  
  499.             case 8:
  500.                 cout << "eight" << "\n";
  501.                 break;
  502.  
  503.             case 9:
  504.                 cout << "nine" << "\n";
  505.                 break;
  506.  
  507.             default:
  508.                 if (a > 9)
  509.                 {
  510.                     if (a % 2 != 0) { cout << "odd" << "\n"; }
  511.                     else { cout << "even" << "\n"; }
  512.                 }
  513.            
  514.         }
  515.     };
  516. }
  517.  
  518. //****FUNCTIONS
  519. int max_of_four(int a, int b, int c, int d) {
  520.     std::vector<int> v = { a,b,c,d };
  521.     std::sort(begin(v), end(v), [](int elem1, int elem2) {return elem1 > elem2; });
  522.     cout << v[0];
  523.     return v[0];
  524. }
  525.  
  526. //****POINTERS
  527. int increment(int*v)
  528. {
  529.     return ++(*v);
  530. }
  531.  
  532. void PointersOperation()
  533. {
  534.     int value = 918;
  535.     int * value_ptr = &value;
  536.     cout << *value_ptr;
  537.     increment(&value);
  538.     cout << value;
  539. }
  540.  
  541. void update(int *a, int *b) {
  542.     const int sum = (*a) + (*b);
  543.     const int sub = abs((*a) - (*b));
  544.     *a = sum;
  545.     *b = sub;
  546. }
  547.  
  548. //****ARRAYS
  549. void ArraysOperation()
  550. {
  551.     int size;
  552.     cin >> size;
  553.     int elemToEnter;
  554.     std::vector<int> v;
  555.  
  556.     for (int i = 0; i < size; ++i)
  557.     {
  558.         cin >> elemToEnter;
  559.         v.push_back(elemToEnter);
  560.     }
  561.  
  562.     for (const auto& value : v) {
  563.         std::cout << value << " ";
  564.     }
  565.     std::cout << '\n';
  566.  
  567.     std::vector<int> destination(size);
  568.     std::reverse_copy(std::begin(v), std::end(v), std::begin(destination));
  569.     for (const auto& value : destination) {
  570.         std::cout << value << " ";
  571.     }
  572.     std::cout << '\n';
  573. }
  574.  
  575. std::ostream operator<<(const std::ostream& lhs, const vector<int>& is);
  576.  
  577. void VariableSizedArrays()
  578. {
  579.     //n = arrays q = queries
  580.     std::map<vector<int>, vector<int>> nq;
  581.     std::map<char, int> mymap;
  582.  
  583.     int n = 2;
  584.     int q = 2;
  585.     //cin >> n; cin >> q; cout << "\n";
  586.     nq.insert(std::pair <vector<int>, vector<int>>(n , q));
  587.     nq.insert(std::pair <vector<int>, vector<int>>(2, 4));
  588.     nq.insert(std::pair <vector<int>, vector<int>>(6, 7));
  589.     nq.insert(std::pair <vector<int>, vector<int>>(1, 5));
  590.  
  591.     int size;
  592.     cin >> size;
  593.     for (auto elem : nq)
  594.     {
  595.         int elemToEnter;
  596.         cin >> elemToEnter;
  597.         //elem.first.push_back(elemToEnter);
  598.         vector<int> temp = elem.first;
  599.         //std::cout << value << " ";
  600.  
  601.         //elem.first[0] = 213;
  602.     }
  603.  
  604.     std::map<vector<int>, int> arraysMap; //Store all elem arrays
  605.     int arrayNumbers = -1;
  606.     for (auto elem : nq)
  607.     {
  608.         arrayNumbers++;
  609.         //cout << elem.first[0];
  610.         arraysMap.insert(std::pair <vector<int> ,int>(elem.first[arrayNumbers] /* must pass vector n */, arrayNumbers));
  611.     }
  612.  
  613.     std::map<int, int> queriesMap; //Store all elem queries
  614.     int queryNumbers = -1;
  615.     for (auto elem : nq)
  616.     {
  617.         queryNumbers++;
  618.         //cout << elem.second[0];
  619.         queriesMap.insert(std::pair <int, int>(elem.second[queryNumbers], queryNumbers));
  620.     }
  621.     cout << "\n";
  622. }
  623.  
  624. void VariableSizedArray2()
  625. {
  626.     std::map<char, int> mymap;
  627.  
  628.     // first insert function version (single parameter):
  629.     mymap.insert(std::pair<char, int>('a', 100));
  630.     mymap.insert(std::pair<char, int>('z', 200));
  631.  
  632.     //std::pair<std::map<char, int>::iterator, bool> ret;
  633.     //ret = mymap.insert(std::pair<char, int>('z', 500));
  634.     //if (ret.second == false) {
  635.     //  std::cout << "element 'z' already existed";
  636.     //  std::cout << " with a value of " << ret.first->second << '\n';
  637.     //}
  638.  
  639.     //// second insert function version (with hint position):
  640.     std::map<char, int>::iterator it = mymap.begin();
  641.     mymap.insert(it, std::pair<char, int>('b', 300));  // max efficiency inserting
  642.     mymap.insert(it, std::pair<char, int>('c', 400));  // no max efficiency inserting
  643.  
  644.     //// third insert function version (range insertion):
  645.     //std::map<char, int> anothermap;
  646.     //anothermap.insert(mymap.begin(), mymap.find('c'));
  647.  
  648.     // showing contents:
  649.     std::cout << "mymap contains:\n";
  650.     for (it = mymap.begin(); it != mymap.end(); ++it)
  651.         std::cout << it->first << " => " << it->second << '\n';
  652.  
  653.     /*std::cout << "anothermap contains:\n";
  654.     for (it = anothermap.begin(); it != anothermap.end(); ++it)
  655.         std::cout << it->first << " => " << it->second << '\n';*/
  656. }
  657.  
  658. //Worked
  659. void VariableSizedArrays4(){
  660.  
  661.     // get length of array 'a' and number of queries
  662.     int n, q;
  663.     cin >> n >> q;
  664.  
  665.     // create vector of vectors
  666.     vector<vector<int>> a(n);
  667.  
  668.     // fill each 2D vector i with k_i values
  669.     for (int i = 0; i < n; i++) {
  670.         // get the length k of the vector at a[i]
  671.         int k;
  672.         cin >> k;
  673.  
  674.         // fill the vector with k values       
  675.         a[i].resize(k);
  676.         for (int j = 0; j < k; j++) {
  677.             cin >> a[i][j]; //Add elem on vector i inside the vector a
  678.         }
  679.     }
  680.  
  681.     // run queries on a
  682.     for (int q_num = 0; q_num < q; q_num++) {
  683.         // get i, j as the 'query' to get a value from a
  684.         int i, j;
  685.         cin >> i >> j;
  686.         cout << a[i][j] << endl; //RunqueryOnElem
  687.     }
  688. }
  689.  
  690. //*****PRE PROCESSOR
  691. #define LIMIT 5
  692. #define AREA(a, b)(a * b)
  693. #define DIFFERENCE(a, b)(a - b)
  694. #define str(x) #x
  695. void PreprocessorSolution()
  696. {
  697.  
  698.     cout << str(test);
  699.  
  700.  
  701.     //int size;
  702.     //cin >> size;
  703.     //vector<int> elems(size);
  704.     //for(int i = 0; i < size; ++i)
  705.     //{
  706.     //  int elem;
  707.     //  cin >> elem;
  708.     //  elems.push_back(elem);
  709.     //}
  710.  
  711.  
  712.     //auto max = *std::max_element(begin(elems), end(elems));
  713.     //auto min = *std::min_element(begin(elems), end(elems));
  714.  
  715.     //cout << DIFFERENCE(max, min);
  716.  
  717. }
  718.  
  719.  
  720. #define foreach(v, i) for (int i = 0; i < v.size(); ++i)
  721. #define toStr(x) #x
  722. #define io(v) cin >> v
  723. #define FUNCTION(name,operator) inline void name(int &current, int candidate) {!(current operator candidate) ? current = candidate : false;}
  724. #define INF (unsigned)!((int)0)
  725.  
  726. #if !defined toStr || !defined io || !defined FUNCTION || !defined INF
  727. #error Missing preprocessor definitions
  728. #endif
  729.  
  730. FUNCTION(minimum, < )
  731. FUNCTION(maximum, > )
  732.  
  733. void PreprocessorSolution2() {
  734.     int n; cin >> n;
  735.     vector<int> v(n);
  736.  
  737.     foreach(v, i) {
  738.         io(v)[i];
  739.     }
  740.  
  741.     int mn = INF;
  742.     int mx = INF; // -INF
  743.  
  744.     foreach(v, i) {
  745.         minimum(mn, v[i]);
  746.         maximum(mx, v[i]);
  747.     }
  748.  
  749.     int ans = mx - mn;
  750.     cout << toStr(Result = ) << ' ' << ans;
  751. }
  752.  
  753. //*****INLINE
  754. class InlineExample {
  755.     int a, b;
  756. public:
  757.     inline void initialize(int x, int y) { a = x; b = y; };
  758.     void display() { cout << a << " " << b << "\n" ; } //automatic inline
  759. };
  760.  
  761.    
  762. //*****OPERATOR OVERLOAD
  763. class Matrix   
  764. {
  765. public:
  766.     vector<vector<int>> a;
  767.     Matrix& operator + (const Matrix &other)
  768.     {
  769.         for(int i = 0; i < other.a.size(); i++)
  770.         {
  771.             for(int j = 0; j < other.a[0].size(); j++)
  772.             {
  773.                 this->a[i][j] = this->a[i][j] + other.a[i][j];
  774.             }
  775.         }
  776.         return *this; // Return a clone of the component of this current object witch is on the stack
  777.     };
  778.  
  779. ///Old solution
  780. //  vector<int> resultVec1, resultVec2;
  781. //public:
  782. //  vector<vector<int>> a;
  783. //
  784. //  Matrix() = default;
  785. //
  786. //  explicit Matrix(const vector<vector<int>>& vectors)
  787. //      : a(vectors)
  788. //  {
  789. //
  790. //  }
  791. //
  792. //  Matrix *pMatrix;
  793. //
  794. //  Matrix operator +(Matrix const &other)
  795. //  {
  796. //      Matrix matrix;
  797. //      pMatrix = &matrix;
  798. //
  799. //      unsigned n_size = this->a.size();
  800. //
  801. //      for (unsigned i = 0; i < n_size; i++)
  802. //      {
  803. //          unsigned m_size = a[i].size();
  804. //          for (unsigned j = 0; j < m_size; j++)
  805. //          {
  806. //              resultVec1.push_back(a[i][j]);
  807. //              //cout << "\n" << "Returns: " << a[i][j] << "\n";
  808. //          }
  809. //      }
  810. //
  811. //      unsigned n_size2 = other.a.size();
  812. //      for (unsigned i = 0; i < n_size2; i++)
  813. //      {
  814. //          unsigned m_size2 = other.a[i].size();
  815. //          for (unsigned j = 0; j < m_size2; j++)
  816. //          {
  817. //              resultVec2.push_back(other.a[i][j]);
  818. //              //cout << "\n" << "Returns: " << other.a[i][j] << "\n";
  819. //          }
  820. //      }
  821. //
  822. //      int result;
  823. //      vector<int> glueVec;
  824. //      unsigned n_size3 = resultVec1.size();
  825. //      for (int i = 0; i < n_size3; i++)
  826. //      {
  827. //          result = resultVec1[i] + resultVec2[i];
  828. //          glueVec.push_back(result);
  829. //      }
  830. //      matrix.a.push_back(glueVec);
  831. //      return matrix;
  832. //  };
  833. //
  834. //  ~Matrix()
  835. //  {
  836. //      delete pMatrix;
  837. //  };     
  838.  
  839.    
  840. };
  841.  
  842. void OperatorOverload()
  843. {
  844.     int cases, k;
  845.     cin >> cases;
  846.     for (k = 0; k < cases; k++) //FOR
  847.     {
  848.         Matrix x;
  849.         Matrix y;
  850.         Matrix result;
  851.         int n, m, i, j; //n & m rows and colums
  852.         cin >> n >> m;
  853.         for (i = 0; i < n; i++)//For
  854.         {
  855.             vector<int> b;  //Act like a glue for x
  856.             int num;
  857.             for (j = 0; j < m; j++) {
  858.                 cin >> num;
  859.                 b.push_back(num);
  860.             }
  861.             x.a.push_back(b);
  862.         }
  863.         for (i = 0; i < n; i++)//For
  864.         {
  865.             vector<int> b; //Act like a glue for y
  866.             int num;
  867.             for (j = 0; j < m; j++) {
  868.                 cin >> num;
  869.                 b.push_back(num);
  870.             }
  871.             y.a.push_back(b);
  872.         }
  873.  
  874.         result = x + y;
  875.         for (i = 0; i < n; i++) {
  876.             for (j = 0; j < m; j++) {
  877.                 cout << result.a[i][j] << " ";
  878.             }
  879.             cout << endl;
  880.         }
  881.     }
  882. }
  883.  
  884. class Complex
  885. {
  886. public:
  887.     int a, b;
  888.     void input(string s)
  889.     {
  890.         int v1 = 0;
  891.         int i = 0;
  892.         while (s[i] != '+')
  893.         {
  894.             v1 = v1 * 10 + s[i] - '0';
  895.             i++;
  896.         }
  897.         while (s[i] == ' ' || s[i] == '+' || s[i] == 'i')
  898.         {
  899.             i++;
  900.         }
  901.         int v2 = 0;
  902.         while (i < s.length())
  903.         {
  904.             v2 = v2 * 10 + s[i] - '0';
  905.             i++;
  906.         }
  907.         a = v1;
  908.         b = v2;
  909.     }
  910. };
  911.  
  912. //*TO OUTPUT COMPLEX
  913.  
  914. #include <string>
  915.  
  916. Complex& operator +(Complex& clx, Complex& clx2)
  917. {
  918.     Complex complex;
  919.     complex.a = clx.a + clx2.a;
  920.     complex.b = clx.b + clx2.b;
  921.     return complex;
  922. };
  923.  
  924. std::ostream& operator<<(std::ostream& lhs, const Complex& rhs)
  925. {
  926.     return lhs << rhs.a << "+" << "i" << rhs.b;
  927. };
  928.  
  929. //*More elaborated solution:
  930.  
  931. //constexpr Complex operator+(const Complex& lhs, const Complex& rhs) noexcept
  932. //{
  933. //  return { lhs.a + rhs.a, lhs.b + rhs.b };
  934. //}
  935. //
  936. //template <class StreamT>
  937. //constexpr StreamT& operator<<(StreamT& s, const Complex& c)
  938. //{
  939. //  return s << c.a << (c.b >= 0 ? "+" : "-") << "i" << c.b;
  940. //}
  941.  
  942. void OverloadOperators_ComplexNumberCalculator()
  943. {
  944.     Complex x, y;
  945.     string s1, s2;
  946.     cin >> s1;
  947.     cin >> s2;
  948.     x.input(s1);
  949.     y.input(s2);
  950.     Complex z = x + y;
  951.     cout << z << endl;
  952. }
  953.  
  954.  
  955. //*************STRINGS (to_string example)
  956. class Demon
  957. {
  958. public:
  959.     string a = "HAAAAAA";
  960.     int len = a.size();
  961.     int maxlen = a.max_size();
  962.     char c = a[2];
  963.     string b = "Satanas";
  964.     int age;
  965.     int *p_age = &age;
  966.  
  967.     int set_age(int age1)
  968.     {
  969.         return age = age1;
  970.     }
  971.  
  972.     std::string str1 = std::to_string(12.10);
  973.     std::string str2 = std::to_string(9999);
  974.     std::string str3 = std::to_string(age);
  975.  
  976.     string to_string() { return str3 + "," + str1; };
  977. };
  978.  
  979.  
  980. //***********CLASSES AND OBJECTS
  981. class Student
  982. {
  983.     Student(const Student& student) {  }
  984.     vector<int> scores;
  985. public:
  986.     Student() = default;
  987.     void input()
  988.     {
  989.         for(int i = 0; i < 5; i++)
  990.         {
  991.             int temp;
  992.             cin >> temp;
  993.             scores.push_back(temp);
  994.         }
  995.     };
  996.  
  997.     int calculateTotalScore()
  998.     {
  999.         int temp = 0;
  1000.         int totalScore = 0;
  1001.        
  1002.         for(int i = 0; i < scores.size(); ++i)
  1003.         {
  1004.             if(i == 1)
  1005.             {
  1006.                 totalScore += scores[i] + temp;
  1007.             }
  1008.             if (i >= 2)
  1009.             {
  1010.                 totalScore += scores[i];
  1011.             }
  1012.             temp = scores[i];
  1013.         }
  1014.  
  1015.         return totalScore;
  1016.     }
  1017.  
  1018. };
  1019.  
  1020. void  ClassesAndObjectsOperations() {
  1021.     int n; // number of students
  1022.     cin >> n;
  1023.     const auto s = new Student[n]; // an array of n students
  1024.  
  1025.     for (auto i = 0; i < n; i++) {
  1026.         s[i].input();
  1027.     }
  1028.  
  1029.      //calculate kristen's score
  1030.     const int kristen_score = s[0].calculateTotalScore();
  1031.  
  1032.     // determine how many students scored higher than kristen
  1033.     int count = 0;
  1034.     for (int i = 1; i < n; i++) {
  1035.         int total = s[i].calculateTotalScore();
  1036.         if (total > kristen_score) {
  1037.             count++;
  1038.         }
  1039.     }
  1040.  
  1041.     // print result
  1042.     cout << count;
  1043. }
  1044.  
  1045.  
  1046. //****Box It!
  1047. //Implement the class Box  
  1048. //l,b,h are integers representing the dimensions of the box
  1049.  
  1050. // The class should have the following functions :
  1051.  
  1052. // Constructors:
  1053. // Box();
  1054. // Box(int,int,int);
  1055. // Box(Box);
  1056.  
  1057.  
  1058. // int getLength(); // Return box's length
  1059. // int getBreadth (); // Return box's breadth
  1060. // int getHeight ();  //Return box's height
  1061. // long long CalculateVolume(); // Return the volume of the box
  1062.  
  1063. //Overload operator < as specified
  1064. //bool operator<(Box& b)
  1065.  
  1066. //Overload operator << as specified
  1067. //ostream& operator<<(ostream& out, Box& B)
  1068.  
  1069. class Box
  1070. {
  1071.     //l,b,h are integers representing the dimensions of the box
  1072. private:
  1073.     int l=0, b=0, h = 0;
  1074. public:
  1075.  
  1076.     // The class should have the following functions :
  1077.  
  1078.     // Constructors:
  1079.     // Box();
  1080.     // Box(int,int,int);
  1081.     // Box(Box);
  1082.     Box() = default;
  1083.     Box(int l, int b, int h) : l(l), b(b), h(h) {};
  1084.     Box(const Box& box)
  1085.     {
  1086.         l = box.getLength();
  1087.         b = box.getBreadth();
  1088.         h = box.getHeight();
  1089.     }
  1090.     // int getLength(); // Return box's length
  1091.     // int getBreadth (); // Return box's breadt h
  1092.     // int getHeight ();  //Return box's height
  1093.     int getLength() const{
  1094.         return l;
  1095.     };
  1096.     int getBreadth() const {
  1097.         return b;
  1098.     };
  1099.     int getHeight() const {
  1100.         return h;
  1101.     };
  1102.     // long long CalculateVolume(); // Return the volume of the box
  1103.     long long CalculateVolume() {
  1104.         return (long long)l*b*h;
  1105.     }
  1106.  
  1107.     //Overload operator < as specified
  1108.     //bool operator<(Box& b)
  1109.     friend bool operator <(Box& A, Box& B)
  1110.     {
  1111.         if ((A.l < B.l) || ((A.b < B.b) && (A.l == B.l)) || ((A.h < B.h) && (A.l == B.l) && (A.b == B.b))) {
  1112.             return true;
  1113.         }
  1114.         else {
  1115.             return false;
  1116.         };
  1117.  
  1118.     }
  1119.  
  1120.     //Overload operator << as specified
  1121.     friend std::ostream& operator<<(std::ostream& out, Box& B)
  1122.     {
  1123.         out << B.getLength() << " " << B.getBreadth() << " " << B.getHeight();
  1124.         return out;
  1125.     }
  1126. };
  1127.  
  1128. void checkBox()
  1129. {
  1130.     int n;
  1131.     cin >> n;
  1132.     Box temp;
  1133.     for (int i = 0; i < n; i++)
  1134.     {
  1135.         int type;
  1136.         cin >> type;
  1137.         if (type == 1)
  1138.         {
  1139.             cout << temp << endl;
  1140.         }
  1141.         if (type == 2)
  1142.         {
  1143.             int l, b, h;
  1144.             cin >> l >> b >> h;
  1145.             Box NewBox(l, b, h);
  1146.             temp = NewBox;
  1147.             cout << temp << endl;
  1148.         }
  1149.         if (type == 3)
  1150.         {
  1151.             int l, b, h;
  1152.             cin >> l >> b >> h;
  1153.             Box NewBox(l, b, h);
  1154.             if (NewBox < temp)
  1155.             {
  1156.                 cout << "Lesser\n";
  1157.             }
  1158.             else
  1159.             {
  1160.                 cout << "Greater\n";
  1161.             }
  1162.         }
  1163.         if (type == 4)
  1164.         {
  1165.             cout << temp.CalculateVolume() << endl;
  1166.         }
  1167.         if (type == 5)
  1168.         {
  1169.             Box NewBox(temp);
  1170.             cout << NewBox << endl;
  1171.         }
  1172.  
  1173.     }
  1174. }
  1175. #include <algorithm>
  1176. //*********Vector-Sort
  1177. void VectorSortOperations()
  1178. {
  1179.     vector<int> v;
  1180.     int size = v.size();
  1181.     int n;
  1182.     cin >> n;
  1183.     for(int i = 0; i < n; i++)
  1184.     {
  1185.         int temp;
  1186.         cin >> temp;
  1187.         v.push_back(temp);
  1188.     };
  1189.  
  1190.     std::sort(std::begin(v), std::end(v));
  1191.  
  1192.     for(int i = 0; i < v.size(); i++)
  1193.     {
  1194.         cout << v[i] << " ";
  1195.     };
  1196. }
  1197.  
  1198. void VectorErraseOperations()
  1199. {
  1200.     vector<int> v;
  1201.     int size;
  1202.     cin >> size;
  1203.     for (int i = 0; i < size; i++) {
  1204.         int b = 0;
  1205.         cin >> b;
  1206.         v.push_back(b);
  1207.     };
  1208.     int r1;
  1209.     cin >> r1;
  1210.     v.erase(v.begin() + r1 - 1);
  1211.     int b, e, r2;
  1212.     cin >> r2;
  1213.     cin >> b >> e;
  1214.     v.erase(v.begin() + b - 1, v.begin() + e - 1);
  1215.  
  1216.     for (int i = 0; i < size; i++) {
  1217.         cout << v[i];
  1218.     };
  1219. };
  1220.  
  1221. //**********Lower Bound - STL
  1222. void LowerBoundSTLOperations()
  1223. {
  1224.     vector<int> v;
  1225.     int size;
  1226.     cin >> size;
  1227.     for(int i = 0; i < size; i++)
  1228.     {
  1229.         int temp= 0;
  1230.         cin >> temp;
  1231.         v.push_back(temp);
  1232.     };
  1233.  
  1234.     std::sort(v.begin(), v.end());
  1235.  
  1236.     std::vector<int>::iterator low;
  1237.     int queries = 0;
  1238.     cin >> queries;
  1239.     for(int i = 0; i < queries; i++)
  1240.     {
  1241.         int numToSearch = 0;
  1242.         cin >> numToSearch;
  1243.  
  1244.         low = std::lower_bound(begin(v), end(v), numToSearch);
  1245.         if (numToSearch == (low - v.begin()))
  1246.             cout << "Yes " << (low - v.begin());
  1247.         else
  1248.             cout << "No " << (low - v.begin());
  1249.     }
  1250. }
  1251.  
  1252.  
  1253. void TestLowerBound()
  1254. {
  1255.     vector<int> v;
  1256.     int s, n;
  1257.     cin >> s;
  1258.     for (int i = 0; i < s; i++) {
  1259.         cin >> n;
  1260.         v.push_back(n);
  1261.     };
  1262.     int qn, q;
  1263.     cin >> qn;
  1264.     for (int i = 0; i < qn; i++) {
  1265.         cin >> q;
  1266.         auto it = lower_bound(begin(v), end(v), q);
  1267.         cout << (*it == q ? "Yes " + to_string(distance(begin(v), it) + 1) + "\n" : "No " + to_string(distance(begin(v), it) + 1) + "\n");
  1268.     }
  1269. };
  1270.  
  1271.  
  1272. //************SET
  1273. void SetChallenge() {
  1274.     int q;
  1275.     cin >> q;
  1276.     set<int> s;
  1277.     for (int i = 0; i < q; i++) {
  1278.         int x;
  1279.         cin >> x;
  1280.         if (x == 1) {   //Add an element to Set
  1281.             int y = 0;
  1282.             cin >> y;
  1283.             s.insert(y);
  1284.         }
  1285.         else if (x == 2) {  //erase an element to Set
  1286.             int y = 0;
  1287.             cin >> y;
  1288.             s.erase(y);
  1289.         }
  1290.         else if (x == 3) {  //Search an element in set
  1291.             int y;
  1292.             cin >> y;
  1293.             auto it = s.find(y);                        //it is an iterator that points to 10
  1294.             cout << (it != s.end() ? " Yes" : "No ");   //outputs FOUND
  1295.  
  1296.         }
  1297.        
  1298.     }
  1299.  
  1300.  
  1301. }
  1302.  
  1303. //************MAP
  1304. void MapChallenge() {
  1305.  
  1306.     //map<int, int> m;                          //m={}
  1307.     //cout << m.size();                         //outputs 0
  1308.     //m.insert(make_pair(20, 1));                   //m={(20,1)}
  1309.     //m.insert(make_pair(10, 1));                   //m={(10,1), (20,1)}
  1310.     //m[10]++;                                  //m={(10,2), (20,1)}
  1311.     //auto it = m.find(10);                 //it is an iterator that points to (10, 2)
  1312.     //cout << (it != m.end() ? it->second : 0); //outputs 2
  1313.     //auto it2 = m.find(20);                //it is an iterator that points to (20, 1)
  1314.     //cout << (it2 != m.end() ? it2->first : 0);    //outputs 20
  1315.     //cout << m.size();                         //outputs 2    
  1316.  
  1317.     map<string, int> m;
  1318.     int q;
  1319.     cin >> q;
  1320.     for (int i = 0; i < q; i++) {
  1321.         int type;
  1322.         cin >> type;
  1323.         if (type == 1) {
  1324.             //Create
  1325.             string name;
  1326.             int mark;
  1327.             cin >> name;
  1328.             cin >> mark;
  1329.             auto it = m.find(name);
  1330.             if (it != m.end()) {
  1331.                 cout << "Here";
  1332.                 it->second = it->second + mark;
  1333.             }
  1334.             else
  1335.                 m.insert(make_pair(name, mark));
  1336.         }
  1337.         else if (type == 2) {
  1338.             //Delete
  1339.             string name;
  1340.             cin >> name;
  1341.             m.erase(name);
  1342.         }
  1343.         else if (type == 3) {
  1344.             //Read
  1345.             string name;
  1346.             cin >> name;
  1347.             auto it = m.find(name);            
  1348.             cout << (it != m.end() ? it->second : 0) << "\n";
  1349.         }
  1350.     }
  1351. }
  1352.  
  1353. //************DEQUE                     ---Incomplete
  1354.  
  1355. #include<deque>
  1356. using std::deque;
  1357. void Deque() {
  1358.  
  1359.     deque<int> mydeque; //Creates a double ended queue of deque of int type
  1360.    
  1361.     int length = mydeque.size(); //Gives the size of the deque
  1362.    
  1363.     mydeque.push_back(1); //Pushes element at the end
  1364.     mydeque.push_front(2); //Pushes element at the beginning
  1365.  
  1366.     mydeque.pop_back(); //Pops element from the end
  1367.     mydeque.pop_front(); //Pops element from the beginning
  1368.  
  1369.     mydeque.empty(); //Returns a boolean value which tells whether the deque is empty or not
  1370.  
  1371. }
  1372.  
  1373. //************Classes
  1374.  
  1375. class Rectangle {
  1376. public:
  1377.     int width, height;
  1378.     void display() { cout << width << " " << height << "\n"; };
  1379. };
  1380.  
  1381. class RectangleArea : public Rectangle {
  1382. public:
  1383.     void read_input() {
  1384.         cin >> width >> height;
  1385.     };
  1386.  
  1387.     void display() {
  1388.         cout << width * height << "\n";
  1389.     };
  1390. };
  1391.  
  1392. //***************Exception Handling
  1393. using std::invalid_argument;
  1394. int largest_proper_divisor(int n) {
  1395.     if (n == 0) {
  1396.         throw invalid_argument("largest proper divisor is not defined for n=0");
  1397.     }
  1398.     if (n == 1) {
  1399.         throw invalid_argument("largest proper divisor is not defined for n=1");
  1400.     }
  1401.     for (int i = n / 2; i >= 1; --i) {
  1402.         if (n % i == 0) {
  1403.             return i;
  1404.         }
  1405.     }
  1406.     return -1; // will never happen
  1407. }
  1408.  
  1409.  
  1410. void process_input(int n) {
  1411.     try {
  1412.         int d = largest_proper_divisor(n);
  1413.         cout << "result=" << d << endl;
  1414.     }
  1415.     catch (const invalid_argument& ia) {
  1416.         cout << ia.what() << endl;
  1417.     }
  1418.     cout << "returning control flow to caller" << endl;
  1419. }
  1420.  
  1421. //******Inherited Code
  1422. class BadLengthException {
  1423.     int elem;
  1424. public:
  1425.     BadLengthException(int n) { elem = n; };
  1426.     int what() { return elem; };
  1427. };
  1428.  
  1429. bool checkUsername(string username) {
  1430.     bool isValid = true;
  1431.     int n = username.length();
  1432.     if (n < 5) {
  1433.         throw BadLengthException(n);
  1434.     }
  1435.     for (int i = 0; i < n - 1; i++) {
  1436.         if (username[i] == 'w' && username[i + 1] == 'w') {
  1437.             isValid = false;
  1438.         }
  1439.     }
  1440.     return isValid;
  1441. }
  1442.  
  1443. int InheritedOperations() {
  1444.     int T; cin >> T;
  1445.     while (T--) {
  1446.         string username;
  1447.         cin >> username;
  1448.         try {
  1449.             bool isValid = checkUsername(username);
  1450.             if (isValid) {
  1451.                 cout << "Valid" << '\n';
  1452.             }
  1453.             else {
  1454.                 cout << "Invalid" << '\n';
  1455.             }
  1456.         }
  1457.         catch (BadLengthException e) {
  1458.             cout << "Too short: " << e.what() << '\n';
  1459.         }
  1460.     }
  1461.     return 0;
  1462. };
  1463.  
  1464. //*********Accessing Inherited Functions
  1465. class A
  1466. {
  1467. public:
  1468.     A() {
  1469.         callA = 0;
  1470.     }
  1471. private:
  1472.     int callA;
  1473.     void inc() {
  1474.         callA++;
  1475.     }
  1476.  
  1477. protected:
  1478.     void func(int & a)
  1479.     {
  1480.         a = a * 2;
  1481.         inc();
  1482.     }
  1483. public:
  1484.     int getA() {
  1485.         return callA;
  1486.     }
  1487. };
  1488.  
  1489. class B
  1490. {
  1491. public:
  1492.     B() {
  1493.         callB = 0;
  1494.     }
  1495. private:
  1496.     int callB;
  1497.     void inc() {
  1498.         callB++;
  1499.     }
  1500. protected:
  1501.     void func(int & a)
  1502.     {
  1503.         a = a * 3;
  1504.         inc();
  1505.     }
  1506. public:
  1507.     int getB() {
  1508.         return callB;
  1509.     }
  1510. };
  1511.  
  1512. class C
  1513. {
  1514. public:
  1515.     C() {
  1516.         callC = 0;
  1517.     }
  1518. private:
  1519.     int callC;
  1520.     void inc() {
  1521.         callC++;
  1522.     }
  1523. protected:
  1524.     void func(int & a)
  1525.     {
  1526.         a = a * 5;
  1527.         inc();
  1528.     }
  1529. public:
  1530.     int getC() {
  1531.         return callC;
  1532.     }
  1533. };
  1534.  
  1535. class D: public A, public B, public C
  1536. {
  1537.     int val;
  1538. public:
  1539.     //Initially val is 1
  1540.     D()
  1541.     {
  1542.         val = 1;
  1543.     }
  1544.  
  1545.     //Implement this function
  1546.     void update_val(int &new_val)
  1547.     {
  1548.         val = new_val;
  1549.         A::func(new_val);
  1550.         B::func(new_val);
  1551.         C::func(new_val);   //In site I did this different dont know what is happening
  1552.     }
  1553.     //For Checking Purpose
  1554.     void check(int); //Do not delete this line.
  1555. };
  1556.  
  1557. void D::check(int new_val)
  1558. {
  1559.     update_val(new_val);
  1560.     cout << "Value = " << val << endl << "A's func called " << getA() << " times " << endl << "B's func called "
  1561.         << getB() << " times" << endl << "C's func called " << getC() << " times" << endl;
  1562. }
  1563.  
  1564. //**********Virtual Functions
  1565. class Person {
  1566. protected:
  1567.     int age;
  1568.     string name;
  1569. public:
  1570.     virtual void getdata();
  1571.     virtual void putdata();
  1572. };
  1573.  
  1574. void Person::getdata() {};
  1575. void Person::putdata() {};
  1576.  
  1577. class Professor : public Person {
  1578. private:
  1579.     const int id;
  1580.     static int cur_id;
  1581.     string name;
  1582.     int publications, age;
  1583.  
  1584. public:
  1585.  
  1586.     Professor() : id(++cur_id)
  1587.     {};
  1588.  
  1589.     void getdata() override {
  1590.         cin >> this->name;
  1591.         cin >> this->age;
  1592.         cin >> this->publications;
  1593.     };
  1594.  
  1595.     void putdata() override {
  1596.         cout << name << " " << age << " " << publications << " " << this->id << "\n";
  1597.     };
  1598. };
  1599.  
  1600. int Professor::cur_id = 0;
  1601.  
  1602. class Student2 : public Person {
  1603.     const int id;
  1604.     static int cur_id;
  1605.  
  1606.     string name;
  1607.     int age;
  1608.     int mark[6];
  1609.  
  1610. public:
  1611.  
  1612.     Student2() : id(++cur_id)
  1613.     {};
  1614.  
  1615.     void getdata() override {
  1616.         cin >> this->name;
  1617.         cin >> this->age;
  1618.  
  1619.         for (int i = 0; i < 6; i++) {
  1620.             cin >> this->mark[i];
  1621.         };
  1622.     };
  1623.  
  1624.     void putdata() override {
  1625.         int sum = 0;
  1626.         for (int i = 0; i < 6; i++) {
  1627.             int previous = mark[i];
  1628.             sum += mark[i];
  1629.         }
  1630.  
  1631.         cout << name << " " << age << " " << sum << " " << id << "\n";
  1632.     };
  1633. };
  1634.  
  1635. int Student2::cur_id = 0;
  1636.  
  1637. void VirtualFuncOperations() {
  1638.  
  1639.     int val;
  1640.     const int n = 4; //The number of objects that is going to be created.
  1641.     Person *per[n];
  1642.  
  1643.     for (int i = 0; i < n; i++) {
  1644.         cin >> val;
  1645.         if (val == 1) {
  1646.             // If val is 1 current object is of type Professor
  1647.             per[i] = new Professor;
  1648.         }
  1649.         else per[i] = new Student2; // Else the current object is of type Student
  1650.  
  1651.         per[i]->getdata(); // Get the data from the user.
  1652.     }
  1653.  
  1654.     for (int i = 0; i < n; i++)
  1655.         per[i]->putdata(); // Print the required output for each object.
  1656.  
  1657.  
  1658.     for (int i = 0; i < n; i++)
  1659.     {
  1660.         delete per[i];//Clean memory
  1661.         per[i] = 0;
  1662.     }
  1663. }
  1664.  
  1665. //*******CLASS TEMPLATES
  1666. //Sum tempalte method
  1667. template <class T>
  1668. T sum(T a, T b)
  1669. {
  1670.     T result;
  1671.     result = a + b;
  1672.     return result;
  1673. }
  1674.  
  1675. //Multiply template class
  1676. template <class T>
  1677. class Multiply {
  1678.     T product1, product2;
  1679. public:
  1680.     Multiply(T arg1, T arg2) { product1 = arg1, product2 = arg2; };
  1681.     T Execute() { return product1 * product2; };
  1682. };
  1683.  
  1684. //Divide template class
  1685. template <class T>
  1686. class Divide {
  1687.     T element;
  1688. public:
  1689.     Divide(T arg) { element = arg; };
  1690.     T Execute() { return element / 2; };
  1691. };
  1692.  
  1693. // class template specialization:
  1694. template<>
  1695. class Divide<char>
  1696. {
  1697.     char element;
  1698. public:
  1699.     Divide(char arg) { element = arg; };
  1700.     char PrintElement() { return element; };
  1701. };
  1702.  
  1703. class Calculator {
  1704.     int e = 8;
  1705.     int s{ 0 };
  1706.     Divide<int> d = e;
  1707.     Multiply<int> m = { 3,3 };
  1708. public:
  1709.     void DivisorCalc() {
  1710.         cout << d.Execute() << "\n";
  1711.     }
  1712.  
  1713.     void SumCalc() {
  1714.         s = sum<int>(3, 5);
  1715.         cout << s << "\n";
  1716.     }
  1717.  
  1718.     void Multiply() {
  1719.         cout << m.Execute();
  1720.     }
  1721. };
  1722.  
  1723. //Exercise:
  1724. /*Write the class AddElements here*/
  1725. template <class T>
  1726. class AddElements
  1727. {
  1728. private:
  1729.     T element;
  1730. public:
  1731.     AddElements(T arg) { element = arg; };
  1732.     T add(T p) {
  1733.         return element + p;
  1734.     };
  1735. };
  1736.  
  1737. //specialization
  1738. template <>
  1739. class AddElements <string>
  1740. {
  1741. private:
  1742.     string element;
  1743. public:
  1744.     AddElements(string arg) { element = arg; };
  1745.  
  1746.     string concatenate(string p) {
  1747.         return element + p;
  1748.     };
  1749. };
  1750.  
  1751. void ClassTemplateOperations()
  1752. {
  1753.     int n, i;
  1754.     cin >> n;
  1755.     for (i = 0; i < n; i++) {
  1756.         string type;
  1757.         cin >> type;
  1758.         if (type == "float") {
  1759.             double element1, element2;
  1760.             cin >> element1 >> element2;
  1761.             AddElements<double> myfloat(element1);
  1762.             cout << myfloat.add(element2) << endl;
  1763.         }
  1764.         else if (type == "int") {
  1765.             int element1, element2;
  1766.             cin >> element1 >> element2;
  1767.             AddElements<int> myint(element1);
  1768.             cout << myint.add(element2) << endl;
  1769.         }
  1770.         else if (type == "string") {
  1771.             string element1, element2;
  1772.             cin >> element1 >> element2;
  1773.             AddElements<string> mystring(element1);
  1774.             cout << mystring.concatenate(element2) << endl;
  1775.         }
  1776.     }
  1777. }
  1778.  
  1779. //***************** Class Template Specialization
  1780. #include <iostream>
  1781. using namespace std;
  1782. enum class Fruit { apple, orange, pear };
  1783. enum class Color { red, green, orange };
  1784.  
  1785. template <typename T> struct Traits;
  1786.  
  1787. template<>
  1788. struct Traits<Color> {
  1789.     static string name(int index)
  1790.     {
  1791.         string asnwer;
  1792.         switch (static_cast<Color>(index)) {
  1793.         case Color::red:
  1794.             asnwer = "red";
  1795.             break;
  1796.         case Color::green:
  1797.             asnwer = "green";
  1798.             break;
  1799.         case Color::orange:
  1800.             asnwer = "orange";
  1801.             break;
  1802.         default:
  1803.             asnwer = "unknown";
  1804.             break;
  1805.             break;
  1806.         };
  1807.         return asnwer;
  1808.     };
  1809. };
  1810.  
  1811. template<>
  1812. struct Traits<Fruit>
  1813. {
  1814.     enum class Fruit { apple, orange, pear };
  1815.  
  1816.     static string name(int index)
  1817.     {
  1818.         string asnwer;
  1819.         switch (static_cast<Fruit>(index)) {
  1820.         case Fruit::apple:
  1821.             asnwer = "apple";
  1822.             break;
  1823.         case Fruit::orange:
  1824.             asnwer = "orange";
  1825.             break;
  1826.         case Fruit::pear:
  1827.             asnwer = "pear";
  1828.             break;
  1829.         default:
  1830.             asnwer = "unknown";
  1831.             break;
  1832.         };
  1833.         return asnwer;
  1834.     };
  1835. };
  1836.  
  1837.  
  1838. void TemplateSpecializationOperations()
  1839. {
  1840.     int t = 0; std::cin >> t;
  1841.  
  1842.     for (int i = 0; i != t; ++i) {
  1843.         int index1; std::cin >> index1;
  1844.         int index2; std::cin >> index2;
  1845.         cout << Traits<Color>::name(index1) << " ";
  1846.         cout << Traits<Fruit>::name(index2) << "\n";
  1847.     }
  1848. }
  1849.  
  1850. //********************Debugging Messages Order
  1851. class Message {
  1852. private:
  1853.     static int cur_id;
  1854.     string text;
  1855. public:
  1856.     int id;
  1857.     //Message() {};
  1858.     //Message(const Message& msg) : id(cur_id++) {};
  1859.     Message() : id(cur_id++) {};   
  1860.  
  1861.     const string& get_text() {
  1862.         return text;
  1863.     }
  1864.  
  1865.     string set_text(const string& elem) {
  1866.         return text = elem;
  1867.     }
  1868.  
  1869.     bool operator <(const Message& a) {
  1870.         if (a.id > this->id) return true;
  1871.         if (a.id < this->id) return false;
  1872.         return 0;
  1873.     };
  1874. };
  1875.  
  1876. int Message::cur_id = 0;
  1877.  
  1878. class MessageFactory {
  1879. public:
  1880.     MessageFactory() {}
  1881.     Message create_message(const string& text) {
  1882.         Message m;
  1883.         m.set_text(text);
  1884.         return m;
  1885.     }
  1886. };
  1887.  
  1888. //A solution using a precise clock (I really liked that one, and it is better)
  1889. //#include <chrono>
  1890. //
  1891. //class Message {
  1892. //  string msg;
  1893. //  chrono::time_point<chrono::high_resolution_clock> sent_at;
  1894. //public:
  1895. //  Message(string msg) : msg(msg) { sent_at = chrono::high_resolution_clock::now(); }
  1896. //  const string& get_text() {
  1897. //      return msg;
  1898. //  }
  1899. //
  1900. //  friend bool operator <(Message& left, Message& right) {
  1901. //      return left.sent_at < right.sent_at;
  1902. //  }
  1903. //};
  1904. //
  1905. //class MessageFactory {
  1906. //public:
  1907. //  MessageFactory() {}
  1908. //  Message create_message(const string& text) {
  1909. //      return Message(text);
  1910. //  }
  1911. //};
  1912.  
  1913. class Recipient {
  1914. public:
  1915.     Recipient() {}
  1916.     void receive(const Message& msg) {
  1917.         messages_.push_back(msg);
  1918.     }
  1919.     void print_messages() {
  1920.         fix_order();
  1921.         for (auto& msg : messages_) {
  1922.             cout << msg.get_text() << endl;
  1923.         }
  1924.         messages_.clear();
  1925.     }
  1926. private:
  1927.     void fix_order() {
  1928.         sort(messages_.begin(), messages_.end());
  1929.     }
  1930.     vector<Message> messages_;
  1931. };
  1932.  
  1933. class Network {
  1934. public:
  1935.     static void send_messages(vector<Message> messages, Recipient& recipient) {
  1936.         // simulates the unpredictable network, where sent messages might arrive in unspecified order
  1937.         random_shuffle(messages.begin(), messages.end());
  1938.         for (auto msg : messages) {
  1939.             recipient.receive(msg);
  1940.         }
  1941.     }
  1942. };
  1943.  
  1944. void MessagesExchangerClient() {
  1945.     MessageFactory message_factory;
  1946.     Recipient recipient;
  1947.     vector<Message> messages;
  1948.     string text;
  1949.     int msglimit = 0; // I just putted this to simulate an end to the task (:
  1950.     while (getline(cin, text) && msglimit < 3) {
  1951.         messages.push_back(message_factory.create_message(text));
  1952.         msglimit++;
  1953.     }
  1954.     Network::send_messages(messages, recipient);
  1955.     recipient.print_messages();
  1956. };
  1957.  
  1958. //****************************ATTENDING WORKSHOPS (Data and Structures problem) //Interval scheduling Problem
  1959. //#include<bits/stdc++.h>
  1960.  
  1961. //Define the structs Workshops and Available_Workshops.
  1962. struct Workshop
  1963. {
  1964.     int startTime,
  1965.         duration,
  1966.         endtime;
  1967. };
  1968.  
  1969. struct Available_Workshops
  1970. {
  1971. public:
  1972.     int signedWorkshops;
  1973.     vector<Workshop> *data = new vector<Workshop>(signedWorkshops);
  1974. private:
  1975.     //Workshop* workShops = new Workshop[signedWorkshops];
  1976. };
  1977.  
  1978. //Implement the functions initialize and CalculateMaxWorkshops
  1979. Available_Workshops* initialize(int start_time[], int duration[], int n) {
  1980.     Available_Workshops* aw = new Available_Workshops();
  1981.     for (int i = 0; i < n; i++) {
  1982.         Workshop* w = new Workshop;
  1983.         w->startTime = start_time[i];
  1984.         w->duration = duration[i];
  1985.         w->endtime = abs(w->startTime + w->duration);  
  1986.     }
  1987.     return aw;
  1988. };
  1989.  
  1990. int CalculateMaxWorkshops(Available_Workshops* ptr) {
  1991.     return 0;
  1992. };
  1993.  
  1994. void ExecWorkshops() {
  1995.     int n; // number of workshops
  1996.     cin >> n;
  1997.     // create arrays of unknown size n
  1998.     int* start_time = new int[n];
  1999.     int* duration = new int[n];
  2000.  
  2001.     for (int i = 0; i < n; i++) {
  2002.         cin >> start_time[i];
  2003.     }
  2004.     for (int i = 0; i < n; i++) {
  2005.         cin >> duration[i];
  2006.     }
  2007.  
  2008.     Available_Workshops * ptr;
  2009.     ptr = initialize(start_time, duration, n);
  2010.     cout << CalculateMaxWorkshops(ptr) << endl;
  2011. }
  2012.  
  2013.  
  2014. void PlayLambdas() {
  2015.     int a = 3, b = 10;
  2016.  
  2017.     auto k = [&a](int c) {return a * 4; };
  2018.  
  2019.     cout << k(6);
  2020. };
  2021.  
  2022.  
  2023.  
  2024. int main()
  2025.  {
  2026.  
  2027.     PlayLambdas();
  2028.  
  2029.     //MessagesExchangerClient();
  2030.    
  2031.     /*Message m;
  2032.     string text;
  2033.     cin >> text;
  2034.     MessageFactory mf;
  2035.     while (getline(cin, text)) {
  2036.         m = mf.create_message(text);
  2037.     }
  2038.     cout << "\n" << m.get_text();*/
  2039.  
  2040.  
  2041.  
  2042.     //Accessing Inherited Functions Tests:
  2043.     //D d;
  2044.     //int new_val;
  2045.     //new_val = 2;
  2046.     //d.check(new_val);
  2047.  
  2048.     //Exception test:
  2049.     //int n;
  2050.     //cin >> n;
  2051.     //process_input(n);
  2052.  
  2053.  
  2054.     //RectangleArea r_area;
  2055.     //r_area.read_input();
  2056.     //r_area.Rectangle::display();
  2057.     //r_area.display();
  2058.  
  2059.     //Calculator c;
  2060.     //c.DivisorCalc();
  2061.     //c.SumCalc();
  2062.     //c.Multiply();
  2063.  
  2064.  
  2065.     //DequeChalenge();
  2066.  
  2067.     //Demon d;
  2068.     //d.set_age(444);
  2069.     //cout << d.to_string();
  2070.  
  2071.     //InlineExample e;
  2072.     //e.initialize(10, 20);
  2073.     //e.display();
  2074.  
  2075.     //std::cout << ret2.returnArea();
  2076.     //LoopOperation();
  2077.     //max_of_four(3, 4, 6, 5);
  2078.     //PointersOperation();
  2079.     //cout << abs(-120);
  2080.     //int a = 4, b = 5;
  2081.     //int* a_ptr = &a, *b_ptr = &b;
  2082.     //update(a_ptr, b_ptr);
  2083.     //cout << a, b;
  2084.     //ArraysOperation();
  2085.     //VariableSizedArrays4();
  2086.     //for (int i = 0; i < LIMIT; ++i)
  2087.     //  cout << i << "\n";
  2088.     //int l1 = 2, l2 = 10;
  2089.     //cout << AREA(l1, l2);
  2090.     //PreprocessorSolution2();
  2091.  
  2092.     //OperatorOverload();
  2093.     //OverloadOperators_ComplexNumberCalculator();
  2094.  
  2095.  
  2096.     //ClassesAndObjectsOperations();
  2097.  
  2098.     //checkBox();
  2099.  
  2100.     //VectorSortOperations();
  2101.  
  2102.     //VectorErraseOperations();
  2103.  
  2104.  
  2105.     //LowerBoundSTLOperations();
  2106.  
  2107.     //TestLowerBound();
  2108. }
  2109.  
  2110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement