tddd38/726g82- advancedprogrammingin c++tddd38/lecture/slides/stl_ii.pdf · 50/67 lambdafuncons...
TRANSCRIPT
![Page 1: TDDD38/726G82- Advancedprogrammingin C++TDDD38/lecture/slides/stl_II.pdf · 50/67 LambdaFuncons Example template bool is_sorted(Container](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/1.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/2.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/3.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/4.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/5.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/6.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/7.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/8.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/9.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/10.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/11.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/12.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/13.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/14.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/15.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/16.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/17.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/18.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/19.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/20.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/21.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/22.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/23.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/24.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/25.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/26.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/27.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/28.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/29.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/30.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/31.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/32.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/33.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/34.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/35.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/36.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/37.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/38.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/39.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/40.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/41.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/42.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/43.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/44.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/45.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/46.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/47.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/48.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/49.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/50.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/51.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/52.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/53.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/54.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/55.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/56.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/57.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/58.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/59.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/60.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/61.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/62.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/63.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/64.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/65.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/66.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/67.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/68.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/69.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/70.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/71.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/72.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/73.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/74.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/75.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/76.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/77.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/78.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/79.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/80.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/81.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/82.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/83.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/84.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/85.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/86.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/87.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/88.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/89.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/90.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/91.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/92.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/93.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/94.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/95.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/96.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/97.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/98.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/99.jpg)
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](https://reader031.vdocuments.us/reader031/viewer/2022040507/5e47431b372a2f20a56c2862/html5/thumbnails/100.jpg)
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;
}