1 00:00:00,173 --> 00:00:03,840 (pleasant electronic music) 2 00:00:05,340 --> 00:00:06,480 -: Okay, welcome back, 3 00:00:06,480 --> 00:00:10,440 so let's continue with the examples of stateful lambdas. 4 00:00:10,440 --> 00:00:13,440 In this case, I'm in Test Seven. 5 00:00:13,440 --> 00:00:15,660 And you can see that I've scrolled that up right up here, 6 00:00:15,660 --> 00:00:18,690 so you can see my Test Seven right here. 7 00:00:18,690 --> 00:00:21,150 This basically is equivalent to the last, 8 00:00:21,150 --> 00:00:22,740 Test Six that we just did. 9 00:00:22,740 --> 00:00:25,320 The difference is that rather than capture by value 10 00:00:25,320 --> 00:00:26,190 and one by reference, 11 00:00:26,190 --> 00:00:28,470 I'm capturing by reference and two by value. 12 00:00:28,470 --> 00:00:31,260 So it's just mixing things up, so let's go through this. 13 00:00:31,260 --> 00:00:34,560 Here I'm saying capture everything by reference. 14 00:00:34,560 --> 00:00:38,010 Except X and Z, capture them by value. 15 00:00:38,010 --> 00:00:40,470 So again, you can see it's exactly the same thing. 16 00:00:40,470 --> 00:00:43,440 Just, you know, done a little bit different, syntactically. 17 00:00:43,440 --> 00:00:46,320 We're gonna capture everything by reference except X and Z, 18 00:00:46,320 --> 00:00:50,610 so X and Z will be copied. 19 00:00:50,610 --> 00:00:52,320 They're gonna be captured by value. 20 00:00:52,320 --> 00:00:54,183 That's 100 and 300. 21 00:00:56,760 --> 00:00:59,040 And everything else, which in this case is Y, 22 00:00:59,040 --> 00:01:00,780 will be captured by reference. 23 00:01:00,780 --> 00:01:02,880 So there's my reference. 24 00:01:02,880 --> 00:01:04,980 And again, we're gonna increment everything by 100, 25 00:01:04,980 --> 00:01:06,930 and display everything, just like we did before, 26 00:01:06,930 --> 00:01:08,310 we should get the same output. 27 00:01:08,310 --> 00:01:10,710 So I'm gonna call my lambda. 28 00:01:10,710 --> 00:01:12,330 X will be incremented by 100, 29 00:01:12,330 --> 00:01:15,000 that's my local X right here. 30 00:01:15,000 --> 00:01:17,077 Y will be incremented by 100 through this reference, 31 00:01:17,077 --> 00:01:19,053 so that's gonna be 300. 32 00:01:20,100 --> 00:01:21,990 Z will be incremented by 100, 33 00:01:21,990 --> 00:01:24,750 so that'll be 400 right here. 34 00:01:24,750 --> 00:01:26,310 And we're gonna display X, Y, and Z 35 00:01:26,310 --> 00:01:31,310 which are 200, 300, 400 and just what we see. 36 00:01:32,310 --> 00:01:35,040 Now when I come back, from the function, 37 00:01:35,040 --> 00:01:37,170 I'm gonna display X, Y, and Z again, 38 00:01:37,170 --> 00:01:39,330 we're gonna display these guys. 39 00:01:39,330 --> 00:01:44,330 So that would be 100 300 300, 100 300 300. 40 00:01:44,820 --> 00:01:45,960 Okay, perfect. 41 00:01:45,960 --> 00:01:49,050 So now let's do a few with some objects. 42 00:01:49,050 --> 00:01:50,940 In Test Eight, 43 00:01:50,940 --> 00:01:53,970 I've defined a person class that's really really similar 44 00:01:53,970 --> 00:01:55,710 to the person class that I defined 45 00:01:55,710 --> 00:01:58,560 in the previous date list lambda demo. 46 00:01:58,560 --> 00:01:59,940 So you can see what's happening here. 47 00:01:59,940 --> 00:02:01,800 I've got my class person. 48 00:02:01,800 --> 00:02:05,220 I've overloaded the insertion operator to be a friend, 49 00:02:05,220 --> 00:02:08,460 just to allow us to display person objects real easily. 50 00:02:08,460 --> 00:02:10,620 A person has a name and an age. 51 00:02:10,620 --> 00:02:12,600 I've got a default constructor. 52 00:02:12,600 --> 00:02:14,220 And you can use this in text, 53 00:02:14,220 --> 00:02:15,960 I didn't really talk about this much in the class, 54 00:02:15,960 --> 00:02:18,877 but, it's just a handy way of telling the compiler 55 00:02:18,877 --> 00:02:22,530 "Hey compiler, generate a default constructor for me." 56 00:02:22,530 --> 00:02:24,000 Sometimes it's real handy, 57 00:02:24,000 --> 00:02:25,890 'cause you can actually be very explicit 58 00:02:25,890 --> 00:02:27,900 to tell whoever's reading your code, 59 00:02:27,900 --> 00:02:29,940 whatever other programmer is reading your code, 60 00:02:29,940 --> 00:02:32,160 that this is exactly what you intended. 61 00:02:32,160 --> 00:02:34,560 So I've got an overloaded constructor here, 62 00:02:34,560 --> 00:02:35,940 a copy constructor here, 63 00:02:35,940 --> 00:02:39,030 and I've done the same thing, a destructor here. 64 00:02:39,030 --> 00:02:42,060 And then I've got my get and my sets and so forth. 65 00:02:42,060 --> 00:02:44,220 Now you can see I've added three methods here. 66 00:02:44,220 --> 00:02:46,650 And let me just expand this, you can see what they're doing. 67 00:02:46,650 --> 00:02:47,483 There we go. 68 00:02:47,483 --> 00:02:51,840 So in this case, I have a method called change person one. 69 00:02:51,840 --> 00:02:53,610 And another one called change person two 70 00:02:53,610 --> 00:02:55,800 and another one called change person three. 71 00:02:55,800 --> 00:02:58,680 They pretty much do exactly the same thing. 72 00:02:58,680 --> 00:03:00,570 Notice that they return auto, 73 00:03:00,570 --> 00:03:02,340 because we're returning a lambda. 74 00:03:02,340 --> 00:03:05,460 And I just want to compiler to figure out the syntax for me. 75 00:03:05,460 --> 00:03:07,260 So in this case, what are we capturing? 76 00:03:07,260 --> 00:03:10,920 We're capturing this, which is the this pointer. 77 00:03:10,920 --> 00:03:12,150 We're capturing by value, 78 00:03:12,150 --> 00:03:13,290 we're capturing by reference, 79 00:03:13,290 --> 00:03:14,970 if you remember from the slides, 80 00:03:14,970 --> 00:03:17,310 these three things are equivalent. 81 00:03:17,310 --> 00:03:19,710 They all capture by reference. 82 00:03:19,710 --> 00:03:23,220 What I would like you to do is always use this version. 83 00:03:23,220 --> 00:03:28,020 The version with the equal sign is deprecated in C++ 20. 84 00:03:28,020 --> 00:03:31,110 That means it's not gonna be supported going forward. 85 00:03:31,110 --> 00:03:34,080 That's a good thing, because when a programmer sees that, 86 00:03:34,080 --> 00:03:36,240 they're thinking capture by value. 87 00:03:36,240 --> 00:03:37,680 And that doesn't happen here, 88 00:03:37,680 --> 00:03:39,570 what's happening is capture by reference. 89 00:03:39,570 --> 00:03:42,480 So it can be confusing and it always has been confusing. 90 00:03:42,480 --> 00:03:43,860 And this one, you know, 91 00:03:43,860 --> 00:03:46,590 why do it when we really wanna capture the this pointer, 92 00:03:46,590 --> 00:03:50,490 so, I would encourage you to use that version only. 93 00:03:50,490 --> 00:03:52,173 So, look what this does. 94 00:03:53,220 --> 00:03:55,260 I'm capturing this. 95 00:03:55,260 --> 00:03:57,330 And the parameters being passed into this 96 00:03:57,330 --> 00:03:59,310 are the new name and the new age, 97 00:03:59,310 --> 00:04:02,100 and all I'm doing is I'm setting name and age 98 00:04:02,100 --> 00:04:04,560 to new name and new age respectively. 99 00:04:04,560 --> 00:04:05,730 Notice that right here, 100 00:04:05,730 --> 00:04:07,923 I don't have to say this name. 101 00:04:09,240 --> 00:04:12,660 Right, or this age, right here. 102 00:04:12,660 --> 00:04:14,580 The compiler will figure that out for us 103 00:04:14,580 --> 00:04:16,200 because of the syntax. 104 00:04:16,200 --> 00:04:18,810 You know, what other name could we be talking about, 105 00:04:18,810 --> 00:04:20,550 it has to be these guys. 106 00:04:20,550 --> 00:04:22,230 So the compiler figures that out. 107 00:04:22,230 --> 00:04:25,800 So what we're gonna do is, when we call these methods, 108 00:04:25,800 --> 00:04:28,560 the lambda will capture the this pointer 109 00:04:28,560 --> 00:04:30,270 and modify those values. 110 00:04:30,270 --> 00:04:31,950 Not a big deal, pretty straightforward, 111 00:04:31,950 --> 00:04:34,590 but I just wanted to show you how you could capture this. 112 00:04:34,590 --> 00:04:36,240 And later on we're gonna do another example 113 00:04:36,240 --> 00:04:37,710 where we capture it another way. 114 00:04:37,710 --> 00:04:40,290 So, let's take a look at the test here. 115 00:04:40,290 --> 00:04:41,400 And I'm only gonna go over this one, 116 00:04:41,400 --> 00:04:43,350 you can see, all three of them, 117 00:04:43,350 --> 00:04:44,760 there's three different examples here, 118 00:04:44,760 --> 00:04:47,040 all three will give you the exact same output. 119 00:04:47,040 --> 00:04:48,450 But in this case, look what we've got, 120 00:04:48,450 --> 00:04:51,277 we've got person, Larry, 18, 121 00:04:51,277 --> 00:04:53,220 so Larry is a person, he's 18 years old. 122 00:04:53,220 --> 00:04:54,630 So now this is what we're going to do, 123 00:04:54,630 --> 00:04:56,730 we're gonna capture using this, 124 00:04:56,730 --> 00:04:59,010 which is that one in person one. 125 00:04:59,010 --> 00:05:02,640 So notice what I'm doing is I'm calling change person one, 126 00:05:02,640 --> 00:05:05,520 in person, a method call, just like before, right? 127 00:05:05,520 --> 00:05:08,253 But this guy is returning a lambda. 128 00:05:09,270 --> 00:05:10,290 That's pretty cool. 129 00:05:10,290 --> 00:05:13,200 So now that I've got my lambda right here, I can call, 130 00:05:13,200 --> 00:05:16,710 I can say change person one and pass in Moe in 30. 131 00:05:16,710 --> 00:05:19,890 Moe is gonna be passed in for the name, and 30 for the age, 132 00:05:19,890 --> 00:05:20,850 and they're gonna change. 133 00:05:20,850 --> 00:05:22,740 So when I display, 134 00:05:22,740 --> 00:05:25,590 you're going to see something else display, right? 135 00:05:25,590 --> 00:05:27,870 You're gonna see, instead of Larry, 136 00:05:27,870 --> 00:05:29,190 you're gonna see Moe display, 137 00:05:29,190 --> 00:05:31,140 let me show you right here, alright, so, 138 00:05:31,140 --> 00:05:33,270 remember this is our first output statement right here, 139 00:05:33,270 --> 00:05:35,670 it just displays Larry, looks like that. 140 00:05:35,670 --> 00:05:37,290 Now we're calling change person one 141 00:05:37,290 --> 00:05:39,960 and then displaying that value. 142 00:05:39,960 --> 00:05:42,090 And in this case, we're displaying Moe. 143 00:05:42,090 --> 00:05:43,050 And you could see here, 144 00:05:43,050 --> 00:05:47,940 where I'm capturing by value and by reference, 145 00:05:47,940 --> 00:05:49,470 it's changing to Curly and to Frank, 146 00:05:49,470 --> 00:05:51,600 because I'm passing in Curly and Frank. 147 00:05:51,600 --> 00:05:53,190 So you can see, that's a good example 148 00:05:53,190 --> 00:05:56,220 that shows how all three of these captures are equivalent. 149 00:05:56,220 --> 00:06:00,720 But as I said, you know, stick to using the this capture, 150 00:06:00,720 --> 00:06:03,030 that's what you're gonna see out there. 151 00:06:03,030 --> 00:06:04,890 I don't think you would ever see this out there, 152 00:06:04,890 --> 00:06:06,240 I've never seen it out there. 153 00:06:06,240 --> 00:06:08,850 Even though it's perfectly legal to use. 154 00:06:08,850 --> 00:06:10,560 And I've seen this a few times, but, 155 00:06:10,560 --> 00:06:13,740 if you're capturing instance variables from an object, 156 00:06:13,740 --> 00:06:17,220 use this, it's really, really clear what your intention is. 157 00:06:17,220 --> 00:06:19,860 Okay, so, that would be our Test Eight. 158 00:06:19,860 --> 00:06:21,540 And let's take a look at this one here. 159 00:06:21,540 --> 00:06:24,540 This is similar, I've got a class called lambda. 160 00:06:24,540 --> 00:06:27,210 And it's got a private instance, variable Y. 161 00:06:27,210 --> 00:06:29,670 You can see my constructor right here. 162 00:06:29,670 --> 00:06:32,220 And there's my operator, does that look familiar? 163 00:06:32,220 --> 00:06:33,480 It should look familiar, right, 164 00:06:33,480 --> 00:06:35,670 because that's exactly like the closure class 165 00:06:35,670 --> 00:06:38,880 that gets created from a lambda expression, remember that? 166 00:06:38,880 --> 00:06:40,710 We've got our function call operator, 167 00:06:40,710 --> 00:06:42,270 we're passing in the X. 168 00:06:42,270 --> 00:06:43,890 This is exactly like the example 169 00:06:43,890 --> 00:06:45,570 that we went over in the slides. 170 00:06:45,570 --> 00:06:47,580 So let's take a look at the equivalence 171 00:06:47,580 --> 00:06:50,010 of a class like this, 172 00:06:50,010 --> 00:06:52,713 versus a lambda expression, they're the same. 173 00:06:53,550 --> 00:06:55,500 And sometimes you'll see these, instead of classes, 174 00:06:55,500 --> 00:06:57,423 you'll see this as a struct. 175 00:06:58,260 --> 00:07:00,150 And everything public, but that's okay, 176 00:07:00,150 --> 00:07:01,920 it works exactly the same way. 177 00:07:01,920 --> 00:07:03,630 Okay, so let's look at Test Nine, 178 00:07:03,630 --> 00:07:05,580 let me scroll up a little bit. 179 00:07:05,580 --> 00:07:06,900 Alright, so here's Test Nine, 180 00:07:06,900 --> 00:07:09,510 I'm displaying the header there. 181 00:07:09,510 --> 00:07:11,850 I've got Y, which is 100. 182 00:07:11,850 --> 00:07:14,520 And I'm creating an instance of lambda one, 183 00:07:14,520 --> 00:07:17,669 and I'm passing in Y, which is 100. 184 00:07:17,669 --> 00:07:19,230 Alright, that is going to create this guy, 185 00:07:19,230 --> 00:07:20,460 it's gonna call this constructor, 186 00:07:20,460 --> 00:07:22,890 it's gonna initialize this to 100. 187 00:07:22,890 --> 00:07:24,270 Just like we would expect. 188 00:07:24,270 --> 00:07:29,270 Now when I call lambda one, I'm passing the 200 here. 189 00:07:29,550 --> 00:07:32,970 And it's gonna add 100 and 200 and display 300. 190 00:07:32,970 --> 00:07:35,673 That's what's happening when we're using this class. 191 00:07:36,990 --> 00:07:41,070 In this case, I've got a lambda variable, lambda two. 192 00:07:41,070 --> 00:07:44,970 Notice what we're doing, we're capturing Y by value. 193 00:07:44,970 --> 00:07:46,590 It's got a parameter X, 194 00:07:46,590 --> 00:07:49,260 and it's displaying the sum of X and Y. 195 00:07:49,260 --> 00:07:53,070 Exactly the same thing, when I call lambda two with 200, 196 00:07:53,070 --> 00:07:55,800 you get exactly the same output. 197 00:07:55,800 --> 00:07:56,640 So you can see 198 00:07:56,640 --> 00:08:01,320 that these two examples are equivalent. 199 00:08:01,320 --> 00:08:03,960 Right, that and that, they're equivalent. 200 00:08:03,960 --> 00:08:07,140 This kind of code is what's being generated 201 00:08:07,140 --> 00:08:09,123 from that lambda expression. 202 00:08:10,470 --> 00:08:12,210 And this is really important to understand, 203 00:08:12,210 --> 00:08:13,980 because it really gives you a good insight 204 00:08:13,980 --> 00:08:16,503 into how these lambdas work behind the scenes. 205 00:08:17,460 --> 00:08:18,990 It's really nice to be able to use them, 206 00:08:18,990 --> 00:08:20,670 but it's even better to be able to understand 207 00:08:20,670 --> 00:08:22,170 how they work behind the scenes. 208 00:08:22,170 --> 00:08:25,473 Alright, so let's do one more, and that would be Test 10. 209 00:08:26,520 --> 00:08:28,950 And for Test 10, I've got a lot going on here, 210 00:08:28,950 --> 00:08:31,830 so let me walk through this really slowly. 211 00:08:31,830 --> 00:08:35,163 In this example, I created a class called people. 212 00:08:36,240 --> 00:08:37,590 And that people class 213 00:08:37,590 --> 00:08:40,080 has a bunch of person objects in it, 214 00:08:40,080 --> 00:08:43,080 right, it's got a stood vector of persons. 215 00:08:43,080 --> 00:08:46,350 So people consists of a bunch of person objects. 216 00:08:46,350 --> 00:08:49,710 And then I've also got this instance variable right here 217 00:08:49,710 --> 00:08:51,540 called max people. 218 00:08:51,540 --> 00:08:53,880 Think of that as the maximum number of people 219 00:08:53,880 --> 00:08:56,220 that you will be able to display. 220 00:08:56,220 --> 00:08:59,640 So if there's 100 person objects in this vector, 221 00:08:59,640 --> 00:09:01,440 and I try to display them, 222 00:09:01,440 --> 00:09:03,420 I should only be allowed to display 10 223 00:09:03,420 --> 00:09:06,480 or five or three or whatever this value is here. 224 00:09:06,480 --> 00:09:09,990 And the constructor just sets this guy to 10 by default, 225 00:09:09,990 --> 00:09:12,930 or gives it whatever value the user supplies. 226 00:09:12,930 --> 00:09:16,140 I've got a copy constructor that I'm just defaulting, 227 00:09:16,140 --> 00:09:18,780 and I think that's about it, I don't need a destructor 228 00:09:18,780 --> 00:09:21,450 because there's no pointers or anything here. 229 00:09:21,450 --> 00:09:23,400 Now I've got an add method, 230 00:09:23,400 --> 00:09:24,750 and what that add method does 231 00:09:24,750 --> 00:09:29,750 is it allows me to add a person to my vector right here. 232 00:09:30,120 --> 00:09:33,240 And I can pass in the person's name and the person's age. 233 00:09:33,240 --> 00:09:35,310 And I can use emplace back, 234 00:09:35,310 --> 00:09:38,400 which creates a person object from these values, 235 00:09:38,400 --> 00:09:41,490 and pushes it back onto the people vector. 236 00:09:41,490 --> 00:09:42,780 If you don't recall emplace back, 237 00:09:42,780 --> 00:09:45,000 you can go back and look at the vectors lecture, 238 00:09:45,000 --> 00:09:46,740 in the STL section. 239 00:09:46,740 --> 00:09:48,150 Now I've got another method right here 240 00:09:48,150 --> 00:09:50,310 called set max people. 241 00:09:50,310 --> 00:09:52,320 And the user passes in a number 242 00:09:52,320 --> 00:09:54,180 and we're setting that instance variable 243 00:09:54,180 --> 00:09:56,630 to whatever they pass in, and the getter as well. 244 00:09:57,570 --> 00:09:58,680 Okay, so. 245 00:09:58,680 --> 00:10:01,290 Now, let's see how we can use a lambda with this. 246 00:10:01,290 --> 00:10:05,640 Okay, so now, I've got a method called get people 247 00:10:05,640 --> 00:10:09,630 and get people returns, a vector of persons. 248 00:10:09,630 --> 00:10:11,490 So basically what we're going to do is, 249 00:10:11,490 --> 00:10:14,580 we're going to return all the person objects 250 00:10:14,580 --> 00:10:19,580 in that people class, whose age is greater than max age. 251 00:10:20,160 --> 00:10:23,490 And I'm only gonna return less than max people, 252 00:10:23,490 --> 00:10:25,233 I'm not gonna go over that value. 253 00:10:26,250 --> 00:10:27,600 That's really important. 254 00:10:27,600 --> 00:10:29,070 Now the interesting thing here is, 255 00:10:29,070 --> 00:10:32,100 when the user calls get people, 256 00:10:32,100 --> 00:10:36,060 the user really has no idea about max people. 257 00:10:36,060 --> 00:10:37,890 They're just sending in an age. 258 00:10:37,890 --> 00:10:41,133 So I might have 100 people in this people vector, 259 00:10:42,060 --> 00:10:44,760 that are over 50, I'm only gonna display 10 of them, 260 00:10:44,760 --> 00:10:46,350 because of that value. 261 00:10:46,350 --> 00:10:47,610 Let's see how this works. 262 00:10:47,610 --> 00:10:49,980 First thing I'm doing is I'm creating 263 00:10:49,980 --> 00:10:53,310 a local stood vector of persons called result, 264 00:10:53,310 --> 00:10:55,080 it's empty right now. 265 00:10:55,080 --> 00:10:56,130 And I've got account. 266 00:10:57,090 --> 00:10:57,960 Now I'm gonna use 267 00:10:57,960 --> 00:11:01,323 the standard template library algorithm copy if. 268 00:11:02,250 --> 00:11:06,990 And I'm gonna copy matches to this result 269 00:11:06,990 --> 00:11:09,780 as long as they match the criteria that I mentioned. 270 00:11:09,780 --> 00:11:12,690 So right here, I'm gonna say stood copy if. 271 00:11:12,690 --> 00:11:14,610 What are we copying from? 272 00:11:14,610 --> 00:11:16,050 People bot begin, 273 00:11:16,050 --> 00:11:18,180 where are we copying up to, people dot N, 274 00:11:18,180 --> 00:11:20,253 so I'm using the entire people Vector. 275 00:11:21,090 --> 00:11:23,730 And what I'm going to do is, when I've got a match, 276 00:11:23,730 --> 00:11:26,130 I'm going to use stood back inserter. 277 00:11:26,130 --> 00:11:28,380 Which basically pushes back 278 00:11:28,380 --> 00:11:31,800 whatever I'm getting into that result, 279 00:11:31,800 --> 00:11:34,050 that vector that I'm building right here. 280 00:11:34,050 --> 00:11:36,960 And obviously what I'm getting is persons, right? 281 00:11:36,960 --> 00:11:38,310 So now it comes to lambda, 282 00:11:38,310 --> 00:11:41,220 when do we actually do this copy if, right? 283 00:11:41,220 --> 00:11:42,053 There's an if in there, 284 00:11:42,053 --> 00:11:43,710 so it has to match something. 285 00:11:43,710 --> 00:11:44,880 And let me expand this out 286 00:11:44,880 --> 00:11:46,830 so you can see all the code right here. 287 00:11:46,830 --> 00:11:48,930 So look at what we're doing here. 288 00:11:48,930 --> 00:11:51,870 First of all, we're capturing this. 289 00:11:51,870 --> 00:11:55,230 That allows me access to that guy 290 00:11:55,230 --> 00:11:58,350 which is an instance variable in this class. 291 00:11:58,350 --> 00:12:02,310 As well as the vector I'm capturing count by reference, 292 00:12:02,310 --> 00:12:05,210 that's this guy right here, because I need to modify that. 293 00:12:06,510 --> 00:12:11,510 and I'm expecting a person reference that's constant. 294 00:12:12,240 --> 00:12:15,240 And that's what copy if is gonna give me. 295 00:12:15,240 --> 00:12:16,410 Right, now what's my predicate, 296 00:12:16,410 --> 00:12:20,580 well my predicate basically says if P's age, 297 00:12:20,580 --> 00:12:22,860 that's the object that's being passed into me, 298 00:12:22,860 --> 00:12:25,500 is greater than this max age. 299 00:12:25,500 --> 00:12:27,300 And I'm going to pre-increment count 300 00:12:27,300 --> 00:12:30,600 and make sure that it's less than equal to max people. 301 00:12:30,600 --> 00:12:32,100 That's it, that's pretty cool. 302 00:12:32,100 --> 00:12:33,540 So you can see what we're using here, 303 00:12:33,540 --> 00:12:37,440 we're using that lambda to gain access to the object. 304 00:12:37,440 --> 00:12:39,300 And once we have access to the object, 305 00:12:39,300 --> 00:12:41,760 we can use all kinds of good stuff. 306 00:12:41,760 --> 00:12:43,110 So at this point, we need that, 307 00:12:43,110 --> 00:12:45,930 we need that access to the object to get to max people. 308 00:12:45,930 --> 00:12:48,300 Okay, so now, we're good. 309 00:12:48,300 --> 00:12:50,070 At this point we're gonna return result, 310 00:12:50,070 --> 00:12:52,350 and it's gonna contain as many people 311 00:12:52,350 --> 00:12:55,800 that match that criteria as I'm allowed to print. 312 00:12:55,800 --> 00:12:57,900 Now the idea here is, sure, 313 00:12:57,900 --> 00:12:59,340 this function could have accepted 314 00:12:59,340 --> 00:13:01,080 the max number of people to print. 315 00:13:01,080 --> 00:13:03,270 But suppose there are two entities here, right, 316 00:13:03,270 --> 00:13:05,280 one that is setting that policy, 317 00:13:05,280 --> 00:13:07,050 and this one that's using this, 318 00:13:07,050 --> 00:13:10,260 and this function really shouldn't know about the other. 319 00:13:10,260 --> 00:13:12,150 Alright, so let's run through a test here, 320 00:13:12,150 --> 00:13:13,500 see how all this works. 321 00:13:13,500 --> 00:13:17,010 So here we are in test 10, and I've got people, friends, 322 00:13:17,010 --> 00:13:20,430 and then I'm gonna add Larry, Curly, Moe, Frank, and James. 323 00:13:20,430 --> 00:13:22,473 Now I'm going to call get people. 324 00:13:23,370 --> 00:13:27,960 Now remember, when I called this guy, the max was 10. 325 00:13:27,960 --> 00:13:29,820 So the most that I could display was 10, 326 00:13:29,820 --> 00:13:32,850 that was the default parameter in my constructor. 327 00:13:32,850 --> 00:13:35,070 When this function returns, 328 00:13:35,070 --> 00:13:37,860 I'm gonna have a stood vector of person objects, 329 00:13:37,860 --> 00:13:39,270 that's what this is, 330 00:13:39,270 --> 00:13:42,930 that contains all the people whose age is 17 or over. 331 00:13:42,930 --> 00:13:44,370 Now this is basically everybody, 332 00:13:44,370 --> 00:13:46,950 you can see everybody here is 17 or older, right? 333 00:13:46,950 --> 00:13:50,190 So once I get that, I'm just gonna loop through that result, 334 00:13:50,190 --> 00:13:51,150 and display them all, 335 00:13:51,150 --> 00:13:53,600 and you can see right here, I'm getting them all. 336 00:13:55,110 --> 00:13:58,590 Now I'm gonna set the maximum number of people to three. 337 00:13:58,590 --> 00:14:01,290 And I'm gonna call that function exactly the same way, 338 00:14:01,290 --> 00:14:04,020 friends dot get people 17. 339 00:14:04,020 --> 00:14:06,330 I'm still gonna get five matches, right? 340 00:14:06,330 --> 00:14:10,413 But I'm only gonna get three back, because the max is three. 341 00:14:11,310 --> 00:14:13,290 Now when I come over here and display this, 342 00:14:13,290 --> 00:14:16,980 I'm gonna see Larry Curly and Moe only. 343 00:14:16,980 --> 00:14:20,610 So now, let's take a look at this last example here. 344 00:14:20,610 --> 00:14:23,193 Remember, we've still got three as the max. 345 00:14:24,390 --> 00:14:25,890 Not that it matters in this case. 346 00:14:25,890 --> 00:14:28,320 But in this case, I wanna get people over 50. 347 00:14:28,320 --> 00:14:31,110 Well the only person over 50 is James. 348 00:14:31,110 --> 00:14:33,600 So all we get back is James. 349 00:14:33,600 --> 00:14:36,660 Had there been 40 people that are over 50, 350 00:14:36,660 --> 00:14:38,340 I only would've gotten three of them back 351 00:14:38,340 --> 00:14:39,810 and displayed only three. 352 00:14:39,810 --> 00:14:40,920 This can be really handy, 353 00:14:40,920 --> 00:14:43,860 sometimes you've got a game going on and you've got players, 354 00:14:43,860 --> 00:14:45,210 you've got a whole bunch of players, 355 00:14:45,210 --> 00:14:47,070 but only a certain number of players 356 00:14:47,070 --> 00:14:48,600 meet a certain criteria. 357 00:14:48,600 --> 00:14:50,070 And there may be so many of them 358 00:14:50,070 --> 00:14:52,110 that you just wanna randomly pick one two or three 359 00:14:52,110 --> 00:14:55,170 or give you the first three back, or the first 10 back, 360 00:14:55,170 --> 00:14:57,810 depending on what logic is running in the program. 361 00:14:57,810 --> 00:14:59,700 Okay, so, that's it, 362 00:14:59,700 --> 00:15:03,000 those are a bunch of examples for stateful lambdas. 363 00:15:03,000 --> 00:15:04,530 Hopefully this stuff makes sense, 364 00:15:04,530 --> 00:15:06,060 there's a lot of other examples 365 00:15:06,060 --> 00:15:09,480 that obviously you can play around with and create yourself. 366 00:15:09,480 --> 00:15:11,910 In the next video we'll wrap up this section 367 00:15:11,910 --> 00:15:15,330 and we'll go over some examples of using lambdas 368 00:15:15,330 --> 00:15:19,020 with the standard template library algorithms, 369 00:15:19,020 --> 00:15:20,490 some simple use cases, 370 00:15:20,490 --> 00:15:23,880 and we'll just kind of pick a bunch of example algorithms 371 00:15:23,880 --> 00:15:26,130 and try them out and show you how that works.