//map3.cc /* This program reads name-course-grade triples such as Art cs1 B Sue cs1 A Bob Math1 C Art cs2 D Sue cs2 B Bob Math2 B Art cs3 B Sue cs3 A and for each person calculates a set of courses they have passed: Art: cs1 cs3 Bob: Math1 Math2 Sue: cs1 cs2 cs3 */ #include #include #include #include int main() { std::string as[] = {"A+","A","A-","B+","B","B-","C"}; //passing grades typedef std::set SSet; // set of strings SSet acceptable( as, as + sizeof(as)/sizeof(as[0]) ); // construct set using all elements of the array typedef std::map SMap; // map strings into SSets SMap m; for(std::string n, c, g; std::cin >> n >> c >> g ; ) // name course grade if ( acceptable.count( g ) /* > 0 */ ) // if grade is in passing set m[n].insert( c ); for(SMap::iterator it = m.begin(), stop=m.end(); it != stop; it++) { std::cout << it->first << ": "; // output person's name for(SSet::iterator p = it->second.begin(), q=it->second.end(); p != q; p++) std::cout << *p << ' '; // output each course passed std::cout << '\n'; } }