Topic J C++ Standard Template Library: SEG4110 - Advanced Software Design and Reengineering
Topic J C++ Standard Template Library: SEG4110 - Advanced Software Design and Reengineering
STL extensively uses generic programming based on templates Divided into three components:
- Containers: data structures that store objects of any type - Iterators: used to manipulate container elements - Algorithms: searching, sorting and many others
Containers
Three types of containers
- Sequence containers: - linear data structures such as vectors and linked lists - Associative containers: - non-linear containers such as hash tables - Container adapters: - constrained sequence containers such as stacks and queues
Iterators
Iterators are pointers to elements of first-class containers
Type const_iterator defines an iterator to a container element that cannot be modified Type iterator defines an iterator to a container element that can be modified
All first-class containers provide the members functions begin() and end()
return iterators pointing to the first and last element of the container
Iterators (cont.)
If the iterator it points to a particular element, then
- it++ (or ++it) points to the next element and - *it refers to the value of the element pointed to by it
The iterator resulting from end() can only be used to detect whether the iterator has reached the end of the container We will see how to use begin() and end() in the next slides
Sequence Containers
STL provides three sequence containers
- vector: based on arrays - deque (double-ended queue): based on arrays - list: based on linked lists
To use vectors, we need to include the header <vector> Some functions of the class vector include
- size, capacity, insert
using std;
int main() { vector<int> v;
for (it = v.begin(); it != v.end(); it++) { cout << *it << \n; } return 0;
10
11
deque (cont.)
Additional storage for a deque is allocated using blocks of memory
- that are maintained as an array of pointers to those blocks
12
Associative Containers
Associative containers use keys to store and retrieve elements There are four types: multiset, set, multimap and map
all associative containers maintain keys in sorted order all associative containers support bidirectional iterators set does not allow duplicate keys multiset and multimap allow duplicate keys multimap and map allow keys and values to be mapped
13
The ordering of the keys is determined by the STL comparator function object less<T> Keys sorted with less<T> must support comparison using the < operator
SEG4110 - Topic J - C++ Standard Template Library 14
16
The ordering of the keys is determined by the STL comparator function object less<T>
17
typedef multimap< int, double, std::less< int > > mp_type; // creates a mutlimap type
int main() { mp_type mp;
// use iterator to go through mp for (mp_type::iterator it = mp.begin(); it != mp.end(); it ++) cout << it->first << '\t' << it->second << '\n';
return 0; }
SEG4110 - Topic J - C++ Standard Template Library 18
19
20
Container Adapters
STL supports three container adapters:
- stack, queue and priority_queue
The functions push and pop are common to all container adapters
SEG4110 - Topic J - C++ Standard Template Library 21
22
Example:
- A queue of int using a list: queue <int, list<int>> q1; - A queue of int using a deque: queue <int> q2;
23
Algorithms
STL separates containers and algorithms
- The main benefit is to avoid virtual function calls - This cannot be done in Java or C# because they do not have such flexible mechanisms for dealing with function objects - Smalltalk does all the following can be easily implemented in Smalltalk
25
fill_n(iterator1, n, value);
changes specified number of elements starting at iterator1 to value
generate_n(iterator1, n, function)
same as fill_n except that it calls a function to return value
SEG4110 - Topic J - C++ Standard Template Library 26
27
28
replace_if( iterator1, iterator2, function, newvalue ); replaces all elements in the range iterator1-iterator2 for which function returns true with newvalue
29
30
Search algorithms
iterator find(iterator1, iterator2, value) returns an iterator that points to first occurrence of value iterator find_if(iterator1, iterator2, function) returns an iterator that points to the first element for which function returns true.
31
Sorting algorithms
sort(iterator1, iterator2)
sorts elements in ascending order
32
33
reverse(iterator1, iterator2)
- reverses elements in the range of iterator1 to iterator2
34
35
36
37
39
References
"C++, How to program", Harvey M. Deitel, Paul J. Deitel , 4th edition, Prentice Hall "The C++ Programming Language", Bjarne Stroustrup, 3rd Edition, Addison-Wesley
40