Download presentation
Presentation is loading. Please wait.
Published byΖεφύρα Μιχαηλίδης Modified over 6 years ago
1
Miscellaneous Stuff Which there wasn’t enough time to cover
And this still isn’t everything… Fall 2004 CS-183 Dr. 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 [] Fall 2004 CS-183 Dr. 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) Fall 2004 CS-183 Dr. 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 > > Fall 2004 CS-183 Dr. 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 Fall 2004 CS-183 Dr. 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 Fall 2004 CS-183 Dr. Mark L. Hornick
7
Algorithm Functions (2)
unique – Remove all adjacent duplicates replace – Substitute on match remove – Remove on match reverse – Swap order Fall 2004 CS-183 Dr. 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 Fall 2004 CS-183 Dr. 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 Fall 2004 CS-183 Dr. Mark L. Hornick
10
Iterator Types ++ (all) *: Assign from/to *: Rvalue/lvalue input
output forward bidirectional -- list<T>::iterator random access vector<T>::iterator +=, -=, <, [], … needed for algorithm’s sort Fall 2004 CS-183 Dr. 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 Fall 2004 CS-183 Dr. Mark L. Hornick
12
For More Information Highlight “algorithm” in MSVC, press F1, select “algorithm header file” for complete list Fall 2004 CS-183 Dr. Mark L. Hornick
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.