180 likes | 425 Views
Generic Programming. Using the C++ Standard Template Library. Traditional programming approach. Data Structures + Algorithms = Programs Quote from the renowned computer scientist Niklaus Wirth. Generic approach.
E N D
Generic Programming Using the C++ Standard Template Library
Traditional programming approach Data Structures + Algorithms= Programs Quote from the renowned computer scientist Niklaus Wirth
Generic approach • Design algorithms to deal with abstractions of data structures rather than specific data structures • Fit the abstractions onto each of the real data structures used in programs and the same algorithm should work with all the data structures
Abstract Data Structures • What operations are common to many data structures? • What operations are useful to support many different algorithms? • Think about data structures that don’t rely on specifics.
Abstraction of a Sequence • Fundamental operations on a sequence • Generate a sequence • Access each item in the sequence in turn. • Other useful operations • Search the sequence for a particular item • Sort the items in the sequence • Etc.
The Standard Template Library • The STL provides programmers with a library of common data structures and a set of fundamental algorithms that operate on them • STL is based around • Containers classes providing abstract data structures • Iterators traverse the containers • Algorithms process the information in the containers • Additional components for flexibility and portability: • Function objects encapsulate a function as an object • Adaptors provide an existing component with a different interface • Allocators encapsulate the memory model of the machine
STL • The C++ Standard Template Library (STL) is now a part of the Standard Libraries. It is a newer part, so it is often still referred to it as if it is a different thing. • References • Free pdf of “Designing Components with the C++ STL” by Ulrich Breymann from – http://www.informatik.hs-bremen.de/~brey/stlbe.html • Complete STL documentation - http://www.sgi.com/tech/stl/ • D R Musser - http://www.cs.rpi.edu/~musser/gp/index.htm
Containers • Data structures that manage a collection of elements and are responsible for the allocation and deallocation of those elements. • Typically have constructors and destructors along with operations for inserting and deleting elements. • Two types: • Sequence Containers, e.g., vectors, lists, priority queues • Associative Containers, e.g., maps, sets
Iterators • Iterators are like generic pointers. They save algorithm writers the worry of having to deal with the internals of containers. They come in various types: • Random access, Bidirectional, Forward, Reverse, Input and Output. • Some containers support only certain iterator types (you can't for example randomly access a list). • Often use iterators with begin and end operations on containers e.g. vector<int>::iterator vi = v.begin() iterator vi placed at beginning of int vector v
Algorithms • Operate on containers. • They fall into 6 groups • Search algorithms - search(), count_if(), etc. • Sorting algorithms - sort(), merge(), etc. • Deletion algorithms - remove(), unique(), etc. • Numeric algorithms - partial_sum(), inner_product(), etc. • Generation algorithms - generate(), for_each(), etc. • Relational algorithms - equal(), min(), etc. • Algorithms usually take as their first 2 arguments iterators to mark the range of elements to be operated on. For example, to replace 'A' by 'B' in a string str one could do :- replace(str.begin(), str.end(), 'A', 'B');
Common operations on containers • push_back – appends an item at the end of a containing • pop_back – deletes the last item from the end of a container • front and back access the first and last items in a container • size is the number of items in the container
Vector • Behaves like an array, but can grow itself as necessary. • Can be accessed using [ ] like an array • Can be accessed using iterators as for all containers • Good for sequential or random access, poor for insertion and deletion anywhere other than the end. • capacity is the total number of items that the container can hold without requiring reallocation. • See example stlvect1.cpp
Lists • Insert and remove from list using push_back and pop_back as for vectors • Also push_front and pop_front which operate on the front of the list container. • remove(x) – removes all the x items from the list • Good for insertion and deletion of items in a list, but can only access sequentially.
Maps & Sets • Maps are associative containers • Map entries are pair of values – key, value • map – keys must be unique • multimap – duplicate keys allowed • set – collection of unique keys • multiset – as set but duplicates allowed
Iterators • Iterators are the glue that connect algorithms to containers • Iterators provide access to the containers. • Similar to a pointer accessing an array • Used to traverse across the containers • Iterators can be • incremented • dereferenced using ‘*’ to access the item in the container
Iterator Types • Every standard container defines two associated iterator types: • container-type::const iterator • container-type::iterator Where container-type is the container type, such as vector<int> • We use the iterator type if we want to change the values stored in the container • We use the const iterator type if we just need read access
Algorithms • Non-Mutating Sequence Operations • Algorithms like count and search which do not modify the iterator or its associated container. • Mutating Sequence Operations • Algorithms like copy, reverse, or swap which may modify a container or its iterator. • Searching and Sorting • Sorting, searching, and merging algorithms, such as stable sort, binary search, and merge. • Set Operations • Mathematical set operations, such as set union and set intersection. These algorithms only work on sorted containers