CSE2050 Fall 2002 Exam #1, Open book, open notes. Name __________________ 1. If p and q are iterators, circle the operations that are valid if they are random iterators, but not if they are bidirectional (2 pts each). p==q p (using the most appropriate parameter passing technique) and printing (in ascending order) a list of keys for which the value is true (25 pts). ANSWER void print(const map& m) { for (map::const_iterator p=m.begin(); p!=m.end(); ++p) if (p->second) cout << p->first << endl; } 5. On the back, write an appropriate header file for p1.cpp and p2.cpp. Show the g++ command to compile and link them into a single program (30 pts) // p1.cpp #include #include "p.h" void set(Record& x, const std::string& name, int age) { x.name = name; x.age = age; } // p2.cpp #include #include #include "p.h" using namespace std; int main() { Record r; set(r, "Matt", 47); cout << r.name << " " << r.age << endl; return 0; } ANSWER // p.h #include struct Record { // Must be before declaration for set() std::string name; // No "using namespace std;" int age; }; void set(Record&, const std::string&, int); // parameter names optional To compile: g++ p1.cpp p2.cpp