Advertisement
alansam

Structing

May 24th, 2023 (edited)
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <ranges>
  5.  
  6. struct test {
  7.   uint32_t ID;
  8.   std::string name;
  9.  
  10.   struct point {
  11.     double xx;
  12.     double yy;
  13.     double zz;
  14.   } point;
  15. };
  16.  
  17. struct testing {
  18.   uint32_t ID;
  19.   std::string name;
  20.  
  21.   struct point {
  22.     double xx;
  23.     double yy;
  24.     double zz;
  25.   } point;
  26.  
  27.   testing(uint32_t ID_ = 0u,
  28.           std::string name_ = "",
  29.           double xx_ = 0.0, double yy_ = 0.0, double zz_ = 0.0)
  30.   : ID { ID_ }, name { name_ }, point { .xx = xx_, .yy = yy_, .zz = zz_ } {}
  31. };
  32.  
  33. int main(int argc, char const * argv[]) {
  34.   auto testpt = [](test const & tt) {
  35.     std::cout << std::fixed << std::setprecision(3);
  36.     std::cout << std::setw(3)  << tt.ID
  37.               << std::setw(15) << tt.name
  38.               << std::setw(7)  << tt.point.xx
  39.               << std::setw(7)  << tt.point.yy
  40.               << std::setw(7)  << tt.point.zz
  41.               << '\n';
  42.   };
  43.  
  44.   auto testingpt = [](testing const & tt) {
  45.     std::cout << std::fixed << std::setprecision(3);
  46.     std::cout << std::setw(3)  << tt.ID
  47.               << std::setw(15) << tt.name
  48.               << std::setw(7)  << tt.point.xx
  49.               << std::setw(7)  << tt.point.yy
  50.               << std::setw(7)  << tt.point.zz
  51.               << '\n';
  52.   };
  53.  
  54.   test t1 { 90u, "Test 90", { 8.4, 2.5, 9.9, } };
  55.   testpt(t1);
  56.  
  57.   test t2 { .ID = 91u, .name = "Test 91", .point = { 7.3, 1.4, 8.8, } };
  58.   testpt(t2);
  59.  
  60.   test t3 { .ID = 92u, .name = "Test 92", .point = { .xx = 6.1, .yy = 0.3, . zz = 7.7, } };
  61.   testpt(t3);
  62.  
  63.   test t4 { 93u, "Test 03", 5.0, -0.2, 6.6, };
  64.   testpt(t4);
  65.  
  66.   testing s0;   //  take defaults
  67.   s0.name = "Testing 500";
  68.   s0.point.yy = 11.02;
  69.   testingpt(s0);
  70.  
  71.   testing s1(501u, "Testing 501", 11.1, 12.2, 13.3);
  72.   testingpt(s1);
  73.  
  74.   return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement