fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. void print(set<int> x){
  6. set <int>::iterator itr;
  7. for(itr = x.begin(); itr != x.end(); itr++){
  8. cout << *itr << " ";
  9. }
  10. cout << '\n';
  11. }
  12.  
  13. int main(){
  14.  
  15.  
  16.  
  17. //--------------S E T--------------//
  18.  
  19. // -> A set is a container that stores unique element following a specific order
  20.  
  21. // Declaring a set
  22.  
  23. set <int> a;
  24.  
  25.  
  26. // empty() -> Returns whether the set is empty or not
  27. if(a.empty()){
  28. cout << "Set empty" << '\n';
  29. }
  30.  
  31.  
  32. // inserting elements in random order
  33.  
  34. a.insert(60);
  35. a.insert(40);
  36. a.insert(10);
  37. a.insert(80);
  38. a.insert(50);
  39. a.insert(20);
  40. a.insert(30);
  41.  
  42. // inserting 30 again, but only one 30 will be added to the set
  43. a.insert(30);
  44.  
  45. // Printing set
  46.  
  47. set <int>::iterator it;
  48.  
  49.  
  50. // The elements will be stored in ascending order
  51. // OUTPUT -> 10 20 30 40 50 60 80
  52. cout << "Elements of set are : ";
  53. for(it = a.begin(); it != a.end(); it++){
  54. cout << *it << " ";
  55. }
  56. cout << '\n';
  57.  
  58.  
  59.  
  60.  
  61. // size()
  62. cout << "Size of set A -> ";
  63. cout << a.size() << '\n';
  64. // OUTPUT -> 7
  65.  
  66.  
  67. // To store the elements in descending order
  68. set <int, greater<int>> b;
  69.  
  70. b.insert(60);
  71. b.insert(40);
  72. b.insert(10);
  73. b.insert(80);
  74. b.insert(50);
  75. b.insert(20);
  76. b.insert(30);
  77. b.insert(30);
  78.  
  79. // OUTPUT -> 80 60 50 40 30 20 10
  80. cout << "Elements of set B in descending order are : ";
  81. for(it = b.begin(); it != b.end(); it++){
  82. cout << *it << " ";
  83. }
  84. cout << '\n';
  85.  
  86.  
  87.  
  88.  
  89. // Assigning the elements from A to C
  90. set <int> c(a.begin(), a.end());
  91.  
  92. cout << "Elements of set C are : ";
  93. print(c);
  94. // OUTPUT -> 10 20 30 40 50 60 80
  95.  
  96.  
  97.  
  98.  
  99. // Remove all elements up to 40 in C
  100. c.erase(c.begin(), c.find(40));
  101.  
  102. cout << "Remove all elements up to 40 in C : ";
  103. print(c);
  104. // OUTPUT -> 40 50 60 80
  105.  
  106.  
  107.  
  108.  
  109. // Remove specific element in C
  110.  
  111. c.erase(50);
  112.  
  113. cout << "Remove specific element in C : ";
  114. print(c);
  115. //Before -> 40 50 60 80
  116. //After -> 40 60 80
  117.  
  118.  
  119.  
  120. cout << "Count : \n";
  121. // Count() -> returns 1 if element is present in the set
  122. // returns 0 if element is not present in the set
  123. //set A -> 10 20 30 40 50 60 80
  124. cout << a.count(50) << '\n'; // 50 is present so OUTPUT -> 1
  125. cout << a.count(100) << '\n'; // 100 is not present so OUTPUT -> 0
  126.  
  127.  
  128.  
  129.  
  130. // emplace() -> used to insert a new element into the set, only if the element to be inserted is unique and does not already exists in the set.
  131.  
  132. // set C elements -> 40 60 80
  133. // emplace 30 <- doesn't exit
  134. // emplace 80 <- already exists
  135.  
  136. c.emplace(30);
  137. c.emplace(80);
  138.  
  139. cout << "emplace() : ";
  140. print(c); // OUTPUT -> 30 40 60 80
  141.  
  142.  
  143.  
  144.  
  145. // array to set
  146. int arr[]={12,75,10,32,20,25};
  147. set <int> d (arr,arr+6); // 10,12,20,25,32,75
  148.  
  149. cout << "array to set : ";
  150. print(d);
  151. //OUTPUT -> 10 12 20 25 32 75
  152.  
  153.  
  154.  
  155.  
  156. // swap() -> Exchanges the content of the set
  157.  
  158. // set C -> 30 40 60 80
  159. // set D -> 10 12 20 25 32 75
  160.  
  161. c.swap(d);
  162.  
  163. cout << "swap() : \n";
  164. print(c); // OUTPUT -> 10 12 20 25 32 75
  165. print(d); // OUTPUT -> 30 40 60 80
  166.  
  167.  
  168.  
  169.  
  170. // find() -> searching an element within a set
  171. // set A -> 10 20 30 40 50 60 80
  172. int val = 50;
  173.  
  174. if(a.find(val) != a.end())
  175. cout<< "The set contains "<< val << '\n';
  176. else
  177. cout<< "The set does not contains "<< val << '\n';
  178.  
  179.  
  180.  
  181. // Copy set to vector
  182.  
  183. //set A -> 10 20 30 40 50 60 80
  184. vector <int> v(a.size()); //a.size() is used to reserve enough space in the vector to hold the content of set A
  185. copy(a.begin(), a.end(), v.begin());
  186.  
  187. cout << "Copy set A to vector V : "; // OUTPUT -> 10 20 30 40 50 60 80
  188. for(auto i:v){
  189. cout << i << " ";
  190. }
  191. cout << '\n';
  192. }
Success #stdin #stdout 0s 5580KB
stdin
Standard input is empty
stdout
Set empty
Elements of set are : 10 20 30 40 50 60 80 
Size of set A -> 7
Elements of set B in descending order are : 80 60 50 40 30 20 10 
Elements of set C are : 10 20 30 40 50 60 80 
Remove all elements up to 40 in C : 40 50 60 80 
Remove specific element in C : 40 60 80 
Count : 
1
0
emplace() : 30 40 60 80 
array to set : 10 12 20 25 32 75 
swap() : 
10 12 20 25 32 75 
30 40 60 80 
The set contains 50
Copy set A to vector V : 10 20 30 40 50 60 80