https://rentry.org/PPP2_p369

// Code derived from Stroustrup's PPP2 book
// § 10.11.1 In-memory representation
//  -and beginning on p 369

#include <iostream>
#include <vector>

using namespace std;

//------------------------------------------------------------------------------

const int not_a_reading = -7777;  // less than absolute zero
const int not_a_month   = -1;

// a day of temperature readings, organized by hour
struct Day {
  vector<double> hour{vector<double>(24, not_a_reading)};
};

// a month of temperature readings, organized by day
struct Month {
  int         month{not_a_month};  // [0:11] January is 0
  vector<Day> day{32};             // [1:31] one vector of readings per day
};

// a year of temperature readings, organized by month
struct Year {
  int           year;       // positive == A.D.
  vector<Month> month{12};  // [0:11] January is 0
};

int main()
{
  Year  y{};
  Month m{};
  Day   d{};

  cout << y.month.size() << '\n'  //
       << m.day.size() << '\n'    //
       << d.hour.size() << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p369.cpp && ./a.out

PrevUpNext

Edit
Pub: 07 Apr 2023 22:36 UTC
Edit: 03 May 2023 10:12 UTC
Views: 425