Guest User

Untitled

a guest
Dec 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Brief: C++17 Structured bindings experiments.
  2. // Author: Caio Rodrigues
  3. //--------------------------------------------------
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <deque>
  8. #include <map>
  9. #include <tuple>
  10. #include <iomanip>
  11. #include <algorithm>
  12.  
  13. int main(){
  14.  
  15. {
  16. std::puts("\n=== EXPERIMENT 1: Decompose: pair - binding - pair =====");
  17. auto p = std::pair<std::string, int>("C++", 17);
  18. auto [name, version] = p;
  19. std::cout << " name = " << name << " ; version = " << version << std::endl;
  20. }
  21.  
  22. {
  23. std::puts("\n=== EXPERIMENT 2: Decompose: map / 'hash-table' - =====");
  24. auto database = std::map<std::string, int>{{"c++", 17}, {"rust", 10}, {"Forth", 200}};
  25. for(const auto& [key, value] : database)
  26. std::cout << "key = " << key << " ; value = " << value << std::endl;
  27. }
  28.  
  29. {
  30. std::puts("\n=== EXPERIMENT 3: Decompose: tuple - =====");
  31. using DatabaseRow = std::tuple<int, std::string, double>;
  32. auto database = std::vector<DatabaseRow>{
  33. {100, "Fried tasty fresh bacon", 3.45},
  34. {400, "Super hot toasted coffee.", 6.25},
  35. {500, "Fresh Orange Juice", 4.50},
  36. };
  37.  
  38. for(const auto& [id, name, price]: database)
  39. std::cout << " ROW=> id = " << id
  40. << " ; name = " << name
  41. << " ; price = " << price
  42. << std::endl;
  43. }
  44.  
  45. {
  46. std::puts("\n=== Decompose: tuple with Reference - =====");
  47. using DatabaseRow = std::tuple<int, std::string, double>;
  48. auto row = DatabaseRow{200, "Coffee", 4.5};
  49. auto& [id, name, price] = row;
  50.  
  51. std::printf(" [BEFORE] => Product{ id = %d ; name = %s ; price = %f }\n",
  52. std::get<0>(row), std::get<1>(row).c_str(), std::get<2>(row));
  53.  
  54. id = 500, name = "italian-sytle coffee", price = 10.60;
  55.  
  56. std::printf(" [AFTER] => Product{ id = %d ; name = %s ; price = %f }\n",
  57. std::get<0>(row), std::get<1>(row).c_str(), std::get<2>(row));
  58. }
  59.  
  60. {
  61. std::puts("\n=== EXPERIMENT 4: Decompose: Structs - =====");
  62. struct GeoCoordinate{
  63. std::string name;
  64. double latitude;
  65. double longitude;
  66. };
  67. auto geoDatabase = std::deque<GeoCoordinate>{
  68. {"Bogota", 4.7110, -74.0721}
  69. ,{"Beijing", 39.9042, 116.4074}
  70. ,{"Gauteng", -26.2708, 28.1123}
  71. ,{"Buenos Aires", -34.6037, -58.3816}
  72. ,{"Brasilia", -15.8267, -47.9218}
  73. };
  74. std::cout << std::setprecision(3);
  75. std::for_each(geoDatabase.begin(), geoDatabase.end(),
  76. [](const auto& city){
  77. const auto& [name, lat, lon] = city;
  78. std::cout << std::setw(15) << std::left << name
  79. << std::setw(8) << std::right << lat
  80. << std::setw(8) << lon
  81. << "\n";
  82.  
  83. });
  84. }
  85.  
  86. {
  87. std::puts("\n=== EXPERIMENT 5: Decompose: C-Array - =====");
  88. double array [3] = {10.23, 90.23, 100.0};
  89. auto [x, y, z] = array;
  90. std::printf(" array { x = %.3f ; y = %.3f ; z = %.3f }", x, y, z);
  91.  
  92. }
  93. return 0;
  94. }
Add Comment
Please, Sign In to add comment