fork(1) download
  1. //
  2. // main.cpp
  3. // Lists (STL)
  4. //
  5. // Created by Himanshu on 18/09/21.
  6. //
  7.  
  8. #include <iostream>
  9. #include <list>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13.  
  14. // comparator to compare only integral part:
  15. bool comparator (double first, double second) {
  16. return ( int(first) < int(second) );
  17.  
  18. }
  19.  
  20. void printIntList (list<int> l) {
  21. list<int>::iterator it;
  22. for (it = l.begin(); it != l.end(); it++) {
  23. cout<<(*it)<<" ";
  24. }
  25. cout<<endl;
  26. }
  27.  
  28. void printFloatList (list<float> l) {
  29. list<float>::iterator it;
  30. for (it = l.begin(); it != l.end(); it++) {
  31. cout<<(*it)<<" ";
  32. }
  33. cout<<endl;
  34. }
  35.  
  36. int main () {
  37.  
  38. // Declaration
  39. list<float> first, second;
  40.  
  41. // Initialisation
  42. list<int> myListFirst({10, 20, 15});
  43.  
  44. // myListSecond is intialised with 3 ints
  45. // having value as 45
  46. list<int> myListSecond(3, 45);
  47.  
  48. cout<<"myListFirst elements:"<<endl;
  49. printIntList(myListFirst);
  50.  
  51. cout<<"myListSecond elements:"<<endl;
  52. printIntList(myListSecond);
  53.  
  54. myListFirst.swap(myListSecond);
  55.  
  56. cout<<"myListFirst elements after swap:"<<endl;
  57. printIntList(myListFirst);
  58. cout<<"myListSecond elements after swap:"<<endl;
  59. printIntList(myListSecond);
  60.  
  61. myListFirst.unique();
  62. cout<<"myListFirst elements after unique:"<<endl;
  63. printIntList(myListFirst);
  64.  
  65. myListFirst.clear();
  66. cout<<"myListFirst elements after clear:"<<endl;
  67. printIntList(myListFirst);
  68.  
  69. if (myListFirst.empty()) {
  70. cout<<"myListFirst is empty"<<endl;
  71. }
  72.  
  73. first.push_back (3.1);
  74. first.push_back (1.2);
  75. first.push_back (5.9);
  76.  
  77. cout<<"first list elements:"<<endl;
  78. printFloatList(first);
  79.  
  80. second.push_back (3.7);
  81. second.push_back (7.8);
  82. second.push_back (1.2);
  83.  
  84. cout<<"second list elements:"<<endl;
  85. printFloatList(second);
  86.  
  87. //sort method of lists sorts the data in increasing order of elements. list sort is different from algorithm sort
  88. first.sort();
  89. second.sort();
  90.  
  91. cout<<"first list elements after sort():"<<endl;
  92. printFloatList(first);
  93.  
  94. cout<<"second list elements after sort():"<<endl;
  95. printFloatList(second);
  96.  
  97. // merge method removes all the elements in second,
  98. // and inserts them into their ordered (sorted) position within first
  99. // provided both are sorted
  100. first.merge(second);
  101.  
  102. cout<<"first list elements after merge:"<<endl;
  103. printFloatList(first);
  104.  
  105.  
  106. // second is now empty after merge
  107. if (second.empty()) {
  108. cout<<"List second is empty after merge"<<endl;
  109. }
  110.  
  111. second.push_back(3.1);
  112. second.push_back(2.8);
  113. second.push_back(3.5);
  114.  
  115.  
  116. // Merge using a custom comparator which only
  117. // merge according to the relation specified
  118.  
  119. first.sort();
  120. second.sort();
  121. first.merge(second, comparator);
  122.  
  123. // Custom comparator compares only integer part
  124. // hence 3.1 is after 3.7
  125. std::cout << "first list after custom merge:"<<endl;
  126. printFloatList(first);
  127.  
  128. return 0;
  129. }
  130.  
Success #stdin #stdout 0s 5616KB
stdin
Standard input is empty
stdout
myListFirst elements:
10 20 15 
myListSecond elements:
45 45 45 
myListFirst elements after swap:
45 45 45 
myListSecond elements after swap:
10 20 15 
myListFirst elements after unique:
45 
myListFirst elements after clear:

myListFirst is empty
first list elements:
3.1 1.2 5.9 
second list elements:
3.7 7.8 1.2 
first list elements after sort():
1.2 3.1 5.9 
second list elements after sort():
1.2 3.7 7.8 
first list elements after merge:
1.2 1.2 3.1 3.7 5.9 7.8 
List second is empty after merge
first list after custom merge:
1.2 1.2 2.8 3.1 3.7 3.1 3.5 5.9 7.8