1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::vector;
using std::cout;
using std::endl;
using std::ostream;
using std::string;
enum Section
{
ORCHESTRA,
FRONT,
MIDDLE,
BACK,
};
struct Date
{
Date() :
month(1),
day(1),
year(1900)
{
}
Date(int month, int day, int year) :
month(month),
day(day),
year(year)
{
}
int month;
int day;
int year;
};
struct Seat
{
Seat() :
assigned(false)
{
}
Seat(char row, int column, Section section, string name, Date date) :
row(row),
column(column),
section(section),
name(name),
date(date),
assigned(true)
{
}
char row;
int column;
Section section;
string name;
Date date;
bool assigned;
};
ostream& operator<<(ostream& os, const Seat& s)
{
string sectionstr;
if (s.section == FRONT)
{
sectionstr = "FRONT";
}
else if (s.section == MIDDLE)
{
sectionstr = "MIDDLE";
}
else if (s.section == BACK)
{
sectionstr = "BACK";
}
else
{
sectionstr = "ORCHESTRA";
}
os << s.row << s.column << " " << sectionstr << " \""<<
s.name << "\" " << s.date.month << "/" << s.date.day << "/" <<
s.date.year << (s.assigned ? " ASSIGNED" : " NOT ASSIGNED") << endl;
return os;
}
struct SeatSorter
{
bool operator()(const Seat& a, const Seat& b)
{
return a.name < b.name;
}
};
struct SeatPtrSorter
{
bool operator()(const Seat* a, const Seat* b)
{
return a->name < b->name;
}
};
int main()
{
// Or 2D array; doesn't matter
vector<Seat> all_seats =
{
{ 'B', 47, ORCHESTRA, "John Doe", {4, 28, 2018} },
{ 'C', 22, FRONT, "Waylon Dalton", {5, 11, 2018} },
{ 'D', 16, MIDDLE, "Marcus Cruz", {9, 30, 2018} },
{ 'E', 4, MIDDLE, "Eddie Randolph", {11, 31, 2018} },
{ 'F', 55, MIDDLE, "Hadassah Hartman", {6, 30, 2018} },
{ 'G', 20, ORCHESTRA, "Justine Henderson", {6, 4, 2018} },
{ 'H', 33, FRONT, "Thalia Cobb", {5, 11, 2018} },
{ 'I', 1, BACK, "Angela Walker", {8, 2, 2018} },
{ 'J', 18, ORCHESTRA, "Joanna Shaffer", {7, 7, 2018} },
{ 'K', 4, BACK, "Abdullah Lang", {8, 8, 2018} },
{ 'L', 56, FRONT, "Mathias Little", {10, 31, 2018} },
{ 'M', 12, MIDDLE, "Lia Shelton", {12, 6, 2018} },
{ 'N', 3, ORCHESTRA, "Jonathon Sheppard", {6, 16, 2018} },
};
// Show all seats
for (auto& seat : all_seats)
cout << seat;
cout << endl;
// Some iteration (doesn't matter) to store particular seats
vector<Seat*> some_subset;
for (int i=0; i<all_seats.size(); ++i)
{
if (i&1)
some_subset.push_back(&all_seats[i]);
}
// Some subset
cout << "Subset with \"odd\" seats\n";
for (auto seat_ptr : some_subset)
cout << *seat_ptr;
// Sorted subset
std::sort(some_subset.begin(), some_subset.end(), SeatPtrSorter());
cout << "\nSorted subset\n";
for (auto seat_ptr : some_subset)
cout << *seat_ptr;
}
|