// // list test // #include #include // listを使用する using namespace std; const int MaxNameLen = 20; class Student { char name[MaxNameLen]; int point; public: void setName(const char *n) { strncpy(name, n, MaxNameLen); } char *getName() { return name; } void setPoint(int po) { point = po; } int getPoint() { return point; } void print() { cout << "name=" << name << " point=" << point << endl; } }; void print_list(list stlst) { int total_sum = 0; cout << "size=" << stlst.size() << endl; for (list::iterator i = stlst.begin(); i != stlst.end(); i++) { i->print(); total_sum += i->getPoint(); } cout << "average=" << ((double)total_sum / stlst.size()) << endl; } int main() { list stlst; // Studentクラスのリスト (サイズ 0) int total_sum; Student st; // リストに追加 (サイズが増加) st.setName("Hamm"); st.setPoint(7); stlst.push_back(st); st.setName("Slink"); st.setPoint(4); stlst.push_back(st); st.setName("Potato"); st.setPoint(6); stlst.push_back(st); // リストの内容表示 cout << "=== phase 1 ===\n"; print_list(stlst); // リストに追加 (サイズが増加) st.setName("Woody"); st.setPoint(8); stlst.push_back(st); st.setName("Sarge"); st.setPoint(9); stlst.push_back(st); // リストの内容表示 cout << "=== phase 2 ===\n"; print_list(stlst); return 0; }