120 likes | 227 Views
Miscellaneous Stuff. Which there wasn’t enough time to cover And this still isn’t everything…. Managing new ed pointers (1). Wouldn’t it be nice if new ed memory were released when pointer went out of scope? The auto_ptr (auto pointer) class from the standard library does this.
E N D
Miscellaneous Stuff • Which there wasn’t enough time to cover • And this still isn’t everything… CS-183Dr. Mark L. Hornick
Managing newed pointers (1) • Wouldn’t it be nice if newed memory were released when pointer went out of scope? • The auto_ptr (auto pointer) class from the standard library does this. • An object “owns” the pointer and calls delete when destroyed (destructor) • But not delete [] CS-183Dr. Mark L. Hornick
Managing newed pointers (2) #include <memory> // auto_ptr ... void f() { ... auto_ptr<MyClass> apObject(new MyClass(...)); apObject->function(...); ... } // no delete (apObject's dtor// takes care of it) CS-183Dr. Mark L. Hornick
Managing newed pointers (3) • Containers of objects (vector<MyClass>) only hold a single object type • Polymorphism: use virtual functions and pointers to hold any derived type (vector<MyBaseClass*>) • Must delete each object before losing its pointer • Add auto_ptr‘s “auto delete” feature… • vector< auto_ptr< MyBaseClass > > CS-183Dr. Mark L. Hornick
Algorithm Library • About 50 methods for containers and other objects • Makes extensive use of templates • Access #include <algorithm> using std::…; • Limitations • Places restrictions on some objects CS-183Dr. Mark L. Hornick
Algorithm Functions (1) • find – Match value, if not found return last • Uses operator== • find_if – Match using generic exp • count – How many matches? (operator==) • count_if – Count matching generic exp • partial_sum – sum of 1st through Nth element CS-183Dr. Mark L. Hornick
Algorithm Functions (2) • unique – Remove all adjacent duplicates • replace – Substitute on match • remove – Remove on match • reverse – Swap order CS-183Dr. Mark L. Hornick
Other Container Functions • random_shuffle – Requires indexing • merge – Two sorted sequences • sort – Sort a sequence • Uses operator < • Requires indexing (random access) • list<> has its own version CS-183Dr. Mark L. Hornick
Non-Container Functions • min – smallest of two • max – largest of two • swap – switch values between two objects • iter_swap – same, but do the swap given iterators CS-183Dr. Mark L. Hornick
forward bidirectional random access Iterator Types ++ (all) *: Assign from/to *: Rvalue/lvalue input output list<T>::iterator -- vector<T>::iterator +=, -=, <, [], … needed for algorithm’s sort CS-183Dr. Mark L. Hornick
for_each • Does the same thing to all objects in a sequence • May not modify objects • Requires a function that has a single parameter • Data type in the container cell void printSquare(TYPE in); for_each(li.begin(), li.end(), printSquare); // func. Name CS-183Dr. Mark L. Hornick
For More Information • Highlight “algorithm” in MSVC, press F1, select “algorithm header file” for complete list CS-183Dr. Mark L. Hornick