Download presentation
Presentation is loading. Please wait.
Published byVeronika Budiono Modified over 6 years ago
1
Prof. Michael Neary mneary1@umbc.edu
Lecture 7: The STL Prof. Michael Neary
2
The Standard Template Library
A collection of commonly used data structures Made up of 3 things Containers Algorithms Iterators Definitely used in project 2
3
Containers A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows a great flexibility in the types supported as elements. The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference objects with similar properties to pointers). From:
4
Sequence Containers All sequential containers is that the elements can be accessed sequentially They reside in namespace std Some only in C++ 11 (but not C++ 98) Name Details array Array class vector Vector deque Double ended queue list List forward_list Singly linked list (11) From:
5
Container Adaptors Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes (such as deque or list) to handle the elements. Name Details stack LIFO stack queue FIFO queue priority_queue Priority queue Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains From:
6
Associative Containers
An abstract data type composed of a collection of (key, value) pairs For sets and maps, each possible key appears at most once in the collection Name Details set Set multiset Multiple-key Set map Map Multimap Multiple-key Map From:
7
Vectors Dynamically sized Elements stored in order
Random access allowed Use [] or .at(i) at(i) will throw an exception if index out of bounds
8
Vector example #include <vector> #include <iostream> using namespace std; int main() { vector<int> numbers(3); numbers.push_back(5); numbers.push_back(10); numbers.push_back(15); cout << numbers.front() << endl; // 15 cout << numbers.back() << endl; // 5 cout << numbers[1] << endl; // 10 cout << numbers.at(1) << endl; // 10 }
9
Stacks Sequential data structure (LIFO) size() empty() push(e) pop()
top()
10
Stack example #include <stack> #include <iostream> using namespace std; int main() { stack<int> numbers; numbers.push(10); numbers.push(7); numbers.push(4); cout << numbers.size() << endl; // 3 cout << numbers.empty() << endl; // false cout << numbers.top() << endl; // 4 numbers.pop(); cout << numbers.top() << endl; // 7 }
11
Queue Sequential data structure (FIFO) size() empty() push(e) pop()
front() back()
12
Queue example #include <queue> #include <iostream> using namespace std; int main() { queue<int> numbers; numbers.push(10); numbers.push(7); numbers.push(4); cout << numbers.size() << endl; // 3 cout << numbers.empty() << endl; // false cout << numbers.front() << endl; // 10 numbers.pop(); cout << numbers.front() << endl; // 7 }
13
Lists Doubly Linked List size() empty() front() back() push_front()
push_back() pop_front() pop_back()
14
List example #include <iostream> #include <list> using namespace std; int main() { list<int> numbers; numbers.push_front(6); // 6 numbers.push_front(3); // 3 6 numbers.push_back(1); // cout << numbers.size() << endl; // 3 cout << numbers.empty() << endl; // false cout << numbers.front() << endl; // 3 cout << numbers.back() << endl; // 1 numbers.pop_back(); cout << numbers.back() << endl; // 6 }
15
Iterators
16
Iterators Problem Solution Also: encapsulation
Not all STL classes provide random access How do we do “for each element in X”? Solution Iterators “Special” pointers “Iterate” through each item in the collection Also: encapsulation User shouldn’t need to know how it works
17
About Iterators Allows the user to access elements in a data structure using a familiar interface, regardless of the internal details of the data structure An iterator should be able to: Move to the beginning (first element) Advance to the next element Return the value referred to Check to see if it is at the end
18
Kinds of Iterators Forward iterators: Bidirectional iterators:
Using ++ works on iterator Bidirectional iterators: Both ++ and -- work on iterator Random-access iterators: Using ++, --, and random access all work with iterator These are "kinds" of iterators, not types!
19
Iterators Essential operations begin()
Returns an iterator to first item in collection end() Returns an iterator ONE BEYOND the last item in collection Why does it do this? If the collection is empty, begin() == end()
20
Constant and Mutable Iterators
Behavior of the dereferencing operator dictates if an iterator is constant or mutable Constant iterator: Cannot edit contents of container using iterator Mutable iterator: Can change corresponding element in container
21
Mutable Iterators Mutable iterator: i.e.: *p returns an lvalue
*p can be assigned value Changes corresponding element in container i.e.: *p returns an lvalue *p can be on the left hand side of the assignment operator (and the right hand side)
22
Vector Example Here’s a very basic example of using an iterator to move through a vector: vector<int> v; // fill up v with data... for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) { cout << *it << endl; } This basic example should work regardless of the container type!
23
Constant Iterators Constant iterator:
* produces read-only version of element Can use *p to assign to variable or output, but cannot change element in container e.g., *p = <anything>; is illegal *p can only be on the right hand side of the assignment operator
24
Vector Example Here’s a a constant iterator moving through a vector
vector<int> v; // fill up v with data... for (vector<int>::const_iterator it = v.begin()); it != v.end(); ++it) { cout << *it << endl; }
25
Iterators - Overloaded Operators
* Dereferences the iterator ++ Moves forward to next element -- Moves backward to previous element == True if two iterators point to same element != True if two iterators point to different elements = Assignment, makes two iterators point to same element
26
Algorithms
27
Algorithms The STL provides a number of algorithms that can be used on any container #include <algorithmName>
28
One example sort(p, q) Several more listed in Chapter 6 of your book
p,q are iterators of the container type Sort the elements in ascending order from p to q Several more listed in Chapter 6 of your book
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.