Lec17STLContainers_6per.pdf - EECS 402 Review of Templates...

Info icon This preview shows pages 1–2. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 17 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators STL Algorithms EECS 402 EECS 402 Andrew M Morgan 2 Review of Templates C++ templates allow you to use one function, or class, to perform the same operations on many different data types Templated functions are often used for functions such as swap, which are often used for any data type Templated classes are usually used for data structures Data structures, such as list, queue, stack, etc, can be used to store any type of data in a specific way Having a stack of gadgets makes just as much sense as having a stack of integers Templates allow multiple data types to be templated That means you can use templates to create data structures that are even more complex than lists, queues, etc.. EECS 402 EECS 402 Andrew M Morgan 3 Intro To The STL It is well known that you will often need these data structures in your programming careers Since every programmer will need them, it seems silly to have you program it every time you come across a need Instead, there exists the C++ Standard Template Library, also known as the STL The STL is essentially a library that provides most of the generic data structures you will need The STL has been proven to be correct Special considerations were taken to ensure maximum efficiency, so you are unlikely to write a "better" list, etc. Since the STL is fully templated, you can use these structures on any data type, even classes you define EECS 402 EECS 402 Andrew M Morgan 4 Container Classes A container class is simply a class which can hold many objects during program execution There are three types of container classes: Sequence container: A container class which stores data in a specific (user placed) order Associative container: A container class which provides direct access to store and retrieve elements via keys - the user (you) need not know, or care what order data is stored in Container Adapter: Similar to container classes, but with restricted functionality - container adapters do not provide iterators The sequence containers and the associative containers are called "first-class containers" since they contain a complete set of operations EECS 402 EECS 402 Andrew M Morgan 5 Iterators Iterators are essentially "pointers" into the data structure Actually, they are objects that point into the structure Many operations are allowed to be performed on iterators, though, making them seem like pointers (i.e. dereference, etc) There are four types of iterators iterator : This type of iterator can be be used to read or write from data structure. Increments move the iterator forward const_iterator : This type of iterator can only be used to read.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern