fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. /*
  6. 1. Pointer/Iterator
  7. * /
  8. int angka;
  9. angka = 1;
  10. int* p;
  11. p = &angka;
  12. cout << angka << endl;
  13. (*p) = 3;
  14. cout << angka << endl;
  15. /*
  16. STL = Standard Template Library
  17. 2. STL Containers
  18. - string -> (dynamic) array of char
  19. - vector -> array dinamis
  20. - pair -> 2 data sembarang
  21. - set -> himpunan -> unique, terurut
  22. - map -> pemetaan/fungsi
  23. - stack -> LIFO
  24. - queue -> FIFO
  25. - priority_queue -> autosorted
  26. * /
  27. char cstr[10] = "KOMPUTER"; // array of char
  28. char cstr2[10];
  29. // cstr2 = cstr;
  30. // if(cstr == cstr) {
  31. //
  32. // }
  33.  
  34.  
  35. // string
  36. string str = "KOMPUTER";
  37. string str2;
  38. str2 = str;
  39. cout << str2 << endl;
  40. if(str == str2) {
  41. cout << "SAMA" << endl;
  42. }
  43. cout << str.size() << endl;
  44.  
  45. // vector
  46. int arr[100];
  47. arr[0] = 0;
  48. arr[99] = 0;
  49. vector<int> vec;
  50. cout << vec.size() << endl;
  51. for(int i = 1; i <= 10; i++)
  52. vec.push_back(i);
  53. cout << vec.size() << endl;
  54. for(int i = 0; i < vec.size(); i++)
  55. cout << vec[i] << " ";
  56. cout << endl;
  57. for(auto it = vec.rbegin(); it != vec.rend(); it++)
  58. cout << (*it) << " ";
  59. cout << endl;
  60.  
  61. // pair
  62. pair<string, int> nilai;
  63. nilai.first = "Ronald";
  64. nilai.second = 100;
  65. double x = 0, y = 0, z = 0;
  66. pair<pair<double, double>, double> koordinat;
  67. koordinat.first.first = x;
  68. koordinat.first.second = y;
  69. koordinat.second = z;
  70.  
  71. vector<pair<string, int> > vnilai;
  72. vnilai.push_back(nilai);
  73. vnilai.push_back(make_pair("Jono", 30));
  74.  
  75. // set
  76. set<int> himp;
  77. himp.insert(10);
  78. himp.insert(-1);
  79. himp.insert(3);
  80. himp.insert(3); // sudah ada, jadi diabaikan
  81. cout << "Ukuran himp: " << himp.size() << endl;
  82. if(himp.find(4) != himp.end())
  83. cout << "Angka 4 ditemukan dalam himpunan" << endl;
  84. else
  85. cout << "Angka 4 tidak ditemukan dalam himpunan" << endl;
  86. for(auto it = himp.begin(); it != himp.end(); it++)
  87. cout << (*it) << " ";
  88. cout << endl;
  89.  
  90. // map
  91. map<string, int> dict;
  92. dict["P0001"] = 0;
  93. dict["P0002"] = 1;
  94. dict["P0003"] = 2;
  95. vector<string> rdict(3);
  96. rdict[0] = "P0001";
  97. rdict[1] = "P0002";
  98. rdict[2] = "P0003";
  99. string IDbaru = "P0004";
  100. if(dict.find(IDbaru) == dict.end()) {
  101. dict[IDbaru] = rdict.size();
  102. rdict.push_back(IDbaru);
  103. }
  104. for(auto it = dict.begin(); it != dict.end(); it++)
  105. cout << (*it).first << " " << it->second << endl;
  106. for(int i = 0; i < rdict.size(); i++)
  107. cout << i << " " << rdict[i] << endl;
  108.  
  109. // stack -> Last In, First Out
  110. /* 1 2 k 3 4 k 5 k k k
  111. ^
  112. .
  113. .
  114. .
  115. .
  116. .
  117. keluar: 2 4 5 3 1
  118. * /
  119. stack<int> st;
  120. st.push(2);
  121. st.push(5);
  122. st.push(3);
  123. st.push(1);
  124. st.push(4);
  125. while(!st.empty()) {
  126. cout << st.top() << " ";
  127. st.pop();
  128. }
  129. cout << endl;
  130. // queue -> First In, First Out
  131. queue<int> qu;
  132. qu.push(2);
  133. qu.push(5);
  134. qu.push(3);
  135. qu.push(1);
  136. qu.push(4);
  137. while(!qu.empty()) {
  138. cout << qu.front() << " ";
  139. qu.pop();
  140. }
  141. cout << endl;
  142. // priority_queue -> autosorted
  143. priority_queue<int> pq;
  144. pq.push(2);
  145. pq.push(5);
  146. pq.push(3);
  147. pq.push(1);
  148. pq.push(4);
  149. while(!pq.empty()) {
  150. cout << pq.top() << " "; // HEAP
  151. pq.pop();
  152. }
  153. cout << endl;
  154. */
  155. /*
  156. 3. STL Functions
  157. - sort -> pair
  158. - next_permutation
  159. - lower_bound
  160. - upper_bound
  161. * /
  162. // sort
  163. int arr[] = {2, 5, 3, 1, 4};
  164. sort(arr, arr+5);
  165. for(int i = 0; i < 5; i++)
  166. cout << arr[i] << " ";
  167. cout << endl;
  168. vector<int> vec = {2, 5, 3, 1, 4};
  169. sort(vec.begin(), vec.end());
  170. for(int i = 0; i < 5; i++)
  171. cout << vec[i] << " ";
  172. cout << endl;
  173.  
  174. vector<pair<int, int> > koord;
  175. koord.push_back(make_pair(1, 2));
  176. koord.push_back(make_pair(1, 0));
  177. koord.push_back(make_pair(0, 0));
  178. koord.push_back(make_pair(-2, 3));
  179. koord.push_back(make_pair(2, 2));
  180. sort(koord.begin(), koord.end());
  181. for(int i = 0; i < koord.size(); i++)
  182. cout << koord[i].first << " " << koord[i].second << endl;
  183.  
  184. int arr[] = {1, 2, 3, 3};
  185. do {
  186. cout << arr[0] << " " << arr[1] << " " << arr[2] << " " << arr[3] << endl;
  187. } while(next_permutation(arr, arr+4));
  188. /*
  189. 0 1 2 3 3
  190. 1 1 3 2 3
  191. 2 1 3 3 2
  192. 3 2 1 3 3
  193. 4 2 3 1 3
  194. 5 2 3 3 1
  195. 6 3 1 2 3
  196. 7 3 1 3 2
  197. 8 3 2 1 3
  198. 9 3 2 3 1
  199. 10 3 3 1 2
  200. 11 3 3 2 1
  201. * /
  202. // linear search -> O(n)
  203. // binary search (data sudah urut) -> O(log(n))
  204. // data[] = {1, 1, 2, 2, 2, 3, 5, 5, 8, 9, 11, 13, 15, 16, 16}
  205. // |--------------------|-------------------------|
  206. // |----------|-----------|
  207. // | | |
  208. // |
  209. // cari = 11
  210. // L U
  211. int data[] = {1, 1, 2, 2, 2, 3, 5, 5, 8, 9, 11, 13, 15, 16, 16};
  212. cout << lower_bound(data, data+15, 2)-data << endl;
  213. cout << upper_bound(data, data+15, 2)-data << endl;
  214. */
  215.  
  216. return 0;
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246. // LATIHAN: https://o...content-available-to-author-only...s.com/contests/ykpjm6
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
2
5