340 likes | 354 Views
Overview of Containers. Standard Template Language (STL) organizes its classes into 3 categories Sequence Containers Arrays and Vectors (provides index access to data) List/ sequence Deque (can move either end, but not middle) Adapter Containers Stacks (last in, first out)
E N D
Overview of Containers • Standard Template Language (STL) organizes its classes into 3 categories • Sequence Containers • Arrays and Vectors (provides index access to data) • List/ sequence • Deque (can move either end, but not middle) • Adapter Containers • Stacks (last in, first out) • Queue (first in, first out) • Priority Queue (deletion returns the largest/smallest value • Associative Containers • Set and Multiset/ bag (elements are the information – ordering is by content) • Map/ Multimap (key-data relationship; associative pairs) • All the container classes are templated and what the container does and how it works is independent of elements it contains
What are Containers? • A Container is a data structure whose main purpose is to store and retrieve a large number of values • Containers are abstract data types (ADTs) that hold values. They don't change the content – only hold it so that it can be retrieved later. • These data structure permits storage and retrieval of data items independent of content. The two fundamental operations of any container are: • Put(C,x): Insert a new data item x into the container C. • Get(C): Retrieve the next item from the container C. Different types of containers support different retrieval orders, based on insertion order or position. • Items in Containers are referred to by special objects called: iterators.
What are Containers? • The STL consists of 10 container classes categorized according to the ordering of the elements and the different types of operations that access the data • Sequence Containers: store data by position in linear order, 1st, 2nd, 3rd, etc. • Associative Containers: store elements by key e.g. name, ssn, part numbers etc. A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container • Adaptor Containers: contain other containers as their underlying storage structure.
Sequence Containers – Vector • We talked about this already?? • A vector is a generalized array that stores a collection of elements of the same data type • Similar to an array – a vector allows access to its elements by using an index in the range from 0 to n-1, where n is the size of the vector • Unlike an array – a vector has operations that allow collection of the elements to grow dynamically at the rear of the sequence to meet the runtime needs of an application • Allows direct access to its elements through the index What is the disadvantage of insertion or deletion within the interior of the sequence??
Inserting into a List Container Sequence Containers - Lists • A list is a data structure that stores elements by position • Each item in the list has both a value and a memory address (pointer) that identifies the next item in the sequence Value 1 Pointer to V2 Value 2 Pointer to V3 Value n • Disadvantage: unlike a vector, a list is not a direct-access structure • In order to access a specific data value in the list, it is necessary to search from position 1 and follow the pointers from element to element until the data value is located • Advantage: unlike a vector, a list is able to add a remove items efficiently at any position in the sequence • Insertion requires 2 updates – ideal for applications that require sequential access to data while also requiring frequent insertion and deletion of elements
Associative Containers – Stacks & Queues • Associative containers store elements by key – in what application is this useful?? • Ex: name, social security number, or part number. • A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container. • Containers are typically most useful when they will contain only a limited number of items and when the retrieval order is predefined or irrelevant. The most popular type of containers are: • Stacks: Supports retrieval in last in, first out order (LIFO). Stacks are simple to implement, and very efficient. Indeed, stacks are probably the right container to use when the retrieval order doesn't matter at all, as when processing batch jobs. The put and get operations for stacks are usually called push and pop. • Queues: Supports retrieval in first in, first out order (FIFO). FIFO may seem the fairest way to control waiting times. However, for many applications, data items have infinite patience. Queues are trickier to implement than stacks and are appropriate only for applications (like certain simulations) where the order is important. The put and get operations for queues are usually called enqueue and dequeue. • Stacks ad Queues cab ve implemented using either arrays or linked lists
Associative Containers – Stacks & Queues • Both stacks and queues are storage containers that restrict how elements enter and leave a sequence
Stack Containers A stack allows access at only one end of the sequence, called the top
Queue Containers A queue is a container that allows access only at the front and rear of the sequence.
Priority Queue Containers A priority queue is a storage structure that has restricted access operations similar to a stack or queue. Elements can enter the priority queue in any order. Once in the container, a delete operation removes the largest (or smallest) value.
Associative Containers – Maps/ Multimaps A set is a collection of unique values, called keys or set members.
Associative Containers – Sets/ Multisets A map is a storage structure that implements a key-value relationship.
Stacks • Further Stack Analogies • Pushing/ Popping a Stack • Class Stack • Mutlibase • Uncoupling Stack
A sequence of items which are accessible only at the top Stacks
C B B D A A A A A Push A Push B Push C Pop B Push D Stack - LIFO • Since pop removes the item last pushed into the stack – it is a Last In First out LIFO ordering container B A Pop C
CLASS stack CLASS stack <stack> <stack> Constructor Operations stack(); Create an empty stack bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise. CLASS Stack
CLASS stack <stack> Operations void pop(); Remove the item from the top of the stack. Precondition: The stack is not empty. Postcondition: Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top. CLASS Stack
CLASS stack <stack> Operations int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition: The stack is not empty. const T& top() const; Constant version of top(). CLASS Stack
Using a Stack to Create a Hex Number 1 1 A A A A F F F F F F Refer to Multi-base program 7-1, page 334
Uncoupling Stack Elements Refer to code on page 337 using “target”
Uncoupling using “target” e 1 pop 3 d e 2 pop 2 c d target push b 1 b d a a e stack s stack s tmpStk Refer to code on page 337 using “target”
29 Main Index Contents
Precedence Symbol Input precedence Stack precedence Rank + - 1 1 -1 * / % 2 2 -1 ^ 4 3 -1 ( 5 -1 0 ) 0 0 0 30 Main Index Contents Infix Expression Rules The figure below gives input precedence, stack precedence, and rank used for the operators +, -, *, /, %, and ^, along with the parentheses. Except for the exponential operator ^, the other binary operators are left-associative and have equal input and stack precedence.
31 Main Index Contents Summary Slide 1 §- Stack - Storage Structure with insert (push) and erase (pop) operations occur at one end, called the top of the stack. - The last element in is the first element out of the stack, so a stack is a LIFO structure.
32 Main Index Contents Summary Slide 2 §- Recursion - The system maintains a stack of activation records that specify: 1) the function arguments 2) the local variables/objects 3) the return address - The system pushes an activation record when calling a function and pops it when returning.
33 Main Index Contents Summary Slide 3 §- Postfix/RPN Expression Notation - places the operator after its operands - easy to evaluate using a single stack to hold operands. - The rules: 1) Immediately push an operand onto the stack. 2) For a binary operator, pop the stack twice, perform the operation, and push the result onto the stack. 3) At the end a single value remains on the stack. This is the value of the expression.
34 Main Index Contents Summary Slide 4 §- Infix notation - A binary operator appears between its operands. - More complex than postfix, because it requires the use of operator precedence and parentheses. - In addition, some operators are left-associative, and a few are right-associative.