STL C Standard Library Main Ideas u General
STL: C++ Standard Library .
Main Ideas u. General purpose: generic data structures & algorithms, templates u. Flexibility: Allows for many combinations of algorithm-container u. Simple & uniform interface: interface through templates (not inheritence) u. Efficiency
Components u. Containers: data structures vector, list, map, set, deque u. Adaptors: high-level data structure stack, queue, priority_queue u. Iterators: allow access into containers u. Algorithms: base algorithms sort, copy, search, min, max, … u. Streams: input/output u. String
Example u. Using STL objects to implement a simplified version of ex 2 Data structures: · string – represent words · map<string, vector<string> > Associate a word with the array of words that appear after it
Quick Facts Vector<T> · Implements an array of objects of type T · Allows random-access to array · Can grow on as needed basis Map<Key. T, Value. T> · “Associative Array”: maps keys to values · Implemented as balanced binary tree
Markov-1. cpp
Extension u. Same but using k-order Markov chain u. Represents the probability of a word after a prefix of k words Data structures: ulist<string> - represent a list of objects See Markov-k. cpp
Containers u. Holds copies of elements u. Assumes Copy Ctor & operator = u. The standard defines the interface, not the implementation u. Two main classes · Sequential containers: list, vector, . . · Associative containers: map, multimap, set. . .
Sequential Containers u. Maintain a linear sequence of objects Types of operations u. Access/inesertion/deletion of elements at the front/end of sequence u. Random access to elements
Sequential Containers list - a linked list of objects · Efficient insertion/deletion in front/end/middle vector - an extendable sequence of objects · Efficient insertion/deletion at end · Random access deque – double-ended queue · Efficient insertion/deletion at front/end · Random access
Sequential Container Interface usize(), empty() upush_front(x), push_back(x) ufront(), back() upop_front(), pop_back() vector & deque uoperator[](int index)
Associative Containers u. Maintain a collection of keys u. Requires order on keys (less-than operator) u. Keys can be accessed based on their order (Typical) Implementation: ured-black binary trees u. O(log n) time for access/insert/delete
Associative Containers Set u. A set of unique keys Map u. Associate a value to key (associative array) u. Unique value of each key Multiset Multimap u. Same, but allow multiple values
Associative Containers & Order u. Associative containers use operator< as default order u. We can control order by using our own comparison function u. To understand this issue, we need to use function object
Function Objects A class that implements operator() Advantages: u. Use the template mechanism (class versus function) u. Enable accumulating information in the function object
Example template<class T> class less { public: bool operator()(T& lhs, T& rhs) { return lhs < rhs; } }; less<int> cmp; // declare if( cmp(1, 2) ) … if( less<int>()(1, 2) ) … an object Creates temporary objects, and then call operator()
Function Object Bases u. Base classes for the functions objects are defined in STL. u. These provide standard names for the arguments and return types. template<class T> class less : public binary_function<T, T, bool> { … }
Function Object Bases template< class Arg, class Res> struct unary_function { typedef Arg argument_type; typedef Res return_type; }; template< class Arg, class Arg 2, class Res> struct binary_function { typedef Arg first_argument_type; typedef Arg 2 second_argument_type; typedef Res return_type; };
Using Comparators template<class T, class Cmp = less<T> > class set { … } … set<int> s 1; Creates a new set<int, less<int> > s 2; // same type My. Comp object. set<int, My. Comp > s 3; Use given My. Comp object. My. Comp cmp; set<int, My. Comp > s 4(cmp);
STL Iterators u. Iterators are allow to traverse sequences u. Methods · operator* · operator++, and operator— u. Different types of iterators - to support read, write and random access u. Containers define their own iterator types u. Changing the container can invalidate the iterator
Iterator Types u. Output: write only and can write only once u. Input: read many times each item u. Forward supports both read and write u. Bi-directional support also decrement u. Random supports random access (just like C pointer)
Iterators & Containers Bidirectional iterators: u list, map, set Random access iterators: uvector, deque Input/output/forward iterators: uiostreams
Iterators and Containers T: : iterator – iterator type for type T begin() – front of the container end() – element after last Container C … Container: : iterator i for( i = C. begin(); i != C. end(); i++) // do something with *i
Iterators & Streams Can access iostreams through iterators: istream_iterator<string> in(cin); istream_iterator<string> end. Of. Input; ostream_iterator<string> out(cout); while( in != end. Of. Input ) { string s = *in++; *out++ = s; *out++ = “ “; } see use. Stream. Iterators. cpp
Sequence Adapters u. Adapters of basic containers u. Very easy, but limited interface stack<T, Seq> uprovides push, pop, top, size and empty queue<T, Seq> ualso provides back priority_queue<T, Seq, Cmp> uprovides same interface as stack uuses a compare function object
Algorithms Overview u. Sequence operations · Non modifying: for_each, find, count, search, mismatch, equal · Modifying: transform, copy, swap, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle u. Sorted sequences operations · sort, lower_bound, upper_bound, equal_range, binary_search, merge, includes, set_union, intersection, difference, symmetric_difference
Algorithms u. Most STL algorithms works on sequences u. Sequences are passed as two iterators: · beginning element · element one after last p q sequence [p, q) u. Algorithms depend on iterator type not on container type
Copy template< class In, class Out> Out copy(In first, In last, Out res) { while (first != last) *res++ = *first++; return res; } See use. Copy. cpp
Non-modifying Sequence Algorithms u In find(In first, In last, const T& val) find the first occurence of val in the sequence u In find_if(In first, In last, Pred p) find the first element satisfying p u I 1 find_first_of(I 1 f 1, I 2 l 1, I 2 f 2, I 2 l 2) find the first match between two sequences. u I 1 search( I 1 f 1, I 1 l 1, I 2 f 1, I 2 l 2 ) search for the sequence f 2. . . l 2 inside f 1. . l 1
Sorted Sequence Algorithms u sort(In first, In last[, class cmp]) find the first occurence of val in the sequence In lower_bound(In first, In last, T const & val[, class cmp]) find the first element not less than val u bool binary_search(In first, In last, T const & val[, class cmp]) check if val appears in the sequence u Out merge( I 1 f 1, I 1 l 1, I 2 f 1, I 2 l 2, Out out ) u merge two sorted sequences and write the merged sequence onto the output iterator out
- Slides: 30