90 likes | 118 Views
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.
E N D
CSCI-383 Object-Oriented Programming & Design Lecture 28
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
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()
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
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
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
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
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
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