Deep initialization structs

#include <iostream> #include <vector> #include <string> #include <set> #include <ctime> using namespace std; struct Phone { int country_code; int city_code; int local_phone; }; struct Address { string street; string city; string country; }; struct Birthday { short day; short month; int year; }; struct Contact { string name; bool works_overtime; vector<string> colleagues; Phone phone; Address address; Birthday birthday; Contact(string n, bool ov, vector<string> col, Phone ph, Address add, Birthday bd) { name = n; works_overtime = ov; colleagues = col; phone = ph; address = add; birthday = bd; } }; int main() { // We don't need to call the Contact constructor for each item in the vector once the type is known vector<Contact> contacts = { {"Harry", true, {"Helen", "Lora", "Peter"}, {231, 44, 441734}, {"Main St. 45", "Coventry", "United Kingdom"}, {20, 7, 1989}}, {"Sonya", false, {"Nora", "Chris", "Manuel"}, {45, 12, 4566454}, {"Denea St. 23", "Birmingham", "United Kingdom"}, {14, 6, 1987}}, {"Gerhard", true, {"Clemens", "Wendy", "Simon"}, {33, 25, 45645645}, {"Yellow St. 112", "Liverpool", "United Kingdom"}, {9, 8, 1991}}, {"Helen", true, {"Harry", "Peter"}, {231, 44, 456456}, {"Colorado St. 12", "Coventry", "United Kingdom"}, {14, 7, 1987}}, {"Lora", false, {"Harry"}, {231, 44, 5633455}, {"West Timor 6", "Coventry", "United Kingdom"}, {12, 3, 1988}}, {"Peter", false, {"Harry", "Helen"}, {231, 35, 4564956}, {"Solitude St. 35", "Coventry", "United Kingdom"}, {11, 9, 1989}}, {"Nora", true, {"Sonya", "Chris"}, {45, 12, 378931}, {"Cox St. 35", "Birmingham", "United Kingdom"}, {17, 10, 1984}}, {"Chris", true, {"Sonya", "Nora"}, {45, 12, 773521}, {"Otto St. 45", "Birmingham", "United Kingdom"}, {13, 5, 1983}}, {"Manuel", false, {"Sonya"}, {45, 12, 6553212}, {"Lake St. 15", "Birmingham", "United Kingdom"}, {9, 12, 1987}}, {"Clemens", true, {"Gerhard", "Wendy"}, {33, 25, 4598694}, {"Sonix Avenue 33", "Liverpool", "United Kingdom"}, {11, 8, 1984}}, {"Wendy", false, {"Clemens", "Gerhard"}, {33, 25, 4594533}, {"Pottery St. 24", "Liverpool", "United Kingdom"}, {10, 2, 1986}}, {"Simon", true, {"Gerhard", "Clemens"}, {33, 25, 839320}, {"Cherry St. 67", "Liverpool", "United Kingdom"}, {2, 6, 1987}} }; vector<string> names; // Q: Which people tend to work overtime? for(const Contact contact:contacts) { if(contact.works_overtime) { cout << contact.name << " "; } } // A: Harry Gerhard Helen Nora Chris Clemens Simon cout << endl; // Q: On which streets live Gerhard's colleagues? vector<string> colleagues; for(const Contact contact:contacts) { if(contact.name == "Gerhard") { colleagues = contact.colleagues; for(const string colleague:colleagues) { for(const Contact contact:contacts) { if(contact.name == colleague) { cout << contact.address.street << ", "; } } } break; } } // A: Sonix Avenue 33, Pottery St. 24, Cherry St. 67 cout << endl; // Q: People in which cities participated in the study? set<string> cities; for(const Contact contact:contacts) { cities.insert(contact.address.city); } for(const string city:cities) { cout << city << " "; } // A: Birmingham Coventry Liverpool cout << endl; // Q: What are the names of the people who live in Coventry? for(const Contact contact:contacts) { if(contact.address.city == "Coventry") { cout << contact.name << " "; } } // A: Harry Helen Lora Peter cout << endl; // Q: What is the age difference between Nora and Clemens? vector<time_t> times; time_t rawtime; struct tm * t = localtime(&rawtime); Birthday bd; for(const Contact contact:contacts) { if(contact.name == "Nora" || contact.name == "Clemens") { bd = contact.birthday; t->tm_mday = bd.day; t->tm_mon = bd.month; t->tm_year = bd.year; time_t tt = mktime(t); times.push_back(tt); } } int diff = abs(times[0] - times[1]); cout << diff / (60 * 60 * 24) << " days" << endl; // A: 67 days return 0; }