C++ Template Library Comp Sci 1575 Data Structures
Outline 1 2 3 4
Data structures Color key:
Array sorting algorithms
Outline 1 2 3 4
is part of standard namespace Old diagram of the standard
Outline 1 2 3 4
Main pillars of the manage storage space for elements and provide member functions to access them. Implemented as class templates, with flexibility of types as elements. act on containers, and perform operations like initialization, sorting, searching, and transforming of the contents of containers. step through elements of collections of objects in containers or subsets of containers. Pointer-like iterators can traverse many container classes.
Example std :: vector Check out the code: container with iterators and algorithms A dynamic C-like array (i.e., capable of random access) with the ability to resize itself automatically when inserting or erasing an object. Random access - constant O(1). Insertion or removal of elements at the back, average time O(1). Inserting an element to the back of the vector at the end takes amortized constant time. Removing the last element takes only constant time, because no re-sizing happens. Insertion or removal of elements - linear in distance to the end of the vector O(n). Inserting and erasing at the beginning or in the middle is linear in time. See: Intro vector.cpp
Outline 1 2 3 4
The is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like queues, lists and stacks. There are three classes of containers sequence containers, associative containers, and unordered associative containers each of which is designed to support a different set of operations. The container manages the storage space that is allocated for its elements and provides member functions to access them, either directly or through iterators (objects with properties similar to pointers). Most containers have at least several member functions in common, and share functionalities. Which container is the best for the particular application depends not only on the offered functionality, but also on its efficiency for different workloads.
For a comprehensive list, see: http://en.cppreference.com/w/cpp/container http://www.cplusplus.com/reference/stl/ https://en.wikipedia.org/wiki/_ Template_Library
How to choose your container? Older version
How to choose your container? Newer version
Outline 1 2 3 4
Where does slist go? http://en.cppreference.com/w/cpp/iterator http://www.cplusplus.com/reference/iterator/
Outline 1 2 3 4
: example of heap functions Defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Range is defined as [first, last) where last refers to the element past the last element to inspect or modify. http://en.cppreference.com/w/cpp/algorithm/ http://www.cplusplus.com/reference/algorithm/ For example, heap operations can be performed on a vector: is heap checks if the given range is a max heap is heap until finds the largest subrange that is a max heap make heap creates a max heap out of a range of elements push heap adds last-1 element to a max heap pop heap removes the largest element from a max heap by moving to end sort heap turns a max heap into a range of elements sorted in ascending order
: example of heap functions See: Heap algorithms.cpp How can we do this more directly?
Outline 1 2 3 4
std::priority queue #include < queue > A priority queue is a container adaptor that provides constant time lookup of the largest (by default) element, at the expense of logarithmic insertion and extraction. A user-provided Compare can be supplied to change the ordering, e.g. using std::greater< T > would cause the smallest element to appear as the top(). Working with a priority queue is similar to managing a heap in some random access container, with the benefit of not being able to accidentally invalidate the heap.
std::priority queue template parameters template< c l a s s T, c l a s s C o n t a i n e r = s t d : : v e c t o r <T>, c l a s s Compare = s t d : : l e s s <typename C o n t a i n e r : : v a l u e t y p e > > T - The type of the stored elements. The behavior is undefined if T is not the same type as Container::value type. Container - Type of underlying container to store the elements. Container must satisfy requirements of SequenceContainer, and its iterators must satisfy the requirements of RandomAccessIterator. It must provide the following functions with the usual semantics: front(); push back(); pop back(); containers std::vector and std::deque satisfy these requirements. Compare - type providing a strict weak ordering.
Demo code: priority queue See: Priority queue.cpp