1 / 9

Understanding STL Containers & Data Structures

This lecture provides an overview of STL containers, their capabilities, and methods. It covers sequential, associative, and adaptor containers, as well as input and output using iterators.

Télécharger la présentation

Understanding STL Containers & Data Structures

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. CSCI-383 Object-Oriented Programming & Design Lecture 28

  2. Containers • Data structures capable of storing objects of almost any data type • The containers in the STL may contain instances of builtin types or user-defined types (i.e., classes) • The classes are expected to implement the following methods • A default and a copy constructor • An assignment operator • An equality operator • A less than operator

  3. Containers • STL containers are ADTs • All containers are parameterized by the types they contain • Each container declares an iterator • Each container has methods for iterators • begin() • end()

  4. Containers • There are three categories of STL containers • Sequential: linear data structures • vector • deque • list • Associative: can locate elements in container quickly • set • multiset • map • multimap • Adaptor: constrained versions of sequential containers • stack • queue • priority queue

  5. STL Container Header Files <vector> <list> <deque> <queue> Contains both queue and priority_queue <stack> <map> Contains both map and multimap <set> Contains both set and multiset <valarray> “Near container”, fast math vector operations <bitset> “Near container”, maintain sets of flag values

  6. Iterator Types Supported by Containers • Sequential • vector random access • deque random access • list bidirectional • Associative • set bidirectional • multiset bidirectional • map bidirectional • multimap bidirectional • Adaptor • stack no iterators supported • queue no iterators supported • priority queue no iterators supported

  7. Input and Output with Iterators • We use iterators with sequences. These sequences can be in containers, or they can be input sequences or output sequences • Handout #17

  8. Sequential Containers • The C++ STL provides three sequential containers – vector, list and deque • Class template vector and class template deque are both based on arrays. Class template list implements a linked-list data structure

  9. vector • One of the most popular containers in the STL is vector • A vector changes size dynamically • Unlike C and C++ pointer-based “raw” arrays, vectors can be assigned to one another • Insertion at the back of a vector is efficient (constant time). The vector simply grows, if necessary, to accomodate the new item • Constant time searching • It is expensive to insert (or delete) an element in the middle of a vector (vector elements occupy contiguous cells in memory just as C or C++ “raw” arrays do) • Handout #18 • Handout #19

More Related