120 likes | 138 Views
Learn about C++ STL's containers, algorithms, and iterators designed for performance and flexibility. Explore reasons, generic algorithms, and efficiency of STL components.
E N D
CSCI 383 Object-Oriented Programming & Design Lecture 24 Martin van Bommel
C++ Standard Template Library (STL) • Part of the C++ standard library (1994 ANSI/ISO) • Developed by Alexander Stepanov and Meng Lee at Hewlett-Packard • Collection of software components, most of which consist of template classes and template functions • Conceived and designed for performance and flexibility • Three basic components • Containers – collections of objects • Algorithms – functions for processing container’s contents • Iterators – mechanism for accessing objects in containers - high-level alternatives to for and while loops CSCI 383 Lecture 23 M. van Bommel
Reasons for Using STL • STL offers an assortment of containers to meet needs • E.g. vectors, associative arrays, sets, stacks, queues • STL containers grow and shrink in size automatically • Programming ease, robustness, and storage efficiency • Time and storage complexities are published • STL provides built-in algorithms for processing • E.g. sort, search, replace, reverse, partition, testing • STL supports powerful numerical algorithms • E.g. accumulate, inner_product, partial_sum CSCI 383 Lecture 23 M. van Bommel
Generic Algorithms • Algorithms in the STL operate over iterators rather than containers • Each container defines its own iterators • A variable declared as an iterator can be incremented • Each container has methods to create its iterators • begin() • end() • Handout #14 • Generic algorithms sort and for_each do not need the type of the container, just the iterators for the container (which can be incremented) CSCI 383 Lecture 23 M. van Bommel
Containers • STL has seven basic containers divided into two groups • Sequential containers • vector – array that grows and shrinks as needed • list – bi-directional linked list of elements • deque – double-ended queue with efficient insertions/deletions at each end • Associative containers • set – collection of non-duplicate keys • multiset – a set with duplicates permitted • map – collection of non-duplicate elements accessed by key • multimap – a map with duplicates permitted CSCI 383 Lecture 23 M. van Bommel
Sequential Container: <vector> • vector <T> name • vector <T> name( size ) • vector <T> name( size, value ) • Grows and shrinks automatically to meet storage needs • Stored in contiguous memory – may be copied if it grows • If size specified, creates vector of that size • If value specified, elements initialized that value • If not, initialized to default value (e.g zero) • vector <T> name( vec ) • Copy constructor CSCI 383 Lecture 23 M. van Bommel
STL <vector> methods • “at” and operator[] both permit access to a position • v.at(i) == v[i] • “front()” and “back()” return refs to first/last elements • “begin()” and “end()” return vector<T>::iterator • for (vector<T>::iterator it = v.begin(); it != v.end(); it++) • “empty()” and “size()” give bool and int respectively • “push_back(ele)” and “pop_back()” add/remove • “clear()” empties the vector • “erase(it1, it2)” removes elements between iterators • “insert(it, value)” inserts element at position of iterator • “find(value)” returns an iterator of first occurrence CSCI 383 Lecture 23 M. van Bommel
Sequential Containers: <deque>, <list> • Use of <deque> and <list> are almost the same as <vector> in terms of constructors and methods • They differ in implementation • <vector> implemented as contiguous array • Reallocated and copied when necessary • Elements moved if insert at front or middle • <deque> implemented as non-contiguous arrays • More overhead to iterate (must move between parts) • Insertions in middle more efficient than <vector> (not <list>) • <list> implemented as doubly-linked list • Even more overhead to iterate • Insertions slower in general but easier in middle CSCI 383 Lecture 23 M. van Bommel
Sequential Container Efficiency • <vector> has variable time insert/erase at beginning • Must move elements • <deque> and <list> have constant time at beginning • Can add components to front • <list> has constant time insert/erase in middle • Manipulate doubly-linked list • <vector> and <deque> has variable in middle • Move elements or add new component • <vector> and <deque> have constant time access for any element by position (<vector> faster) • <list> is slower to get to middle elements CSCI 383 Lecture 23 M. van Bommel
STL Associative Containers • set – collection of zero or more non-duplicate, unordered elements, called keys • multiset – set that permits duplicates • map – collection of zero or more key / value pairs where key values are unique • multimap – map that permits duplicates • Note: even though it does not make much sense, the associative containers can also be traversed with iterators, thus have “begin()” and “end()” methods, and can “insert” based on iterators CSCI 383 Lecture 23 M. van Bommel
STL: <set> • set <T> name • Empty set (typically implemented as binary search tree) • set <T> name( first, last ) • Iterators to positions in a sequence to initialize set • set <T> name( my_set ) • Copy constructor CSCI 383 Lecture 23 M. van Bommel
STL: <map> • map<key_type, value_type> name • Empty map (typically stored in key value order) • map<T> name( first, last ) • Iterators to positions in another map to initialize map • map<T> name( my_map ) • Copy constructor • Key and value are accessed via iterators with data fields “first” and “second” • map<key_type, value_type>::iterator it;it – >first & it – >second • Can assign using name[key] = value CSCI 383 Lecture 23 M. van Bommel