0% found this document useful (0 votes)
66 views72 pages

Section Ranges Library in Cpp20 Slides

The document discusses the ranges library in C++20, which includes range algorithms, projections, views and view adaptors, function composition, and range factories. It covers topics like input and output iterators and ranges, constrained iterator pair algorithms, and using projections with algorithms.

Uploaded by

Akash Karn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views72 pages

Section Ranges Library in Cpp20 Slides

The document discusses the ranges library in C++20, which includes range algorithms, projections, views and view adaptors, function composition, and range factories. It covers topics like input and output iterators and ranges, constrained iterator pair algorithms, and using projections with algorithms.

Uploaded by

Akash Karn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Slides

Section : Ranges Library in


C++20
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Ranges Library in C++20

3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Range algorithms
• Projections
• Views and view adaptors
• Function composition
• Range factories

4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Input iterators Output iterators

Forward iterators

Bidirectional iterators

Random access iterators

Contiguous iterators

5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Input Range Output Range

Forward Range

Bidirectional Range

Random access Range

Contiguous Range

6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Range Algorithms

8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Standard Template Library

Containers Algorithms Iterators

9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Legacy algorithms Work on iterator pairs

Range algorithms Work on containers directly

10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::all_of [Iterator pair]

11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::all_of

12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::for_each

13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::sort

14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::find

15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy into output stream on the fly

16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
More

https://en.cppreference.com/w/cpp/algorithm

17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constrained Iterator Pair
Algorithms

19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Standard Template Library

Containers Algorithms Iterators

20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Legacy algorithms Work on iterator pairs

Range algorithms Work on containers directly

21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::all_of [Iterator pair]

22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::all_of

23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::for_each

24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::sort

25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges::find

26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy into output stream on the fly

27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constrained Iterator Pair Algorithms

29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::ranges algorithms should be PREFERED

33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Projections

35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Some algorithms support projections

36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Points are ordered by distance by default

37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Points are ordered by distance by default

38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Sorting without projection

39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Using lambda as a projection

40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Using public member as a projection

41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Projections with the for_each algorithm

42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Views and range adaptors

44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• A view is a non owning range
• It’s like a window we can set up to view some real data without
setting up the infrastructure to store data
• Views are cheap to copy and pass around as function
parameters by design.

45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Evens

Odds

1 23 3 6 3 67 18 4 …

< 10

46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Filtering view

47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Filtering view

48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Transform_view

49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compiler errors while constructing some views directly

52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Filter range adaptor

53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Student type

54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Students in a classroom

55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
View composition and pipe
operator

57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Composing multiple views

58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Filter evens

Collection View evens

Square View squares


transform

59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Raw function composition

60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pipe operator

61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pipe operator

62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Range factories

66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Producing views out of the blue

67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
iota

68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
iota

69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Ranges Library in C++20 : Summary

71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Range algorithms
• Projections
• Views and view adaptors
• Function composition
• Range factories

72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya

You might also like