C++ Lecture 9: Custom Comparison Operators, STL - Programming
Document Details
![HeartfeltPopArt](https://quizgecko.com/images/avatars/avatar-13.webp)
Uploaded by HeartfeltPopArt
Tags
Summary
This document presents a lecture on C++ programming covering key concepts such as custom comparison operators, templates, the Standard Template Library (STL), and STL algorithms including iterators. It explores generic programming techniques and provides practical examples to solve various programming problems, with a focus on the efficient use of the STL.
Full Transcript
1 Lecture #9 Custom Comparison Operators Templates The Standard Template Library (STL) STL Iterators STL Algorithms (sort, etc.) On-your-own Study: – Inline Functions, Template Exercise, More STL Algorithms 2...
1 Lecture #9 Custom Comparison Operators Templates The Standard Template Library (STL) STL Iterators STL Algorithms (sort, etc.) On-your-own Study: – Inline Functions, Template Exercise, More STL Algorithms 2 WRITES 50 DIFFERENT SORT FUNCTIONS Implement polymorphism in C++. DATA WAS ALREADY SORTED Generic Programming What’s the big picture? Generic programming is when you write a function or class in a manner so that it can process many different types of data. Like a generic sorting function that can sort an array holding AN Y type of value Or a generic linked list class that can hold AN Y type of variable int main() { Create a linked list holding list list_of_integers; any type of data in ONE list list_of_strings; LINE! Uses: list_of_integers.push_back(42); Used in virtually every list_of_strings.push_back(“It’s LIT!”); C++ program to speed } up development – search engines, video games, Once you define such a generic function or class, you can quickly reuse it etc. to solve many different problems. 4 Part 1: Allowing Generic Comparisons Consider the following main function that compares various objects to each other… int main() Notice that the way we compare two dogs (by { weight) is int i1 = 3, i2 = 5; different than the way we if (i1 > i2) compare two circles (by radius). cout b) > b.radius()) cout spot) > (fido.weight() cout =(const Dog &a, NOTE: if (a = b.getWeight()) return true; from your class! return false; // otherwise } 6 Custom Comparison Operators bool operator>=(const Dog &a, const Dog &b) And here’s how they work! { 5 3 if (a.getWeight() >= b.getWeight()) return true; else return false; } If you forget the const If we define an operator like == does C++ classkeyword… Dog Simply using the operator in Oh, andautomatically by the way…define since arelated and b operators are const, Answer: { yourNo! Youcauses code must define each C++ to call our function public: operator explicitly! like !=can only call const functions in for us? your comparison int getWeight() function! const Dog! Thinking time! { So you’d better make int main() return m_weight; getWeight() const too! { } fido weight Dog... fido(5), spot(3); 5 if (fido >= spot) private: You’ll see this kind of cryptic cout int b. getBite()) For instance, this function will place dogs return true if A belongs before n.push_back("alex"); return true; // Dog a has a nastier bite! with a bigger bite before dogs with a smaller B if (a.getBite() < b.getBite()) bite… // sorts returnthe whole false; vectorb has a nastier bite! // Dog sort ( n.begin( ), n.end(The ) ); sort() function uses the return false if A belongs after return a.getBark() > b.getBark(); B. } // sorts just the firstpassed-in 2 items offunction n to figure out int main() how sort ( n.begin( ), n.begin() + 2to);order the items! { And break tiesfunction’s by the You then pass this Dog arr int arr == {5,2,1,-7}; {...}; address asloudest bark… a parameter to sort()! // sorts the first 4 array items } sort sort( arr, ( arr,arr+4, arr+4&customCompare); ); } 65 Part 6: Compound STL Data Structures Let’s say you want to maintain a #include list of courses for each UCLA #include crsMap student. class Course “carey” c1 c2 { public: “david” c1 c3 How could you do it … with the STL? }; int main() Well, how about creating a map between { a map< , > crsmap; student’s stringname and their Course c1(“cs”,”32”), list of courses? list c2(“math”,”3b”), c3(“english”,”1”); In many cases, you’ll want to combine crsmap[“carey”].push_back(c1); multiple STL containers to represent crsmap[“carey”].push_back(c2); more complex associations like this! crsmap[“david”].push_back(c1); crsmap[“david”].push_back(c3); 66 STL Challenges Design a compound STL data structure that allows us to associate people (Person objects) and each person’s unique group of friends (also Person objects). Remember: If you’re mapping your own class class Person toThinking time! something else you’ll need to { define the operator< for it. public: Person(string name); string getName();... Answer: private:... map facebook ; }; and: Also, if youbool have a set containing operator