chapter 16egabrielsen/cs1342/chapter16.pdflist iterator an iterator is an object that points to a...

19
Chapter 16 Standard Template Library Containers and Iterators

Upload: others

Post on 19-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

Chapter 16Standard Template Library Containers and Iterators

Page 2: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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

Page 3: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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

Page 4: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

Modifying Elements

When modifying elements inside a range-based for loop, remember to include &.

example: rangeModification.cpp

for (int &value : myVector) { value = 10; }

Page 5: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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

Page 6: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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

Page 7: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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;

Page 8: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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; 

Page 10: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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; 

Page 12: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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; 

Page 14: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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; 

Page 16: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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)

Page 17: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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)

Page 18: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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)

Page 19: Chapter 16egabrielsen/cs1342/Chapter16.pdfList Iterator An iterator is an object that points to a location in a list and can be used to traverse the list bidirectionally. Inserting

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)