// File: C++Examples/Lists/prof2A.h #ifndef __prof #define __prof using namespace std; // is default but should be referred explicity #include #include #include #include #include #include #include #include class PROF { basic_string lname; // concept demo'd: basic_string template class // avoid the use of char[] altogether basic_string fname; char MI; double salary; int rank; float TPA; // ``teaching point average'' public: /* needed if explictly instantiating list */ PROF(void) { } /* */ PROF(const basic_string& LN, const basic_string& FN, const char M, const double S, int R, float T) : lname(LN), fname(FN), MI(M), salary(S), rank(R), TPA(T) {} // constructor initializes fields bool operator<(const PROF& professor) const // needed in STL lower_bound algo {return lname < professor.lname; } bool operator==(const PROF& professor) const // needed in hire {return lname == professor.lname; } class comp_w_lastname { // needed by find_if in fire // concepts demo'd: predicate object, encapsulated class basic_string last_name; public: comp_w_lastname(const basic_string& name) : last_name(name) {} bool operator()(const PROF& professor) const {return professor.lname >= last_name;} }; // end class comp_w_lastname friend class PROF::comp_w_lastname; // accesses lname friend ostream& operator<<(ostream&, const PROF&);// accesses lname,fname template class C> friend class PROFLIST; // needed in fire }; // end class PROF template class CONTAINER> // template-template parameters class PROFLIST : public CONTAINER { // CONTAINER can be list or vector (STL sequence container) public: typedef CONTAINER PROF_CONTAINER; int fire(const basic_string&); int hire(const PROF&); // friend ostream& operator<< <>(ostream&, const PROFLIST&); }; // end template class PROFLIST template class CONTAINER> int PROFLIST::hire(const PROF& goodprof) {// insert goodprof in alphabetic order; // returns 0 if successful, 1 if goodprof already there, // -1 if no more memory bool found; typename PROF_CONTAINER::iterator p =std::lower_bound(PROFLIST::PROF_CONTAINER::begin() ,PROFLIST::PROF_CONTAINER::end() ,goodprof); // STL search using PROF::operator< // returns the first valid place to insert goodprof and preserve order // concept demo'd: STL lower_bound algo // Note: on doubly linked lists, binary search performs fewer // comparisions than a sequential search. // Note: std::lower_bound refers to the STL algorithm // set also has a member function, which is more efficient if (p == PROFLIST::PROF_CONTAINER::end()) found = false; else if (*p == goodprof) found = true; else found = false; if(found) return 1; // already there if(PROFLIST::PROF_CONTAINER::size() == PROFLIST::PROF_CONTAINER::max_size() ) return -1; // out of memory // max_size is how large the list can get at most PROFLIST::PROF_CONTAINER::insert(p, goodprof); // STL insertion into the list // concept demo'd: STL insert return 0; } // end hire template class CONTAINER> int PROFLIST::fire(const basic_string& badprof) {// remove element from sorted list; // return 0 if removal was successful; typename PROF_CONTAINER::iterator p = find_if(PROFLIST::PROF_CONTAINER::begin() ,PROFLIST::PROF_CONTAINER::end() ,PROF::comp_w_lastname(badprof)); // STL search using (encapsulated) predicate object // concept demo'd: STL find_if, usage of function object // Note: lower_bound could be more efficient, as it only // performs log(size) comparision. if (p == PROFLIST::PROF_CONTAINER::end()) return 1; // not found if ((*p).lname != badprof) return 1; // not found PROFLIST::PROF_CONTAINER::erase(p); return 0; } // end fire #endif