1 / 12

Miscellaneous Stuff

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.

zariel
Download Presentation

Miscellaneous Stuff

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Miscellaneous Stuff • Which there wasn’t enough time to cover • And this still isn’t everything… CS-183Dr. Mark L. Hornick

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. Algorithm Functions (2) • unique – Remove all adjacent duplicates • replace – Substitute on match • remove – Remove on match • reverse – Swap order CS-183Dr. Mark L. Hornick

  8. 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

  9. 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

  10. 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

  11. 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

  12. For More Information • Highlight “algorithm” in MSVC, press F1, select “algorithm header file” for complete list CS-183Dr. Mark L. Hornick

More Related