tddd38 - advanced programming in c++ - stl iiitddd38/lecture/slides/stl_iii.pdf · 4/62...

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

Upload: others

Post on 03-Sep-2019

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

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

Christoffer Holm

Department of Computer and informa on science

Page 2: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

1 More on Func on Objects2 More on Iterators3 Algorithms

Page 3: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

1 More on Func on Objects2 More on Iterators3 Algorithms

Page 4: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

3 / 63

More on Func on Objectsstd::func on

#include <functional>void foo() { }struct Functor{void operator()() { }

};int main(){std::function<void()> fun;fun = foo;fun = Functor{};fun = []() { };

}

Page 5: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

4 / 63

More on Func on Objectsstd::function

‚ A type to represent any callable object

‚ Specify in the template parameter what signature thecallable object must have

‚ can be used to store lambdas as variables

Page 6: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

5 / 63

More on Func on ObjectsSome problems with std::function

void foo(int) { }void bar(int, int = 0) { }int main(){std::function<void(int)> fun {foo}; // ok!fun = bar; // not ok

}

Page 7: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

6 / 63

More on Func on ObjectsSome problems with std::function

‚ the specified signature must be an exact match

‚ it is not enough that the func on we are trying to storeis callable in the specified way

Page 8: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

7 / 63

More on Func on ObjectsA possible solu on

void foo(int) { }void bar(int, int = 0) { }int main(){std::function<void(int)> fun {foo};fun = std::bind(bar, placeholders::_1, 0);

}

Page 9: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

8 / 63

More on Func on Objectsstd::bind

‚ std::bind generates a func on-object

‚ the func on-object will be based on some otherfunc on-object

‚ with std::bind we can create lambdas that callfunc ons where some or all of the arguments havespecified values

Page 10: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

9 / 63

More on Func on Objectsstd::bind

int add(int x, int y){return x + y;

}

int main(){std::bind(add, 5, 10)(); // will return 15

}

Page 11: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

10 / 63

More on Func on Objectsstd::bind

int main(){auto f{std::bind(add, placeholders::_1, 10)};f(0); // will return 10f(10); // will return 20

}

Page 12: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

11 / 63

More on Func on Objectsstd::bind

int sub(int a, int b){return a - b;

}int main(){using namespace std::placeholders;auto f{std::bind(sub, _2,_1)};f(10, 5); // will return -5f(2, 10); // will return 8

}

Page 13: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

12 / 63

More on Func on Objectsstd::bind

‚ placeholders are used to specify which argumentsshould be free

‚ that is, which arguments should be available in thegenerated func on

‚ placeholders::_1 represents the first argument tothe generated func on

‚ placeholders::_2 represents the second argument,and so on, up to some implemena on defined number

Page 14: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

13 / 63

More on Func on ObjectsAnother problem with std::function

struct Cls{void foo() { }

};

int main(){std::function<void()> fun;Cls c{};fun = c.foo; // not okfun = &Cls::foo; // not ok

}

Page 15: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

14 / 63

More on Func on ObjectsWhy?

‚ Member func ons are not ordinary func ons

‚ Each member func on requires an object to be calledfrom

‚ Due to this, they cannot be bound to std::function

Page 16: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

15 / 63

More on Func on ObjectsSolu on

Cls c{};auto foo{std::mem_fn(&Cls::foo)};std::function<void()> fun;fun = std::bind(foo, c);

Page 17: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

16 / 63

More on Func on Objectsstd::mem_fn

‚ std::mem_fn converts a member func on pointer to anormal func on

‚ the generated func on takes the object as a parameter

‚ therefore we can bind the result of std::mem_fn withthe Cls object c

‚ this will make fun() equivalent to c.foo()

Page 18: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

17 / 63

More on Func on ObjectsWhat will be printed?

int fun1() { return 1; }int fun2(int a) { return a * 3; }

int main(){function<int()> fun{fun1};

fun = bind([](int a, int b){

return a + b;}, 1, fun());

fun = [fun]() { return 2 * fun(); };

cout << bind([](int x, int y, int z){

return fun2(x) + y;}, _2, _1, 17)(fun(), 3);

}

Page 19: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

1 More on Func on Objects2 More on Iterators3 Algorithms

Page 20: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

19 / 63

More on IteratorsItera ng arrays

int array[3] {1, 2, 3};for (int e : array){cout << e << endl;

}

Page 21: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

20 / 63

More on IteratorsItera ng arrays

‚ arrays don’t have member func ons

‚ more specifically; they don’t have begin and endfunc ons

‚ yet we can use range-based for-loops to iterate them

Page 22: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

21 / 63

More on IteratorsRange-based for-loops

for (auto&& e : array){

// ...

}

auto it {std::begin(array)};auto last {std::end(array)};for (; it != last; ++it){auto&& e{*it};// ...

}

Page 23: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

22 / 63

More on Iteratorsstd::begin and std::end

‚ std::begin(v)

‚ if v has a member begin(), return v.begin()

‚ if v is an array, return a pointer to the first element

‚ std::end(v)

‚ if v has a member end(), return v.end()

‚ if v is an array, return a pointer to the elementpast the last

Page 24: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

23 / 63

More on IteratorsDeclara ons

template <typename C>auto begin(C& c) -> decltype(c.begin()){ return c.begin(); }

template <typename T, size_t N>T* begin(T (&a)[N]){ return a; }

Page 25: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

24 / 63

More on IteratorsDeclara ons

template <typename C>auto end(C& c) -> decltype(c.end()){ return c.end(); }

template <typename T, size_t N>T* end(T (&a)[N]){ return a + N; }

Page 26: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

25 / 63

More on IteratorsIterator u lity func ons

‚ std::advance

‚ std::distance

‚ std::next

‚ std::prev

Page 27: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

26 / 63

More on IteratorsU lity

‚ These func ons are more general (work with alliterators)

‚ beware; these u lity func ons comes with some extracost, so think carefully before using them

Page 28: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

27 / 63

More on IteratorsReverse iterators

vector<int> v {1, 2, 3};auto it {v.rbegin()};auto end {v.rend()};

// will write "3 2 1 " to the terminalfor (; it != end; ++it){cout << *it << " ";

}

Page 29: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

28 / 63

More on IteratorsReverse iterators

‚ A special type of iterator that traverses the containerbackwards

‚ rbegin() will point to the last element in the container(so end() != rbegin())

‚ rend() points to the element before the first one (sobegin() != rend())

‚ is only available for containers withBidirec onalIterators

Page 30: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

29 / 63

More on IteratorsConst iterators

set<int> const s {3, 6, 9};auto it {s.cbegin()};auto end {s.cend()};for (; it != end; ++it){// reading is ok!cout << *it << " ";// writing is not ok...*it = 5;

}

Page 31: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

30 / 63

More on IteratorsConst iterators

‚ const_iterator is an iterator that can be used for onlyreading data

‚ disallows modifica on of the underlying object

‚ is the only way to use iterators on containers markedconst

Page 32: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

31 / 63

More on IteratorsAccess func ons

‚ std::rbegin() and std::rend()

‚ std::cbegin() and std::cend()

‚ You can even combine them:

‚ std::crbegin() and std::crend()

Page 33: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

32 / 63

More on IteratorsInput stream iterators

std::istream_iterator<int> iit {std::cin};std::istream_iterator<int> end {};while (iit != end){cout << *iit++ << endl;

}

Page 34: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

33 / 63

More on IteratorsInput stream iterators

‚ std::istream_iterator are iterators that read datafrom an input stream

‚ the template parameter defines what type should beread from the stream

‚ data is read from the stream when the iterator isincremented

‚ dereferencing returns a copy of last read object

‚ is an InputIterator

Page 35: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

34 / 63

More on IteratorsOutput stream iterators

std::ostream_iterator<int> oit {std::cout};oit = 5; // will write 5 to the terminal

Page 36: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

35 / 63

More on IteratorsOutput stream iterators

std::ostream_iterator<int> oit {std::cout, " "};

// will write "0 1 2 3 4 " to the terminalfor (int i{0}; i < 5; ++i){out = i;

}

Page 37: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

36 / 63

More on IteratorsOutput stream iterators

‚ std::ostream_iterator are iterators that write datato some output stream

‚ template parameter defines what type is to be wri en

‚ data is wri en to the stream during assignment

‚ is an OutputIterator

‚ constructor takes an extra parameter that defines adelimiter string to insert a er each write

Page 38: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

1 More on Func on Objects2 More on Iterators3 Algorithms

Page 39: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

38 / 63

AlgorithmsCore of STL

‚ Everything in STL is based around algorithms andcontainers

‚ There are 110+ algorithms defined in the STL (exactamount depends on version)

‚ All algorithms operate on iterator ranges

‚ Uses lambdas and func on objects heavily

‚ Defined (mostly) in <algorithm>

Page 40: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

39 / 63

AlgorithmsAlgorithm categories

‚ Non-modifying sequence opera ons

‚ Modifying sequence opera ons

‚ Par oning opera ons

‚ Sor ng opera ons

‚ Sorted range opera ons

‚ Set opera ons

Page 41: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

40 / 63

AlgorithmsAlgorithm categories

‚ Heap opera ons

‚ Minmax opera ons

‚ Comparison opera ons

‚ Permuta on opera ons

‚ Numeric opera ons (<numeric>)

‚ Unini alized opera ons (<memory>)

Page 42: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

41 / 63

Algorithmsfor_each; the dull one of the bunch!

void put(int n){cout << n << endl;

}

int main(){std::vector<int> v { 1, 2, 3 };std::for_each(std::begin(v), std::end(v), put);

}

Page 43: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

42 / 63

AlgorithmsPossible implementa on

template <typename It, typename Function>Function for_each(It first, It last, Function f){while (first != last){f(*first++);

}return f;

}

Page 44: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

43 / 63

AlgorithmsWhat will be printed?

std::vector<int> v {1, 2, 3};auto f {[x = 0](int n) mutable

{x += n;return x;

}};std::for_each(std::begin(v), std::end(v), f);cout << f(0) << endl;

Page 45: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

44 / 63

AlgorithmsAnswer

‚ 0 will be printed

‚ x is an internal variable accessible inside the lambdawhich will retain its value through each successive callto f

‚ however; std::for_each takes f as a copy, so f willnot have been called when we reach the printstatement

Page 46: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

45 / 63

AlgorithmsPossible fix

std::vector<int> v {1, 2, 3};auto f {[x = 0](int n) mutable

{x += n;return x;

}};std::for_each(std::begin(v), std::end(v),

std::ref(f));cout << f(0) << endl;

Page 47: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

46 / 63

Algorithmsstd::ref

‚ std::ref(x) forces the compiler to interpret x as anlvalue

‚ thus forcing any template-parameters to deduce it as areference parameter rather than a by-value parameter

Page 48: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

47 / 63

AlgorithmsAnother possible fix

std::vector<int> v {1, 2, 3};auto f {[x = 0](int n) mutable

{x += n;return x;

}};f = std::for_each(std::begin(v), std::end(v), f);cout << f(0) << endl;

Page 49: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

48 / 63

Algorithmsstd::find

std::vector<int> v {5, -2, 8, 4, 7};auto it {std::find(std::begin(v), std::end(v), 8)

};if (it == std::end(v)){// we didn't find it :(

}else{cout << *it << endl;

}

Page 50: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

49 / 63

AlgorithmsPredicate algorithms

std::set<int> m { -1, 4, 0, 3 };auto p {[](auto a)

{return a >= 0;

}};if (std::all_of(std::begin(m), std::end(m), p)){// all of the numbers are positive

}

Page 51: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

50 / 63

AlgorithmsPredicate algorithms

template <typename It,typename Predicate>

bool all_of(It first, It last, Predicate p){while (first != last){if (!p(*first++)){return false;

}}return true;

}

Page 52: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

51 / 63

Algorithmsstd::copy

std::vector<int> v {1, 2, 3};std::set<int> s {};std::copy(std::begin(v), std::end(v),

std::inserter(s, std::end(s)));

Page 53: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

52 / 63

AlgorithmsA cool usage of std::copy and stream iterators

std::vector<int> v{1, 2, 3};std::copy(std::begin(v), std::end(v),

std::ostream_iterator<int>{cout, " "});

Page 54: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

53 / 63

AlgorithmsAnother cool usage of std::copy

std::vector<int> v;auto begin{std::istream_iterator<int>{cin}};auto end{std::istream_iterator<int>{}};std::copy(begin, end, std::back_inserter(v));

Page 55: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

54 / 63

Algorithmsstd::transform

std::vector<std::string> v{"1", "2", "3"};std::vector<int> target{};

std::transform(std::begin(v), std::end(v),std::back_inserter(target),[](std::string const& s){return std::stoi(s);

});

Page 56: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

55 / 63

Algorithmsstd::remove_if

std::vector<int> v{1, -1, -7, 4, 2};v.erase(

std::remove_if(std::begin(v), std::end(v),[](int n){return n < 0;

}), std::end(v));

Page 57: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 -1 -7 4 2

endbegin

Page 58: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 -1 -7 4 2

endbegin

Page 59: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 -1 -7 4 2

endbegin

Page 60: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 -7 4 -1

endbegin

Page 61: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 -7 4 -1

beginend

Page 62: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 -7 4 -1

endbegin

Page 63: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 -7 4 -1

endbegin

Page 64: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 4 -7 -1

endbegin

Page 65: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 4 -7 -1

beginend

Page 66: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

56 / 63

Algorithmsstd::remove_if

1 2 4 -7 -1

endbegin

Page 67: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

57 / 63

AlgorithmsA note on modifying sequence opera ons

‚ algorithms operate on iterator ranges

‚ it is not possible to remove or add elements througharbitrary iterators

‚ due to this, no algorithm is able to remove elementsfrom containers

‚ (they are able to add elements with the use of outputiterators)

Page 68: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

58 / 63

AlgorithmsModifying sequence opera ons

‚ All algorithms that are meant to remove elements willinstead move them to the end of the range and returnan iterator to the first removed element

‚ with that iterator we can now call the erase func on ofthe underlying container to actually remove thoseelements

Page 69: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

59 / 63

Algorithmsstd::accumulate

#include <numeric>// ...int main(){std::vector<int> v{1, 2, 3, 4, 5};int sum{std::accumulate(std::begin(v), std::end(v), 0)

};cout << sum << endl; // will print 15

}

Page 70: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

60 / 63

Algorithmsstd::accumulate

std::set<std::string> v{"1", "2", "3"};int result{std::accumulate(std::begin(v), std::end(v), 4,

[](int n, std::string const& s){return n + std::stoi(s);

})};

Page 71: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

61 / 63

Algorithmsstd::accumulate

‚ std::accumulate is like a fold-expression

‚ but it operates during run me on iterator ranges

‚ very flexible when combining values into a single value

Page 72: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

62 / 63

AlgorithmsFinal words

‚ There are a lot of algorithms

‚ You are not expected to memorize them all

‚ However you must be able to find suitable algorithmsand use them

Page 73: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

63 / 63

Algorithms

Now go forth and use this great power!

Page 74: TDDD38 - Advanced programming in C++ - STL IIITDDD38/lecture/slides/stl_III.pdf · 4/62 MoreonFunctionObjects std::function • A type to represent any callable object • Specify

www.liu.se