Title: Data Structures and the Standard Template Library
1Data Structuresand theStandard Template Library
2Review of Classes
- Construct to define a data type
- data (usually private)
- functions (private or public)
- Used to create objects of that type
- Public part is the interface
- Application Programming Interface (API)
- Constructors
- Operations
- Header File
3Time24 Example
- time24(int h0, int m0)
- //initializes hours to h and minutes to m
- void setHours(int h)
- //Postcondition hours contains the value h
- void setMinutes(int m)
- //Postcondition The minutes contains the value
m - int getHours()
- //Returns the value of hours
- int getMinutes()
- //Returns the value of minutes
- time24 duration(const time24 t)
- //Precondition t must not be earlier than the
current time - //Return the length of time between t and the
current time - void operator(int m)
- //Postcondition adds m minutes to current time
4Simple Classes
- Constructors
- Member functions
- parameter types
- const
- Implementation
- Overloading operators
- members
- friends
- File Set up for Use
5Templates and Dynamic Memory
- Templates
- The Big-3
- Destructors
- Copy constructors
- Assignment operators
6Data Structures
- A systematic way of organizing and accessing data
- Modeled with an abstract data type
- Gives an implementation-independent view of the
data structure - Formally
- Operation name
- Preconditions
- Postconditions
- Return
7Standard Template Library
- Sequence Containers
- Vector
- List
- Dequeue
- Adapters
- Stack
- Queue
- Priority-queue
- Associative Containers
- Map, multimap
- Set, multiset
8Vector Class
- Allows direct access by index
- Dynamic array
- Not efficient for insertion/deletion at an
arbitrary position
9Vector Class
- vector()
- vector(int n, cont T value T() )
- vector(T first, T last)
- T back()
- bool empty () const
- T operator (int i)
- void push_back(const T value)
- void pop_back()
- void resize(int n, const T fill T())
- int size() const
10Adaptors
- Stacks
- Accessible at one end
- Last-in/first-out (LIFO)
- Queues
- Add at one end called the back
- Remove from the other end called the front
- First-in/first-out (FIFO)
11Stack API
stack() Create an empty stack bool empty ()
const Check whether or not the stack is
empty. Return true false otherwise 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 top. 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. int size() const Return the
number of items on the stack T top() Return
a reference to the value of the item on the top.
Precondition The stack is not empty const
T top() const Constant version of top()
12Queue API
queue() Create an empty queue bool empty()
const Check whether the queue is empty.
Return true if is, false otherwise T front()
Return a reference to the value of the item at
the form of the queue Precondition The queue
is not empty. const T front() const
Constant version of from. void pop() Remove
the item from the front of the queue.
Precondition The queue is not empty.
Postcondition The queue is empty or it has a
new front element. void push(const T item)
Insert the argument item at the back of the
queue. Postcondition The queue has a new
item at the back. int size() const Return
the number of elements on the queue.
13List Container
- Linear structure
- Linked
- No random access to items
- Must iterate through list to find items
- Iteration done with iterators
14List API
list() Create an empty list. Default
constructor. list(int n, const T value T())
Create a list with n elements, each having a
specified value. If the value argument
is omitted, the elements are filled with the
default value for type T. Type T must
have a default constructor. list (T first, T
last) Initialize the list, using the address
range first, last) T back() Return the
value of the item at the back of the list
Precondition The list must contain at least on
element. bool empty() const Return true if
the list is empty , false otherwise. T front()
Return the value of the item at the from tf
the list. Precondition The list must
contain at least one value.
15List API continued
void push_back(const T value) Add a value
at the back of the list. Postcondition The
list has a new item at the back its size is 1
more void pop_back() Remove the item at the
back of the list. Precondition The list is
not empty. Postcondition The list has a new
element at the back or is empty. void
push_front(const T value) Add a value at
the front of the list. Postcondition The
list has a new item at the front its size is 1
more void pop_front() Remove the item at the
front of the list. Precondition The list is
not empty. Postcondition The list has a new
element at the front or is empty. int size ()
const Return the number of elements in the
list.
16Iterators
- Declarations
- listltTgtiterator iteratorName
- Operations
-
- --
-
-
- !
17List operations and Iterators
- iterator begin()
- Returns an iteratory that references the first
postion(front) of the list. If empty, the
iterator value end() is returned - iterator end()
- Returns an iterator that signified a location
immediately out of the range of the actual
elements. A program must not dereference the
value of end() with the operator - void erase(iterator pos)
- Erase the element pointed to by pos.
- Precondition The list is not empty.
- Postcondition The list has one fewer element.
- void erase(iterator first, iterator last)
- Erase all the list elements within the iteraor
frange first, last) - Precondition The list is not empty.
- Postcondition The size of the list decreases
by the number of elements in the range. - iterator insert(iterator pos, const T value)
- Insert value before po, and return tan
iterator pointing to the position of value. - Postcondition The list has a new element.