fork download
  1. //
  2. // main.cpp
  3. // Pair
  4. //
  5. // Created by Himanshu on 11/04/22.
  6. //
  7.  
  8. #include <iostream>
  9. #include <utility>
  10. #include <vector>
  11. #include <string>
  12. using namespace std;
  13.  
  14. int main () {
  15.  
  16. //Initializations
  17.  
  18. //using make_pair
  19. pair<int, int> pairRank = make_pair(1, 98);
  20.  
  21. //using value init
  22. pair<int, string> pairNameFirst (1, "Anon");
  23.  
  24. //using copy constructor
  25. pair<int, string> pairTemp(pairNameFirst);
  26.  
  27.  
  28. cout<<"pairRank values:"<<endl;
  29. cout<<pairRank.first<<", "<<pairRank.second<<endl;
  30.  
  31. //accessing pair elements using method get<>
  32. cout<<"pairNameFirst values:"<<endl;
  33. cout<<get<0>(pairNameFirst)<<", "<<get<1>(pairNameFirst)<<endl<<endl;
  34.  
  35. vector<pair<int, int>> pairRankVector;
  36. vector<pair<int, string>> pairNameVector;
  37.  
  38. vector<pair<int, int>>::iterator rankItr;
  39. vector<pair<int, string>>::iterator nameItr;
  40.  
  41. pairRankVector.push_back(pairRank);
  42. pairNameVector.push_back(pairNameFirst);
  43.  
  44. vector<int> rankMarks = {95, 90, 87, 83, 75};
  45. vector<string> rankNames = {"Bruw", "Cync", "Droke", "Elph", "Frel"};
  46.  
  47.  
  48. int n = (int) rankMarks.size();
  49.  
  50. for (int i=0; i<n; i++) {
  51. pairRankVector.push_back(make_pair(i+2, rankMarks[i]));
  52. pairNameVector.push_back(make_pair(i+2, rankNames[i]));
  53. }
  54.  
  55. cout<<"Size of pairRankVector: "<<pairRankVector.size()<<endl<<endl;
  56.  
  57. //accessing vector elements using iterator,
  58. //accessing pair elements using -> operator
  59. //accessing vector element using (*iterator)
  60. for (rankItr = pairRankVector.begin(), nameItr = pairNameVector.begin();
  61. rankItr != pairRankVector.end(); rankItr++, nameItr++) {
  62.  
  63. cout<<rankItr->first<<" "<<nameItr->second<<" "<<(*rankItr).second<<endl;
  64. }
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
pairRank values:
1, 98
pairNameFirst values:
1, Anon

Size of pairRankVector: 6

1 Anon 98
2 Bruw 95
3 Cync 90
4 Droke 87
5 Elph 83
6 Frel 75