TDDD38/726G82 - Advanced programming in C++ Introducon STL - - PowerPoint PPT Presentation

tddd38 726g82 advanced programming in c
SMART_READER_LITE
LIVE PREVIEW

TDDD38/726G82 - Advanced programming in C++ Introducon STL - - PowerPoint PPT Presentation

TDDD38/726G82 - Advanced programming in C++ Introducon STL Christoffer Holm Department of Computer and informaon science 1 Introducon 2 IO 3 Sequenal Containers 1 Introducon 2 IO 3 Sequenal Containers 3 / 47


slide-1
SLIDE 1

TDDD38/726G82 - Advanced programming in C++

Introducon STL

Christoffer Holm

Department of Computer and informaon science

slide-2
SLIDE 2

1 Introducon 2 IO 3 Sequenal Containers

slide-3
SLIDE 3

1 Introducon 2 IO 3 Sequenal Containers

slide-4
SLIDE 4

3 / 47

Introducon

What is the STL?

‚ Library accessible everywhere ‚ Solving common problems ‚ Modular design ‚ Efficiency

slide-5
SLIDE 5

3 / 47

Introducon

What is the STL?

‚ Library accessible everywhere ‚ Same behaviour independent of plaorm ‚ Shipped with the compiler itself ‚ ISO C++ requires the full library to be accessible ‚ Solving common problems ‚ Modular design ‚ Efficiency

slide-6
SLIDE 6

3 / 47

Introducon

What is the STL?

‚ Library accessible everywhere ‚ Solving common problems ‚ Having to reinvent the wheel is costly ‚ There are problems most programmers face ‚ Designed to be as widely usable as possible ‚ Modular design ‚ Efficiency

slide-7
SLIDE 7

3 / 47

Introducon

What is the STL?

‚ Library accessible everywhere ‚ Solving common problems ‚ Modular design ‚ Don’t pay for what you don’t use ‚ Only import the parts that you need ‚ All modules are compable with each other ‚ Efficiency

slide-8
SLIDE 8

3 / 47

Introducon

What is the STL?

‚ Library accessible everywhere ‚ Solving common problems ‚ Modular design ‚ Efficiency ‚ Library writers are very skilled ‚ Components are highly opmized ‚ Maintenance is not your responsibility

slide-9
SLIDE 9

4 / 47

Introducon

Standard Template Library

slide-10
SLIDE 10

5 / 47

Introducon

Design principles of STL

‚ Should be as general as possible Solves common problems The common case should be convenient Must work together with user-defined code Efficient enough to replace hand-wrien alternaves Should have robust error handling

slide-11
SLIDE 11

5 / 47

Introducon

Design principles of STL

‚ Should be as general as possible ‚ Solves common problems The common case should be convenient Must work together with user-defined code Efficient enough to replace hand-wrien alternaves Should have robust error handling

slide-12
SLIDE 12

5 / 47

Introducon

Design principles of STL

‚ Should be as general as possible ‚ Solves common problems ‚ The common case should be convenient Must work together with user-defined code Efficient enough to replace hand-wrien alternaves Should have robust error handling

slide-13
SLIDE 13

5 / 47

Introducon

Design principles of STL

‚ Should be as general as possible ‚ Solves common problems ‚ The common case should be convenient ‚ Must work together with user-defined code Efficient enough to replace hand-wrien alternaves Should have robust error handling

slide-14
SLIDE 14

5 / 47

Introducon

Design principles of STL

‚ Should be as general as possible ‚ Solves common problems ‚ The common case should be convenient ‚ Must work together with user-defined code ‚ Efficient enough to replace hand-wrien alternaves Should have robust error handling

slide-15
SLIDE 15

5 / 47

Introducon

Design principles of STL

‚ Should be as general as possible ‚ Solves common problems ‚ The common case should be convenient ‚ Must work together with user-defined code ‚ Efficient enough to replace hand-wrien alternaves ‚ Should have robust error handling

slide-16
SLIDE 16

6 / 47

Introducon

Components

‚ Algorithms ‚ Containers ‚ Funcons ‚ Iterators

slide-17
SLIDE 17

6 / 47

Introducon

Components

‚ Algorithms ‚ General facilies for solving common problems ‚ A large amount of algorithms exist in the STL ‚ Highly opmized for both speed and memory ‚ Containers ‚ Funcons ‚ Iterators

slide-18
SLIDE 18

6 / 47

Introducon

Components

‚ Algorithms ‚ Containers ‚ General data structures ‚ Based on high level abstracons ‚ Should not be required to understand the underlying implementaon ‚ Funcons ‚ Iterators

slide-19
SLIDE 19

6 / 47

Introducon

Components

‚ Algorithms ‚ Containers ‚ Funcons ‚ General ulity funcons ‚ Should be usable for as many types as possible ‚ Solves all manner of problems ‚ Iterators

slide-20
SLIDE 20

6 / 47

Introducon

Components

‚ Algorithms ‚ Containers ‚ Funcons ‚ Iterators ‚ Abstracon which allows for general traversal of data ‚ Used in conjuncon with algorithms ‚ An interface that works with all containers without the need to specify the container type

slide-21
SLIDE 21

1 Introducon 2 IO 3 Sequenal Containers

slide-22
SLIDE 22

8 / 47

IO

Streams

ios

  • stream
  • stringstream
  • fstream

istream ifstream istringstream iostream stringstream fstream

slide-23
SLIDE 23

8 / 47

IO

Streams

ios

  • stream
  • stringstream
  • fstream

istream ifstream istringstream iostream stringstream fstream std::cout std::cin

slide-24
SLIDE 24

8 / 47

IO

Streams

ios

  • stream
  • stringstream
  • fstream

istream ifstream istringstream iostream stringstream fstream Output Input

slide-25
SLIDE 25

9 / 47

IO

Streams

‚ Represent reading and wring data to some device ‚ Example of devices; ‚ a terminal ‚ a file ‚ a chunk of memory ‚ sockets ‚ operator>> to read ‚ operator<< to write

slide-26
SLIDE 26

10 / 47

IO

Stream operators template <typename T>

  • stream& operator<<(ostream& os, T&& data)

{ // write data to the device return os; } // ... cout << 1 << 2;

slide-27
SLIDE 27

11 / 47

IO

Stream operators

  • stream& operator<<(ostream& os, T&& data);

cout << 1 << 2;

slide-28
SLIDE 28

11 / 47

IO

Stream operators

  • stream& operator<<(ostream& os, T&& data);

(cout << 1) << 2;

slide-29
SLIDE 29

11 / 47

IO

Stream operators

  • stream& operator<<(ostream& os, T&& data);
  • perator<<(cout, 1) << 2;
slide-30
SLIDE 30

11 / 47

IO

Stream operators

  • stream& operator<<(ostream& os, T&& data);

cout << 2;

slide-31
SLIDE 31

11 / 47

IO

Stream operators

  • stream& operator<<(ostream& os, T&& data);

(cout << 2);

slide-32
SLIDE 32

11 / 47

IO

Stream operators

  • stream& operator<<(ostream& os, T&& data);

cout;

slide-33
SLIDE 33

12 / 47

IO

Chaining operators

‚ Stream operators return a reference to the stream ‚ This is done to enable chaining ‚ Since << and >> are le associave this will allow us to make several calls to the stream in one expression

slide-34
SLIDE 34

13 / 47

IO

Devices

  • stream& operator<<(ostream& os, T&& data);

int main() {

  • stringstream oss{};
  • fstream ofs{"my_file.txt"};

cout << 1; // write to terminal

  • ss

<< 1; // write to string

  • fs

<< 1; // write to file

  • ss.str(); // access string

}

slide-35
SLIDE 35

14 / 47

IO

Devices istream& operator>>(istream& is, T& data); int main() { int x; istringstream iss{"1"}; ifstream ofs{"my_file.txt"}; cin >> x; // read from terminal

  • ss >> x; // read from string
  • fs >> x; // read from file

}

slide-36
SLIDE 36

15 / 47

IO

Devices

‚ The interface of streams are general ‚ Underlying devices are abstracted away ‚ all streams are within a (polymorphic) hierarchy ‚ so we can write general code that operates on arbitrary streams if we take ostream& or istream&

slide-37
SLIDE 37

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail:

unable to read as int

eof:

found end of file character

bad:

file is corrupt

slide-38
SLIDE 38

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail:

unable to read as int

eof:

found end of file character

bad:

file is corrupt

slide-39
SLIDE 39

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail:

unable to read as int

eof:

found end of file character

bad:

file is corrupt

slide-40
SLIDE 40

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail:

unable to read as int

eof:

found end of file character

bad:

file is corrupt

slide-41
SLIDE 41

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail: unable to read as int eof:

found end of file character

bad:

file is corrupt

slide-42
SLIDE 42

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail: unable to read as int eof: found end of file character bad:

file is corrupt

slide-43
SLIDE 43

16 / 47

IO

Error handling int x; ifstream ifs{"file"}; while (ifs >> x) { // do stuff }

Exits loop if:

fail: unable to read as int eof: found end of file character bad: file is corrupt

slide-44
SLIDE 44

17 / 47

IO

Error handling int x; ifstream ifs{"file"}; ifs >> x; if (ifs.fail()) // unable to read as int // ... else if (ifs.eof()) // reached end of file // ... else if (ifs.bad()) // device is corrupt // ...

slide-45
SLIDE 45

18 / 47

IO

Error flags istream& operator>>(istream& is, T& t) { // try to read from is if (/* unable to read as T */) { is.setstate(ios::failbit); } return is; }

slide-46
SLIDE 46

19 / 47

IO

Error flags ios::failbit

stream operaon failed

ios::eofbit

device has reached the end

ios::badbit

irrecoverable stream error

ios::goodbit

no error

slide-47
SLIDE 47

20 / 47

IO

Error flags

‚ Mulple flags can be set at once ‚ except goodbit; it is set when no other flag is set ‚ This means that several errors can occur at once ‚ Do note that these flags are set aer a stream

  • peraon failed

‚ The stream does not magically detect an error if no

  • peraon has been performed
slide-48
SLIDE 48

21 / 47

IO

Converng from strings

int main(int argc, char* argv[]) { int x; istringstream iss{argv[1]}; if (!(iss >> x)) { // error // reset flags iss.clear(); } // continue } int main(int argc, char* argv[]) { int x; try { x = stoi(argv[1]); } catch (invalid_argument& e) { // error } // continue }

slide-49
SLIDE 49

22 / 47

IO

Converng from strings istringstream version

` More general ` Cheaper error path ´ Requires a stream ´ Must check flags

stoi version

` No extra objects ` Easier error handling ´ Expensive error path ´ Only works for int Prefer the istringstream version because of generality, but as always; there are no universal soluons

slide-50
SLIDE 50

23 / 47

IO

What will be printed?

#include <sstream> #include <iostream> #include <string> using namespace std; int main() { stringstream ss{}; ss << "123a bc hello"; int n{}; char c{}; string str{}; if (ss >> n >> n >> c) cout << n << " "; ss.clear(); if (ss >> c >> c) cout << c << " "; ss.clear(); if (ss >> str) cout << str << " "; }

slide-51
SLIDE 51

1 Introducon 2 IO 3 Sequenal Containers

slide-52
SLIDE 52

25 / 47

Containers

Containers

‚ Sequenal Containers ‚ Associave Containers ‚ Container Adaptors

slide-53
SLIDE 53

26 / 47

Sequenal Containers

Important concepts

‚ Memory allocaons ‚ Different containers have different models of allocaon. ‚ Calling new is very slow, ‚ So the number of memory allocaons is an important factor in the effecveness of a container ‚ CPU caching ‚ Pointer invalidaon

slide-54
SLIDE 54

26 / 47

Sequenal Containers

Important concepts

‚ Memory allocaons ‚ CPU caching ‚ Modern CPU:s perform what is known as caching. ‚ Whenever the CPU fetch data from the RAM it will fetch a block of data and store that in the cache. ‚ Accessing data in the CPU cache is several magnitudes faster than accessing data in the RAM. ‚ Pointer invalidaon

slide-55
SLIDE 55

26 / 47

Sequenal Containers

Important concepts

‚ Memory allocaons ‚ CPU caching ‚ We always read data in blocks, so we know that the element aer the data we just read is almost guaranteed to be in the cache. ‚ So containers that read data in sequence is a lot faster than those that do not. ‚ Pointer invalidaon

slide-56
SLIDE 56

26 / 47

Sequenal Containers

Important concepts

‚ Memory allocaons ‚ CPU caching ‚ On the flip side: if the elements of a container is spread all around the RAM, then it will be a lot slower since we almost always have to read the data from RAM rather than cache. ‚ Usually we talk about the cache locality of a container: how much of the cache it can leverage for speedups. ‚ Pointer invalidaon

slide-57
SLIDE 57

26 / 47

Sequenal Containers

Important concepts

‚ Memory allocaons ‚ CPU caching ‚ Pointer invalidaon ‚ If we have pointers or references to data in containers we have to know whenever these gets invalidated. ‚ A pointer (or reference) points to a specific address in memory,

slide-58
SLIDE 58

26 / 47

Sequenal Containers

Important concepts

‚ Memory allocaons ‚ CPU caching ‚ Pointer invalidaon ‚ So if the container for some reason moves the element to another address in memory, then the pointer doesn’t refer to the same element (and chances are it doesn’t even point to a valid object) ‚ This can prove to be a big impact in how we use containers.

slide-59
SLIDE 59

27 / 47

Sequenal Containers

What is a sequenal container?

‚ Data stored in sequence ‚ Accessed with indices ‚ Ordered but not (necessarily) sorted

slide-60
SLIDE 60

28 / 47

Sequenal Containers

Which sequenal containers are there?

‚ std::array ‚ std::vector ‚ std::list ‚ std::forward_list ‚ std::deque

slide-61
SLIDE 61

29 / 47

Sequenal Containers

std::array

data std::array<int, 4>

std::array<int, 4> array{};

slide-62
SLIDE 62

29 / 47

Sequenal Containers

std::array

data std::array<int, 4>

array[0] = 1;

slide-63
SLIDE 63

29 / 47

Sequenal Containers

std::array

1

data std::array<int, 4>

array[0] = 1;

slide-64
SLIDE 64

29 / 47

Sequenal Containers

std::array

1

data std::array<int, 4>

array[1] = 2;

slide-65
SLIDE 65

29 / 47

Sequenal Containers

std::array

1 2

data std::array<int, 4>

array[1] = 2;

slide-66
SLIDE 66

29 / 47

Sequenal Containers

std::array

1 2

data std::array<int, 4>

array[2] = 3;

slide-67
SLIDE 67

29 / 47

Sequenal Containers

std::array

1 2 3

data std::array<int, 4>

array[2] = 3;

slide-68
SLIDE 68

29 / 47

Sequenal Containers

std::array

1 2 3

data std::array<int, 4>

array[3] = 4;

slide-69
SLIDE 69

29 / 47

Sequenal Containers

std::array

1 2 3 4

data std::array<int, 4>

array[3] = 4;

slide-70
SLIDE 70

30 / 47

Sequenal Containers

std::array

‚ inseron: not applicable ‚ deleon: not applicable ‚ lookup: Op1q

slide-71
SLIDE 71

31 / 47

Sequenal Containers

std::array

` No memory allocaons ` Data never move in memory ´ Fixed size ´ Size must be known during compilaon

slide-72
SLIDE 72

32 / 47

Sequenal Containers

Example

#include <array> // ... int main() { std::array<int, 5> data{}; for (unsigned i{}; i < data.size(); ++i) { cin >> data.at(i); } for (auto&& i : data) { cout << i << endl; } }

slide-73
SLIDE 73

33 / 47

Sequenal Containers

std::vector

data size capacity

4

std::vector<T>

std::vector<int> vector{};

slide-74
SLIDE 74

33 / 47

Sequenal Containers

std::vector

data size capacity

4

std::vector<T>

vector.push_back(1);

slide-75
SLIDE 75

33 / 47

Sequenal Containers

std::vector

1

data size

1

capacity

4

std::vector<T>

vector.push_back(1);

slide-76
SLIDE 76

33 / 47

Sequenal Containers

std::vector

1

data size

1

capacity

4

std::vector<T>

vector.push_back(2);

slide-77
SLIDE 77

33 / 47

Sequenal Containers

std::vector

1 2

data size

2

capacity

4

std::vector<T>

vector.push_back(2);

slide-78
SLIDE 78

33 / 47

Sequenal Containers

std::vector

1 2

data size

2

capacity

4

std::vector<T>

vector.push_back(3);

slide-79
SLIDE 79

33 / 47

Sequenal Containers

std::vector

1 2 3

data size

3

capacity

4

std::vector<T>

vector.push_back(3);

slide-80
SLIDE 80

33 / 47

Sequenal Containers

std::vector

1 2 3

data size

3

capacity

4

std::vector<T>

vector.push_back(4);

slide-81
SLIDE 81

33 / 47

Sequenal Containers

std::vector

1 2 3 4

data size

4

capacity

4

std::vector<T>

vector.push_back(4);

slide-82
SLIDE 82

33 / 47

Sequenal Containers

std::vector

1 2 3 4

data size

4

capacity

4

std::vector<T>

vector.push_back(5);

slide-83
SLIDE 83

33 / 47

Sequenal Containers

std::vector

1 2 3 4

data size

5

capacity

8

std::vector<T>

vector.push_back(5);

slide-84
SLIDE 84

33 / 47

Sequenal Containers

std::vector

1 2 3 4 5

data size

5

capacity

8

std::vector<T>

vector.push_back(5);

slide-85
SLIDE 85

33 / 47

Sequenal Containers

std::vector

1 2 3 4 5

data size

5

capacity

8

std::vector<T>

vector.erase(vector.begin() + 2);

slide-86
SLIDE 86

33 / 47

Sequenal Containers

std::vector

1 2 4 5

data size

4

capacity

8

std::vector<T>

vector.erase(vector.begin() + 2);

slide-87
SLIDE 87

33 / 47

Sequenal Containers

std::vector

1 2 4 5

data size

4

capacity

8

std::vector<T>

vector.erase(vector.begin() + 2);

slide-88
SLIDE 88

33 / 47

Sequenal Containers

std::vector

1 2 4 5

data size

4

capacity

8

std::vector<T>

vector.erase(vector.begin() + 2);

slide-89
SLIDE 89

33 / 47

Sequenal Containers

std::vector

1 2 4 5

data size

4

capacity

8

std::vector<T>

vector.erase(vector.begin() + 2);

slide-90
SLIDE 90

33 / 47

Sequenal Containers

std::vector

1 2 4 5

data size

4

capacity

8

std::vector<T>

vector.erase(vector.begin() + 2);

slide-91
SLIDE 91

34 / 47

Sequenal Containers

std::vector

‚ inseron: ‚ at end: Op1q ‚ otherwise: Opnq ‚ deleon: ‚ last element: Op1q ‚ otherwise: Opnq ‚ lookup: Op1q

slide-92
SLIDE 92

35 / 47

Sequenal Containers

std::vector

` Data is sequenal in memory ` Dynamic size ´ Enre data range can move in memory ´ Dynamic allocaons are slow

slide-93
SLIDE 93

36 / 47

Sequenal Containers

Example

#include <vector> // ... int main() { std::vector<int> data{}; int x{}; while (cin >> x) { data.push_back(x); } for (auto&& i : data) cout << i << endl; }

slide-94
SLIDE 94

37 / 47

Sequenal Containers

std::list

first last size std::list<T>

std::list<int> list{};

slide-95
SLIDE 95

37 / 47

Sequenal Containers

std::list

first last size std::list<T>

list.push_back(1);

slide-96
SLIDE 96

37 / 47

Sequenal Containers

std::list

first last size

1

std::list<T> 1

list.push_back(1);

slide-97
SLIDE 97

37 / 47

Sequenal Containers

std::list

first last size

1

std::list<T> 1

list.push_back(2);

slide-98
SLIDE 98

37 / 47

Sequenal Containers

std::list

first last size

2

std::list<T> 1 2

list.push_back(2);

slide-99
SLIDE 99

37 / 47

Sequenal Containers

std::list

first last size

2

std::list<T> 1 2

list.push_back(3);

slide-100
SLIDE 100

37 / 47

Sequenal Containers

std::list

first last size

3

std::list<T> 1 2 3

list.push_back(3);

slide-101
SLIDE 101

37 / 47

Sequenal Containers

std::list

first last size

3

std::list<T> 1 2 3

list.push_back(4);

slide-102
SLIDE 102

37 / 47

Sequenal Containers

std::list

first last size

4

std::list<T> 1 2 3 4

list.push_back(4);

slide-103
SLIDE 103

38 / 47

Sequenal Containers

std::list

‚ inseron: ‚ at the ends: Op1q ‚ otherwise: Opnq ‚ deleon: ‚ first or last element: Op1q ‚ otherwise: Opnq ‚ lookup: Opnq

slide-104
SLIDE 104

39 / 47

Sequenal Containers

std::list

` elements never move in memory ` Operaons around a specific element is Op1q ´ Many allocaons (one for each element) ´ Linear lookup Makes the CPU cache very sad :(

slide-105
SLIDE 105

39 / 47

Sequenal Containers

std::list

` elements never move in memory ` Operaons around a specific element is Op1q ´ Many allocaons (one for each element) ´ Linear lookup ´ Makes the CPU cache very sad :(

slide-106
SLIDE 106

40 / 47

Sequenal Containers

Example

#include <list> #include <vector> // ... int main() { std::list<int> data{}; std::vector<int*> order{}; int x; while (cin >> x) { data.push_back(x);

  • rder.push_back(&data.back());

} data.sort(); int i{0}; for (auto&& val : data) { cout << val << ", " << *order[i++] << endl; } }

slide-107
SLIDE 107

41 / 47

Sequenal Containers

std::forward_list

first size std::forward_list<T>

std::forward_list<int> list{};

slide-108
SLIDE 108

41 / 47

Sequenal Containers

std::forward_list

first size std::forward_list<T>

list.push_front(1);

slide-109
SLIDE 109

41 / 47

Sequenal Containers

std::forward_list

first size

1

std::forward_list<T> 1

list.push_front(1);

slide-110
SLIDE 110

41 / 47

Sequenal Containers

std::forward_list

first size

1

std::forward_list<T> 1

list.push_front(2);

slide-111
SLIDE 111

41 / 47

Sequenal Containers

std::forward_list

first size

2

std::forward_list<T> 2 1

list.push_front(2);

slide-112
SLIDE 112

41 / 47

Sequenal Containers

std::forward_list

first size

2

std::forward_list<T> 2 1

list.push_front(3);

slide-113
SLIDE 113

41 / 47

Sequenal Containers

std::forward_list

first size

3

std::forward_list<T> 3 2 1

list.push_front(3);

slide-114
SLIDE 114

41 / 47

Sequenal Containers

std::forward_list

first size

3

std::forward_list<T> 3 2 1

list.push_front(4);

slide-115
SLIDE 115

41 / 47

Sequenal Containers

std::forward_list

first size

4

std::forward_list<T> 4 3 2 1

list.push_front(4);

slide-116
SLIDE 116

42 / 47

Sequenal Containers

std::forward_list

‚ inseron: ‚ in beginning: Op1q ‚ otherwise: Opnq ‚ deleon: ‚ first element: Op1q ‚ otherwise: Opnq ‚ lookup: Opnq

slide-117
SLIDE 117

43 / 47

Sequenal Containers

std::forward_list

` Less memory per element compared to std::list ´ No Op1q operaons on last element ´ Unable to go backwards

slide-118
SLIDE 118

44 / 47

Sequenal Containers

std::deque

begin data size chunks std::deque<T>

std::deque<int> deque{};

slide-119
SLIDE 119

44 / 47

Sequenal Containers

std::deque

begin data size chunks std::deque<T>

deque.push_back(1);

slide-120
SLIDE 120

44 / 47

Sequenal Containers

std::deque

begin data size chunks

1

std::deque<T>

deque.push_back(1);

slide-121
SLIDE 121

44 / 47

Sequenal Containers

std::deque

begin data size

1

chunks

1

std::deque<T>

1

deque.push_back(1);

slide-122
SLIDE 122

44 / 47

Sequenal Containers

std::deque

begin data size

1

chunks

1

std::deque<T>

1

deque.push_back(2);

slide-123
SLIDE 123

44 / 47

Sequenal Containers

std::deque

begin data size

2

chunks

1

std::deque<T>

1 2

deque.push_back(2);

slide-124
SLIDE 124

44 / 47

Sequenal Containers

std::deque

begin data size

2

chunks

1

std::deque<T>

1 2

deque.push_back(3);

slide-125
SLIDE 125

44 / 47

Sequenal Containers

std::deque

begin data size

3

chunks

1

std::deque<T>

1 2 3

deque.push_back(3);

slide-126
SLIDE 126

44 / 47

Sequenal Containers

std::deque

begin data size

3

chunks

1

std::deque<T>

1 2 3

deque.push_back(4);

slide-127
SLIDE 127

44 / 47

Sequenal Containers

std::deque

begin data size

4

chunks

1

std::deque<T>

1 2 3 4

deque.push_back(4);

slide-128
SLIDE 128

44 / 47

Sequenal Containers

std::deque

begin data size

4

chunks

1

std::deque<T>

1 2 3 4

deque.push_back(5);

slide-129
SLIDE 129

44 / 47

Sequenal Containers

std::deque

begin data size

4

chunks

2

std::deque<T>

1 2 3 4

deque.push_back(5);

slide-130
SLIDE 130

44 / 47

Sequenal Containers

std::deque

begin data size

5

chunks

2

std::deque<T>

1 2 3 4 5

deque.push_back(5);

slide-131
SLIDE 131

44 / 47

Sequenal Containers

std::deque

begin data size

5

chunks

2

std::deque<T>

1 2 3 4 5

deque.pop_front();

slide-132
SLIDE 132

44 / 47

Sequenal Containers

std::deque

begin data size

4

chunks

2

std::deque<T>

2 3 4 5

deque.pop_front();

slide-133
SLIDE 133

44 / 47

Sequenal Containers

std::deque

begin data size

4

chunks

2

std::deque<T>

2 3 4 5

deque.erase(deque.begin() + 2);

slide-134
SLIDE 134

44 / 47

Sequenal Containers

std::deque

begin data size

3

chunks

2

std::deque<T>

2 3 5

deque.erase(deque.begin() + 2);

slide-135
SLIDE 135

44 / 47

Sequenal Containers

std::deque

begin data size

3

chunks

2

std::deque<T>

2 3 5

deque.erase(deque.begin() + 2);

slide-136
SLIDE 136

44 / 47

Sequenal Containers

std::deque

begin data size

3

chunks

2

std::deque<T>

2 3 5

deque.erase(deque.begin() + 2);

slide-137
SLIDE 137

44 / 47

Sequenal Containers

std::deque

begin data size

3

chunks

1

std::deque<T>

2 3 5

deque.erase(deque.begin() + 2);

slide-138
SLIDE 138

45 / 47

Sequenal Containers

std::deque

‚ inseron: ‚ at ends: Op1q ‚ otherwise: Opnq ‚ deleon: ‚ at ends: Op1q ‚ otherwise: Opnq ‚ lookup: Op1q

slide-139
SLIDE 139

46 / 47

Sequenal Containers

std::deque

` Elements rarely move in memory ` Fast operaons at ends ` More cache friendly than std::list ´ Not congous in memory ´ Addional complexity gives slighly worse performance

slide-140
SLIDE 140

47 / 47

Sequenal Containers

Uses

‚ Great for queues and stacks! ‚ Will automacally shrink the container so use it when there are a lot of inserons and deleons

slide-141
SLIDE 141

www.liu.se