Advertisement
AbhijitPaul

Set Example

Jul 7th, 2022
699
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None
  1. #+begin_src c++
  2.   #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. void print(map<int, string> m);
  7. void print(vector<pair<int, string>> m);
  8. void print(set<pair<int, string>> m);
  9.  
  10. int main(){
  11.   set<pair<int,string>> test_set;
  12.   test_set.insert({1201, "Abhijit Paul"});
  13.   test_set.insert({1202, "Sazzad Hossain"});
  14.   test_set.insert({1203, "Shazzad Hossain"});
  15.   test_set.insert({1230, "Arnab Banik"});
  16.  
  17.   for(auto itr: test_set ){
  18.     itr.second = itr.second+" ALIVE";
  19.   }
  20.  
  21.   print(test_set); // NO change
  22.  
  23.   printf("After using &:\n");
  24.   /* for(auto& itr: test_set ){
  25.     itr.first = itr.first-1200; // ERROR HERE
  26.     itr.second = itr.second+" ALIVE"; // ERROR HERE
  27.     }*/
  28.  
  29.   for(auto& itr: test_set ){
  30.     test_set.erase(itr);
  31.     test_set.insert({itr.first-1200, itr.second+" ALIVE"});
  32.   }
  33.  
  34.   print(test_set);
  35.  
  36.  
  37.   return 0;
  38. }
  39.  
  40.  
  41. void print(map<int, string> m){
  42.   for(const auto itr: m) cout<<itr.first<<","<<itr.second<<endl;
  43. }
  44. void print(vector<pair<int, string>> m){
  45.   for(const auto itr: m) cout<<itr.first<<","<<itr.second<<endl;
  46. }
  47. void print(set<pair<int, string>> m){
  48.   for(const auto itr: m) cout<<itr.first<<","<<itr.second<<endl;
  49. }
  50.  
  51.  
  52.  
  53. #+end_src
  54.  
Advertisement
RAW Paste Data Copied
Advertisement