Guest User

Untitled

a guest
Aug 9th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. /*
  2. This a header file that includes every standard library.
  3. You can use it to save time.
  4. NOTE: This header file may not be recognized by compilers
  5. other than gcc.
  6. */
  7. #include <bits/stdc++.h>
  8.  
  9. /*
  10. //Use this if the above header file doesn't work.
  11.  
  12. #include <iostream>
  13.  
  14. #include <algorithm>
  15. #include <list>
  16. #include <map>
  17. #include <priority_queue>
  18. #include <stack>
  19. #include <string>
  20. #include <vector>
  21. */
  22.  
  23. using namespace std;
  24.  
  25.  
  26. // These will be used later
  27. struct Person {
  28. string name;
  29. int age;
  30. } p1, p2, p3;
  31.  
  32. struct is_older {
  33. bool operator()(struct Person p1, struct Person p2) {
  34. return p1.age > p2.age;
  35. }
  36. };
  37.  
  38. bool compare_names(struct Person p1, struct Person p2) {
  39. return p1.name < p2.name;
  40. }
  41.  
  42. bool way_to_sort(int i, int j) { return i > j; }
  43.  
  44.  
  45. int main() {
  46.  
  47. /*
  48. ========
  49. STACK
  50. ========
  51. */
  52.  
  53. stack <string> distros; //Create a stack of strings.
  54.  
  55. distros.push("Ubuntu"); //Pushing elements into the stack.
  56. distros.push("Mint");
  57.  
  58. cout << "Number of distros in the stack are " << distros.size() << endl;
  59. cout << "Distro on the top is " << distros.top() << endl;
  60.  
  61. distros.pop();
  62. cout << "The top of the stack is now " << distros.top() << endl;
  63.  
  64. /*
  65. ========
  66. VECTOR
  67. ========
  68. */
  69.  
  70. vector <int> numbers;
  71.  
  72. if (numbers.empty()){ //check if the vector is empty?
  73. cout << "The vector is empty :(" << endl;
  74. }
  75.  
  76. for(int i=0; i<100; i+=10){ //Add some values to the vector
  77. numbers.push_back(i);
  78. }
  79.  
  80. cout << "Size of the vector is " << numbers.size() << endl;
  81.  
  82. // iterating over the vector, declaring the iterator
  83. vector <int>::iterator it;
  84.  
  85. cout << "The vector contains: ";
  86. for (it=numbers.begin(); it!=numbers.end(); it++) {
  87. cout << " " << *it;
  88. }
  89.  
  90. // getting value at particular position
  91. int position = 5;
  92. cout<<"\nVector at position "<<position<<" contains "<<numbers.at(position)<<endl;
  93.  
  94. // deleting an element at position
  95. numbers.erase(numbers.begin() + position);
  96. cout<<"Vector at position "<<position<<" contains "<<numbers.at(position)<<endl;
  97.  
  98. // deleting a range of elements, first two elements
  99. // NOTE: You may expect elements at 0, 1 and 2 to be deleted
  100. // but index 2 is not inclusive.
  101. numbers.erase(numbers.begin(), numbers.begin()+2);
  102. cout << "The vector contains: ";
  103. for (it=numbers.begin(); it!=numbers.end(); it++) {
  104. cout << " " << *it;
  105. }
  106.  
  107. // Clearing the vector
  108. numbers.clear();
  109. if (numbers.empty()){
  110. cout << "\nThe vector is now empty again :(";
  111. }
  112.  
  113.  
  114. /*
  115. =========
  116. HASHMAP
  117. =========
  118. */
  119.  
  120. // Declaration <key type, value type>
  121. map <string, string> companies;
  122.  
  123. companies["Google"] = "Larry Page";
  124. companies["Facebook"] = "Mark Zuckerberg";
  125.  
  126. // insertion can also be done as
  127. companies.insert(pair<string, string> ("Xarvis Tech", "xarvis"));
  128. // or
  129. companies.insert(map<string,string>::value_type("Quora", "Adam D'Angelo"));
  130. // or even
  131. companies.insert(make_pair(string("Uber"), string("Travis Kalanick")));
  132.  
  133. // Iterating the map
  134. map<string, string>::iterator itz;
  135. cout << "\n\nCompanies and founders" << endl;
  136. for (itz=companies.begin(); itz!=companies.end(); itz++){
  137. cout << "Company: " << (*itz).first << "\t Founder: " << itz->second <<endl;
  138. }
  139.  
  140. itz = companies.find("Google");
  141. cout << itz->second;
  142.  
  143. /*
  144. ==============
  145. LINKED LISTS
  146. ==============
  147. */
  148. list<int> mylist;
  149. list<int>::iterator it1,it2,itx;
  150.  
  151. // set some values:
  152. for (int i=1; i<10; ++i) mylist.push_back(10*i);
  153.  
  154. // 10 20 30 40 50 60 70 80 90
  155. it1 = it2 = mylist.begin(); // ^^
  156. advance (it2,6); // ^ ^
  157. ++it1; // ^ ^
  158.  
  159. it1 = mylist.erase (it1); // 10 30 40 50 60 70 80 90
  160. // ^ ^
  161.  
  162. it2 = mylist.erase (it2); // 10 30 40 50 60 80 90
  163. // ^ ^
  164.  
  165. ++it1; // ^ ^
  166. --it2; // ^ ^
  167.  
  168. mylist.erase (it1,it2); // 10 30 60 80 90
  169.  
  170. cout << "\nmylist contains:";
  171. for (itx=mylist.begin(); itx!=mylist.end(); ++itx)
  172. cout << ' ' << *itx;
  173. cout << '\n';
  174.  
  175. // NOTE: it1 still points to 40, and 60 is not deleted
  176. cout << endl << *it1 << "\t" << *it2 <<endl;
  177.  
  178. // This will print an unexpected value
  179. it1++;
  180. cout << *it1;
  181.  
  182. cout << "\nmylist now contains:";
  183. for (it1=mylist.begin(); it1!=mylist.end(); ++it1)
  184. cout << ' ' << *it1;
  185. cout << '\n';
  186.  
  187. /*
  188. =======
  189. HEAPS
  190. =======
  191. */
  192.  
  193. // Creates a max heap
  194. priority_queue <int> pq;
  195.  
  196. // To create a min heap instead, just uncomment the below line
  197. // priority_queue <int, vector<int>, greater<int> > pq;
  198.  
  199. pq.push(5);
  200. pq.push(1);
  201. pq.push(10);
  202. pq.push(30);
  203. pq.push(20);
  204.  
  205. // Extracting items from the heap
  206. while (!pq.empty())
  207. {
  208. cout << pq.top() << " ";
  209. pq.pop();
  210. }
  211.  
  212.  
  213. // creating heap from user defined objects
  214. // Let's initialize the properties of `Person` object first
  215. p1.name = "Linus Torvalds";
  216. p1.age = 47;
  217.  
  218. p2.name = "Elon Musk";
  219. p2.age = 46;
  220.  
  221. p3.name = "Me!";
  222. p3.age = 19;
  223.  
  224. // Initialize a min heap
  225. // Note: We defined a comparator is_older in the beginning to
  226. // compare the ages of two person.
  227. priority_queue <struct Person, vector<struct Person>, is_older> mh;
  228. mh.push(p1);
  229. mh.push(p2);
  230. mh.push(p3);
  231.  
  232. // Extracting items from the heap
  233. while (!mh.empty())
  234. {
  235. struct Person p = mh.top();
  236. cout << p.name << " ";
  237. mh.pop();
  238. }
  239.  
  240. /*
  241. =========
  242. SORTING
  243. =========
  244. */
  245.  
  246. // The following list type initialization is only supported after C++11
  247. //vector<int> int_vec = {56, 32, -43, 23, 12, 93, 132, -154};
  248.  
  249. // If the above style of initialization doesn't work, use the following one
  250. static int arr[] = {56, 32, -43, 23, 12, 93, 132, -154};
  251. int arr_len = sizeof(arr) / sizeof(arr[0]);
  252. vector <int> int_vec(arr, arr + arr_len);
  253.  
  254. cout << endl;
  255. // Default: sort ascending
  256. // sort(int_vec.begin(), int_vec.end());
  257. // To sort in descending order:
  258. // Do not include the () when you call wayToSort
  259. // It must be passed as a function pointer or function object
  260. sort(int_vec.begin(), int_vec.end(), way_to_sort);
  261. for (vector <int>::iterator i = int_vec.begin(); i!=int_vec.end(); i++)
  262. cout << *i << " ";
  263. cout << endl;
  264.  
  265. // sorting the array
  266. sort(arr, arr + arr_len);
  267. for (int i=0; i < arr_len; i++) {
  268. cout << arr[i] << " ";
  269. }
  270.  
  271. // Sorting user-defined objects
  272. static struct Person persons[] = {p1, p2, p3};
  273. sort(persons, persons+3, compare_names);
  274.  
  275. // This will printout the names in alphabetical order
  276. for (int i=0; i < 3; i++) {
  277. cout << persons[i].name << " ";
  278. }
  279.  
  280. return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment