Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a...
Transcript of Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a...
Chapter 16Standard Template Library Containers and Iterators
Range-based for loops
The range-based for loop is a for loop that iterates through each element in a vector or container.
A range-based for loop is also known as a for each loop. The range-based loop declares a new variable that will be assigned with each successive element of a container, such as a vector, from the first element to the last element.
example: rangeLoop.cpp
Range-based for loops
Range Based for loops provide several advantages to normal for loops:
1. Improved Readability - decreases the amount of code you have to write to setup a for loop.
2. Reduced Errors - range-based loops prevent you from incorrectly accessing elements outside the containers range.
3. Clearer Intentions - range-based loops make it more clear what the programmer is intending.
example: rangeLoop.cpp
Modifying Elements
When modifying elements inside a range-based for loop, remember to include &.
example: rangeModification.cpp
for (int &value : myVector) { value = 10; }
List Container
The list container is an STL container that contains a sequence of elements.
#include <list>
The list container is implemented as a templated class
The list implementation is that of a doubly linked list.
Common List functions:
See ZyBook Chapter 16.2
example: list.cpp
List IteratorAn iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally.
Inserting and removing elements using iterators:
myList.begin() // returns iterator at beginning of list
myList.end() // returns iterator at end of list
Pair
The pair class is an STL container that consists of two elements
#include <utility>
The pair container is implemented as a templated class
Many STL container classes make use of the pair container. Ex: key-value pairs in maps.
example: pair.cpp
pair<string, int> myPair;
Map
The map class is an STL container that utilizes the pair container to store elements as key-value pairs
#include <map>
The map container is implemented as a templated class
where K is the key data-type and V is the value data type.
example: map.cpp
map<K, V> newMap;
Map
example: map.cpp
Set
The set class is an STL container that defines a collection of unique elements.
#include <set>
The set container is implemented as a templated class
Sets are often used when a programmer does not want duplicate elements in a collection as it has built in duplication error handling.
example: set.cpp
set<T> newSet;
Set
example: setOperations.cpp
Queue
The queue class is an STL container that contains elements and support inserting from the tail and retrieval from the head.
#include <queue>
The queue container is implemented as a templated class
The Queue class is a FIFO data structure.
example: queue.cpp
queue<T> newQueue;
Queue
example: queue.cpp
Deque
The deque (“deck”) class is an STL container that contains elements and support inserting and removal at both the tail and the head.
#include <deque>
The queue container is implemented as a templated class
We can use specific deque functions to make it act as a stack. A Deque can either be a FIFO or LIFO data structure depending on how you use it
example: deque.cpp
deque<T> newDeque;
find()
The find() function seeks a specific value in a range of elements. It can be used with a number of STL containers such as lists and vectors.
#include <algorithm>
The container elements can be any C++ data type that supports the equality operator (==).
find() returns an iterator to the first element in the range that is equal to value. If value is not found, find() returns iteratorLast.
example: find.cppfindObject.cpp
find(iteratorFirst, iteratorSecond, value)
find_if()
The find_if() function is a variation of the find() function, defined in the STL algorithms library, that searches a range for an element that satisfies a boolean condition via a boolean function
#include <algorithm>
Each element inside the container is passed to the function individually.
find_if() returns an iterator to the first element in the range that is equal to value. If value is not found, find_if() returns iteratorLast.
example: findif.cpp
find_if(iteratorFirst, iteratorSecond, func)
sort()
The sort() function arranges a container's elements in ascending order. Can be performed on numerous STL containers like vectors and lists.
#include <algorithm>
The sorting algorithm function takes in 2 iterators as parameters, usually the first and last iterator in a collection container.
example: sort.cpp
sort(iteratorFirst, iteratorSecond)
random_shuffle()
The random_shuffle() function randomizes the order of a container by using iterators. Can be performed on numerous STL containers like vectors and lists.
#include <algorithm>
The random_shuffle algorithm function takes in 2 iterators as parameters, usually the first and last iterator in a collection container.
example: random_shuffle.cpp
random_shuffle(iteratorFirst, iteratorSecond)