101
TDDD38/726G82 - Advanced programming in C++ STL II Christoffer Holm Department of Computer and informaƟon science

TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template bool is_sorted(Container

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

TDDD38/726G82 -Advanced programming inC++STL II

Christoffer Holm

Department of Computer and informa on science

Page 2: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

1 Iterators2 Associa ve Containers3 Container Adaptors4 Lambda Func ons

Page 3: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

1 Iterators2 Associa ve Containers3 Container Adaptors4 Lambda Func ons

Page 4: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

3 / 67

IteratorsGeneral itera ons

for (auto&& element : c){// ...

}

Page 5: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

4 / 67

IteratorsGeneral itera ons

‚ itera ng over a sequen al container means goingthrough all the elements in order

‚ seman cally this is the same for all containers coveredso far

‚ however; implementa on varies wildly

Page 6: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

5 / 67

IteratorsGeneral itera ons

‚ with std::vector and std::array we can iterate overthe actual memory

‚ for std::list and std::forward_list we have tofollow pointers

‚ std::deque is a combina on of the two

Page 7: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

6 / 67

IteratorsGeneral itera ons

‚ Wri ng general code using these containers requiresabstrac on

‚ we want one universal way of itera ng over anycontainer

‚ this has been solved with iterators

Page 8: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

7 / 67

IteratorsGeneral itera ons

for (auto it{c.begin()}; it != c.end(); ++it){auto&& element{*it};// ...

}

Page 9: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

8 / 67

IteratorsIterators

auto it{c.begin()};auto end{c.end()};while (it != end){// ...++it;

}

auto it{c.data()};auto end{it + c.size()};while (it != end){// ...++it;

}

Page 10: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

9 / 67

IteratorsIterators

‚ iterators can be thought of as generalized pointers

‚ share similar interface and seman cs with pointers

‚ However; no assump ons about the memory layout ofelements

‚ the end-iterator signifies that the itera on is complete,i.e. we have visited all elements

‚ the end-iterator does not point to the last element, butrather one element past the last element

Page 11: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

10 / 67

IteratorsIterator categories

‚ ForwardIterator

‚ can only step forward in the container

‚ Bidirec onalIterator

‚ can step forward and backwards in the container

‚ RandomAccessIterator

‚ can access any element in the container

Page 12: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

11 / 67

IteratorsInputIterator

std::vector<int> v{};auto it{v.begin()};for (int i{0}; i < 10; ++i){*it++ = i;

}

Page 13: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

11 / 67

IteratorsInputIterator

std::vector<int> v{};auto it{v.begin()};for (int i{0}; i < 10; ++i){*it++ = i;

}

‚ Won’t work; v.begin() returns an InputIterator

‚ InputIterators can only access exis ng elements

‚ v.insert or v.push_back to add elements

Page 14: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

12 / 67

IteratorsOutputIterator

‚ The standard library is built on using iterators

‚ Iterators define some kind of behaviour for variouscomponents

‚ Some mes we want the iterators to add elementsrather than modify exis ng ones

‚ This is where OutputIterators come in

Page 15: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

13 / 67

IteratorsOutputIterator

‚ InputIterator

` Can access elements in container

´ Cannot add elements to container

‚ OutputIterator

` Can add elements to container

´ Cannot access elements in container

Page 16: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

13 / 67

IteratorsOutputIterator

‚ InputIterator

` Can access elements in container

´ Cannot add elements to container

‚ OutputIterator

` Can add elements to container

´ Cannot access elements in container

Page 17: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

14 / 67

IteratorsOutputIterators

‚ OutputIterator is an Iterator so it must defineoperator++ and operator*

‚ An OutputIterator cannot access elements sodereferencing the iterator shouldn’t do anything

‚ Likewise shouldn’t incremen ng the iterator doanything

‚ The only opera on that performs any work isoperator= which will insert the right-hand side intothe container

Page 18: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

15 / 67

Iteratorsstd::insert_iterator

std::vector<int> v{};auto it{std::inserter(v, v.end())};for (int i{0}; i < 10; ++i){*it++ = i;

}

Page 19: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

15 / 67

Iteratorsstd::insert_iterator

std::vector<int> v{};auto it{std::inserter(v, v.end())};for (int i{0}; i < 10; ++i){*it = i;

}

Page 20: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

15 / 67

Iteratorsstd::insert_iterator

std::vector<int> v{};auto it{std::inserter(v, v.end())};for (int i{0}; i < 10; ++i){it = i;

}

Page 21: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

15 / 67

Iteratorsstd::insert_iterator

std::vector<int> v{};auto it{std::inserter(v, v.end())};for (int i{0}; i < 10; ++i){v.insert(v.end(), i);

}

Page 22: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

16 / 67

Iteratorsstd::insert_iterator

‚ Each assignment calls insert on the underlyingcontainer

‚ Is created with std::inserter

‚ must know which container it should operate on

‚ must know where in the container the inser on shouldhappen

‚ Works with any container that has an insert func on

Page 23: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

17 / 67

IteratorsOther output iterators

‚ std::back_inserter

‚ std::front_inserter

‚ These only need to know the container, since theirinser on posi ons are fixed

Page 24: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

17 / 67

IteratorsOther output iterators

‚ std::back_inserter

‚ like std::inserter but adds to the end

‚ calls push_back

‚ std::front_inserter

‚ These only need to know the container, since theirinser on posi ons are fixed

Page 25: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

17 / 67

IteratorsOther output iterators

‚ std::back_inserter

‚ std::front_inserter

‚ Like std::back_inserter but adds to the front

‚ calls push_front

‚ These only need to know the container, since theirinser on posi ons are fixed

Page 26: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

17 / 67

IteratorsOther output iterators

‚ std::back_inserter

‚ std::front_inserter

‚ These only need to know the container, since theirinser on posi ons are fixed

Page 27: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

18 / 67

IteratorsIterator hierarchy

Iterator

InputIterator

ForwardIterator(forward_list)

Bidirec onalIterator(list)

RandomAccessIterator(array) (deque) (vector)

OutputIterator

(front_inserter)(inserter)

(back_inserter)

Page 28: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

19 / 67

IteratorsWhat will be printed?

int main(){

std::vector<int> v {1, 3};*std::back_inserter(v)++ = 7;std::inserter(v, 1 + v.begin()) = ++*v.begin();for (int i : v){

std::cout << i << " ";}

}

Page 29: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

1 Iterators2 Associa ve Containers3 Container Adaptors4 Lambda Func ons

Page 30: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{}

std::set<int> set{};

Page 31: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{}

std::set<int> set{};

Page 32: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{}

set.insert(4);

Page 33: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{4}

set.insert(4);

Page 34: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{4}

set.insert(3);

Page 35: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{3, 4}

set.insert(3);

Page 36: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{3, 4}

set.insert(5);

Page 37: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{3, 4, 5}

set.insert(5);

Page 38: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{3, 4, 5}

set.insert(1);

Page 39: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{1, 3, 4, 5}

set.insert(1);

Page 40: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{1, 3, 4, 5}

set.insert(2);

Page 41: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{1, 2, 3, 4, 5}

set.insert(2);

Page 42: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{1, 2, 3, 4, 5}

set.erase(3);

Page 43: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

21 / 67

Associa ve Containersstd::set

{1, 2, 4, 5}

set.erase(3);

Page 44: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

22 / 67

Associa ve Containersstd::set

‚ std::set contains a set of unique values

‚ requires that the data type of the elements arecomparable

‚ will iterate through the elements in sorted order

‚ is represented as a binary search tree

Page 45: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

23 / 67

Associa ve Containersstd::set

‚ inser on: Oplog nq

‚ dele on: Oplog nq

‚ lookup: Oplog nq

Page 46: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

24 / 67

Associa ve ContainersExample

#include <set>// ...int main(){std::set<std::string> words{};std::string str;while (cin >> str){set.insert(str);

}for (auto const& word : words){cout << word << endl;

}}

Page 47: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

std::map<std::string, int> map{};

Page 48: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

map["c"] = 3;

Page 49: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"c" 3

map["c"] = 3;

Page 50: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"c" 3

map["a"] = 1;

Page 51: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"a" 1"c" 3

map["a"] = 1;

Page 52: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"a" 1"c" 3

map["d"] = 4;

Page 53: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"a" 1"c" 3"d" 4

map["d"] = 4;

Page 54: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"a" 1"c" 3"d" 4

map["b"] = 2;

Page 55: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

25 / 67

Associa ve Containersstd::map

"a" 1"b" 2"c" 3"d" 4

map["b"] = 2;

Page 56: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

26 / 67

Associa ve Containersstd::map

‚ std::map associates a key with a value

‚ keys must be unique and comparable

‚ will iterate through the key-value pairs in sorted order(according to the key)

‚ both lookup and inser on can be done withoperator[]

‚ implemented as a binary search tree

Page 57: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

27 / 67

Associa ve Containersstd::map

‚ inser on: Oplog nq

‚ dele on: Oplog nq

‚ lookup: Oplog nq

Page 58: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

28 / 67

Associa ve ContainersExample

#include <map>// ...int main(){std::map<std::string, int> words{};std::string str;while (cin >> str){words[str]++;

}for (std::pair<std::string, int> const& p : words){cout << p.first << ": " << p.second << endl;

}}

Page 59: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

29 / 67

Associa ve ContainersVariants

‚ std::set and std::map are the base associa vecontainers

‚ but there are several varia ons of these containers thatcan be used

‚ it is important to choose the appropriate variant

Page 60: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

30 / 67

Associa ve ContainersVariants

‚ multi*

‚ unordered_*

Page 61: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

30 / 67

Associa ve ContainersVariants

‚ multi*

‚ std::multiset

‚ std::multimap

‚ unordered_*

Page 62: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

30 / 67

Associa ve ContainersVariants

‚ multi*

‚ unordered_*

‚ std::unordered_set

‚ std::unordered_map

‚ std::unordered_multiset

‚ std::unordered_multimap

Page 63: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

31 / 67

Associa ve Containersstd::multiset and std::map

‚ just like std::set and/or std::map but with oneexcep on;

‚ possible to store mul ple duplicates of the same key

‚ can be likened to a list where elements are sorted bythe key

‚ In general these variants are more costly compared totheir normal counterparts

Page 64: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

32 / 67

Associa ve Containersunordered variants

‚ works like the other associa ve containers;

‚ but the elements are not sorted

‚ these are usually implemented as hash tables

‚ o en a bit faster than the other variants since there areless constraints on the implementa on

‚ Note: the order is not defined, so assume nothingabout the order

Page 65: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

1 Iterators2 Associa ve Containers3 Container Adaptors4 Lambda Func ons

Page 66: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

34 / 67

Container AdaptorsAdaptors

‚ adaptors are wrappers around other containers

‚ adaptors exposes specific interfaces

‚ but they are not containers by themselves

Page 67: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

35 / 67

Container Adaptorsstd::stack

template <typename T,typename Container = std::deque<T>>

class stack;

std::stack<int> st{};st.top(); // top of stackst.push(); // push to stackst.pop(); // pop the stack

Page 68: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

36 / 67

Container Adaptorsstd::stack

std::stack can be wrapped around any container that hasthe following member func ons:

‚ back()

‚ push_back()

‚ pop_back()

Page 69: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

37 / 67

Container Adaptorsstd::queue

template <typename T,typename Container = std::deque<T>>

class queue;

std::queue<int> q{};q.front(); // front of the queueq.back(); // back of the queueq.push(); // add element to back of queueq.pop(); // pop first element of the queue

Page 70: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

38 / 67

Container Adaptorsstd::queue

std::queue can be wrapped around any container that hasthe following member func ons:

‚ back()

‚ front()

‚ push_back()

‚ pop_front()

Page 71: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

39 / 67

Container Adaptorsstd::priority_queue

template <typename T,typename Container = std::vector<T>,typename Compare = std::less<T>>

class priority_queue;

std::priority_queue<int> pq{};pq.top(); // get the largest valuepq.push(); // add an elementpq.pop(); // extract the largest value

Page 72: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

40 / 67

Container Adaptorsstd::priority_queue

‚ represents a (min- or max) heap

‚ stores it in some array-like container

‚ we can supply some custom comparator

Page 73: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

41 / 67

Container Adaptorsstd::priority_queue

std::priority_queue can be wrapped around anycontainer which fullfill the following requirements:

‚ the container has RandomAccessIterators

‚ It must provide the following func ons:

‚ front()

‚ push_back()

‚ pop_back()

Page 74: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

42 / 67

Container AdaptorsExample

int main(){std::priority_queue<float, std::greater<float>> q{};

float value;while (cin >> value){q.push(value);

}

float sum{0.0};while (!q.empty()){sum += q.top();q.pop();

}

cout << sum << endl;}

Page 75: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

43 / 67

Container AdaptorsExplana on

‚ floa ng point addi on is more precise for smallervalues

‚ so if we add the smallest elements first we will getbe er accuracy

‚ use the std::greater comparator to make our queuea min-heap

‚ that way, we will add the numbers in ascending order

Page 76: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

1 Iterators2 Associa ve Containers3 Container Adaptors4 Lambda Func ons

Page 77: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

45 / 67

Lambda Func onsPossible implementa on of std::less

template <typename T>struct less{bool operator()(T const& lhs,

T const& rhs){return lhs < rhs;

}};

int main(){less<int> obj{};

// we can use the function call// operator to treat this object// as a functioncout << obj(1, 2) << endl;

}

Page 78: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

46 / 67

Lambda Func onsPossible implementa on of std::less

‚ less is a class-type

‚ less defines the func on call operator operator()

‚ therefore all instances of less are both objects andfunc ons at the same me

‚ these types of classes are called func on objects orfunctors

Page 79: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

47 / 67

Lambda Func onsFirst-class func ons

template <typename Function>auto perform(Function f) -> decltype(f()){return f();

}

Page 80: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

48 / 67

Lambda Func onsFirst-class func ons

struct my_function{int operator()(){ return 1; }

};

int main(){my_function f{};perform(f);

}

Page 81: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

49 / 67

Lambda Func onsFirst-class func ons

‚ Func on-objects allows us to treat func ons as data

‚ we can pass func on-objects as parameters

‚ with this we can create highly customizable code

Page 82: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

50 / 67

Lambda Func onsExample

template <typename Container,typename Compare>

bool is_sorted(Container const& c,Compare const& comp)

{auto it{c.begin()};auto prev{it++};for (; it != c.end(); ++it){if (!comp(*prev, *it))return false;

prev = it;}return true;

}

int main(){std::vector<int> v{1,2,3,4};std::deque<int> d{3,2,1,0};

std::less<int> lt{};std::greater<int> gt{};

cout << is_sorted(v, lt);cout << is_sorted(d, gt);

}

Page 83: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

51 / 67

Lambda Func onsExample

‚ The sorted func on uses comp to compare eachelement with the one before

‚ note that we can pass in any func on object as secondparameter

‚ as long as comp is callable, takes two parameters andreturns a bool this will work

‚ will work for both func on objects and normalfunc ons

‚ we can even define our own func on object

Page 84: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

52 / 67

Lambda Func onsLambda expressions

std::vector<int> v{10, -1, 12};

is_sorted(v, [](int a, int b) -> bool{return abs(a - 10) < abs(b - 10);

});

Page 85: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

53 / 67

Lambda Func onsLambda expressions

[](int a, int b) -> bool{return abs(a - 10) < abs(b - 10);

}

struct my_lambda{bool operator()(int a, int b){return abs(a - 10) < abs(b - 10);

}};

Page 86: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

54 / 67

Lambda Func onsLambda expressions

‚ Lambda func ons:[ captures ] ( parameters ) -> result { body; }

‚ essen ally short-hand nota on for genera ng func onobjects

‚ useful when crea ng func ons that are passed asparameters

Page 87: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

55 / 67

Lambda Func onsCaptures

std::vector<int> v{10, -1, 12};int x{10};

auto comp{[x](int a, int b) -> bool{return abs(a - x) < abs(b - x);

}};

is_sorted(v, comp);

Page 88: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

56 / 67

Lambda Func onsCaptures

[x](int a, int b) -> bool{return abs(a - x) < abs(b - x);

}

struct my_lambda{my_lambda(int x) : x{x} { }bool operator()(int a, int b){return abs(a - x) < abs(b - x);

}private:int const x;

};

Page 89: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

57 / 67

Lambda Func onsCaptures

[&x](int a, int b) -> bool{return abs(a - x) < abs(b - x);

}

struct my_lambda{my_lambda(int& x) : x{x} { }bool operator()(int a, int b){return abs(a - x) < abs(b - x);

}private:int& x;

};

Page 90: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

58 / 67

Lambda Func onsCaptures

[x = 10](int a, int b) -> bool{return abs(a - x) < abs(b - x);

}

struct my_lambda{my_lambda() : x{10} { }bool operator()(int a, int b){return abs(a - x) < abs(b - x);

}private:int const x;

};

Page 91: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

59 / 67

Lambda Func onsCaptures

[x, &y, z = 10](/* function parameters */){// ...

}

Page 92: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

60 / 67

Lambda Func onsCaptures

‚ It is possible to make extra variables available insidelambda expressions

‚ These extra variables (non-parameters) are said to becaptured inside the lambda

‚ They can either be copies or references of variablesdeclared outside the lambda

‚ They can also be completely new variables that a ainits current value between calls

Page 93: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

61 / 67

Lambda Func onsmutable

int x{};auto f = [x]() { x = 1; };

Page 94: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

61 / 67

Lambda Func onsmutable

int x{};auto f = [x]() mutable { x = 1; };

Page 95: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

62 / 67

Lambda Func onsmutable

‚ Everything captured by-value in lambdas will be const

‚ Including variables defined inside the lambda

‚ To make them non-const we have to declare thelambda as mutable

Page 96: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

63 / 67

Lambda Func onsSpecial captures

int global{1};int main(){int x{2};int y{3};auto f{[&]()

{return x + y + global;

}};f(); // will return 6y = -3;f(); // will return 0

}

Page 97: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

64 / 67

Lambda Func onsSpecial captures

int global{1};int main(){int x{2};int y{3};auto f{[=]()

{return x + y + global;

}};f(); // will return 6y = -3;f(); // will return 6

}

Page 98: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

65 / 67

Lambda Func onsSpecial captures

‚ Capture everything as reference:

[&] ( parameters ) -> result { body }

‚ Capture everything as a copy:

[=] ( parameters ) -> result { body }

‚ Both of these act as if they capture every variableaccessible in the code at the point of defini on

‚ However; in reality they will capture only thosevariables that are used inside the body

Page 99: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

66 / 67

Lambda Func onsMixing captures

[=, &x](/* parameters */){// ...

}

Capture everything as copy, except x; capture x as reference.

Page 100: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

67 / 67

Lambda Func onsWhat will be printed?

int main(){auto f = [n = 0]() mutable { return n++; };auto g { f };cout << f() << ' '

<< f() << ' '<< g() << endl;

}

Page 101: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template  bool is_sorted(Container

www.liu.se