1 00:00:00,067 --> 00:00:02,650 (bright music) 2 00:00:05,310 --> 00:00:06,210 Presenter: Now that we've seen how 3 00:00:06,210 --> 00:00:07,620 to implement both stateless 4 00:00:07,620 --> 00:00:09,720 and stateful Lambdas, let's take a look 5 00:00:09,720 --> 00:00:12,000 at more ways to use them. 6 00:00:12,000 --> 00:00:13,770 The most predominant use of Lambdas 7 00:00:13,770 --> 00:00:15,780 is in conjunction with the standard template 8 00:00:15,780 --> 00:00:17,190 algorithms library 9 00:00:17,190 --> 00:00:19,530 as either a predicate or operator. 10 00:00:19,530 --> 00:00:21,510 Remember, a predicate is just a function 11 00:00:21,510 --> 00:00:24,540 that takes several arguments and returns a Boolean. 12 00:00:24,540 --> 00:00:27,270 Alternatively, an operator is a function that 13 00:00:27,270 --> 00:00:28,380 takes several arguments 14 00:00:28,380 --> 00:00:30,393 and applies some operation to them. 15 00:00:31,710 --> 00:00:34,380 When using Lambdas with the STL Algorithms library 16 00:00:34,380 --> 00:00:35,550 you'll often hear them referred 17 00:00:35,550 --> 00:00:37,950 to as either unary binary predicates 18 00:00:37,950 --> 00:00:40,410 or unary binary operations. 19 00:00:40,410 --> 00:00:42,660 This simply refers to whether the Lambda will act 20 00:00:42,660 --> 00:00:44,580 as a predicate or as an operator 21 00:00:44,580 --> 00:00:46,800 and how many parameters the Lambda requires. 22 00:00:46,800 --> 00:00:49,830 Unary meaning one and binary meaning two. 23 00:00:49,830 --> 00:00:50,820 We've already seen lots 24 00:00:50,820 --> 00:00:53,790 of examples that use Lambdas with the STL algorithms. 25 00:00:53,790 --> 00:00:55,680 We've seen examples in this section 26 00:00:55,680 --> 00:00:57,030 and we also saw examples 27 00:00:57,030 --> 00:00:59,670 in the STL section of the course. 28 00:00:59,670 --> 00:01:01,080 Now that we've got the jargon down 29 00:01:01,080 --> 00:01:02,940 let's take a look at a few more examples 30 00:01:02,940 --> 00:01:06,033 of how to use Lambdas with a few more STL algorithms. 31 00:01:11,730 --> 00:01:14,460 Okay, so then let's go through some examples, in the IDE. 32 00:01:14,460 --> 00:01:15,293 You can see here 33 00:01:15,293 --> 00:01:17,970 that I'm in the section 21 workspace 34 00:01:17,970 --> 00:01:21,750 and I'm in the STL underscore Lambdas project. 35 00:01:21,750 --> 00:01:23,250 I've already gone ahead and built and run it. 36 00:01:23,250 --> 00:01:25,710 You can see the run out here on the right side. 37 00:01:25,710 --> 00:01:28,918 And let's go over the Includes IO stream vector algorithm 38 00:01:28,918 --> 00:01:31,170 and I'm also including numeric. 39 00:01:31,170 --> 00:01:33,125 This is for a function called iota, which is 40 00:01:33,125 --> 00:01:35,640 a pretty handy little function that I haven't covered. 41 00:01:35,640 --> 00:01:36,540 So I thought I would throw 42 00:01:36,540 --> 00:01:37,920 it in just so you could learn it. 43 00:01:37,920 --> 00:01:39,810 The algorithms that I'm gonna be using in here 44 00:01:39,810 --> 00:01:41,212 are pretty straightforward ones. 45 00:01:41,212 --> 00:01:42,630 There are a lot more of them, 46 00:01:42,630 --> 00:01:47,310 as I said, please see your CPP reference.com website. 47 00:01:47,310 --> 00:01:50,430 It's got documentation on all the STL algorithms. 48 00:01:50,430 --> 00:01:52,470 Again, this video is not about the algorithms 49 00:01:52,470 --> 00:01:54,150 it's about using Lambdas with them. 50 00:01:54,150 --> 00:01:55,320 Let's go over some examples. 51 00:01:55,320 --> 00:01:57,780 I've got I think seven tests that we'll run 52 00:01:57,780 --> 00:02:00,060 and I'll walk through them just like I have before. 53 00:02:00,060 --> 00:02:02,010 The first one we've already seen before. 54 00:02:02,010 --> 00:02:03,030 It's for each. 55 00:02:03,030 --> 00:02:04,980 Now I've included this as well as sort 56 00:02:04,980 --> 00:02:07,860 in this video in case somebody jumps straight 57 00:02:07,860 --> 00:02:09,509 into this video and they haven't seen them before. 58 00:02:09,509 --> 00:02:10,342 They can see them here 59 00:02:10,342 --> 00:02:11,176 for the first time 60 00:02:11,176 --> 00:02:12,210 and I'll walk through them again. 61 00:02:12,210 --> 00:02:14,070 Okay, so let's start with test one. 62 00:02:14,070 --> 00:02:15,810 You can see test one's output 63 00:02:15,810 --> 00:02:17,070 is right up here. 64 00:02:17,070 --> 00:02:18,420 In test one, what we've got 65 00:02:18,420 --> 00:02:20,640 is we're going over the, for each algorithm. 66 00:02:20,640 --> 00:02:22,740 Now I've already covered the, for each algorithm a couple 67 00:02:22,740 --> 00:02:25,380 of times in this section as well as in the course. 68 00:02:25,380 --> 00:02:27,540 It's a very handy algorithm that lets you iterate 69 00:02:27,540 --> 00:02:29,910 over a container in the standard template library. 70 00:02:29,910 --> 00:02:31,500 So it's non modifying 71 00:02:31,500 --> 00:02:33,390 so we're not gonna modify anything in the container. 72 00:02:33,390 --> 00:02:36,450 It just basically processes each element, if you will. 73 00:02:36,450 --> 00:02:38,700 In this case, I've got nums 74 00:02:38,700 --> 00:02:41,280 which is a vector of five integers 75 00:02:41,280 --> 00:02:44,220 and I've initialized it to 10, 20, 30, 40, 50. 76 00:02:44,220 --> 00:02:46,410 And then what we're gonna do is we're gonna call for each 77 00:02:46,410 --> 00:02:48,300 from the beginning to the end. 78 00:02:48,300 --> 00:02:50,340 So we're gonna cover the entire container. 79 00:02:50,340 --> 00:02:52,290 We don't have to, we can only loop, you know 80 00:02:52,290 --> 00:02:54,870 once or twice or three or four elements, whatever we want. 81 00:02:54,870 --> 00:02:58,230 But in this case, I'm iterating over the entire vector 82 00:02:58,230 --> 00:02:59,070 and what's gonna happen 83 00:02:59,070 --> 00:03:02,010 is for each element in that vector 84 00:03:02,010 --> 00:03:04,143 it will be passed into this Lambda. 85 00:03:05,038 --> 00:03:06,300 And we don't, we're not doing that right. 86 00:03:06,300 --> 00:03:07,950 The for each algorithm is doing that. 87 00:03:07,950 --> 00:03:11,130 And then we just use this Lambda as an operator here 88 00:03:11,130 --> 00:03:12,810 not a predicate, it's just a regular operator. 89 00:03:12,810 --> 00:03:14,130 We just do some stuff. 90 00:03:14,130 --> 00:03:16,320 So in this case we're just displaying. 91 00:03:16,320 --> 00:03:18,210 So it's gonna iterate, pass those guys in. 92 00:03:18,210 --> 00:03:19,710 Notice there's no capture. 93 00:03:19,710 --> 00:03:21,886 We only have that one parameter which we need 94 00:03:21,886 --> 00:03:24,360 because for each is giving it to us 95 00:03:24,360 --> 00:03:26,773 and it's just going to display 10, 20, 30, 40, 50. 96 00:03:26,773 --> 00:03:27,990 So this is a really, 97 00:03:27,990 --> 00:03:30,540 really simple example of using a Lambda 98 00:03:30,540 --> 00:03:32,100 with one of the algorithms 99 00:03:32,100 --> 00:03:33,960 in the standard template library. 100 00:03:33,960 --> 00:03:35,580 A very handy algorithm as well. 101 00:03:35,580 --> 00:03:38,730 I'm using it here, but this can be a container of anything. 102 00:03:38,730 --> 00:03:39,870 Okay, so let's take a look 103 00:03:39,870 --> 00:03:40,980 at a different one 104 00:03:40,980 --> 00:03:43,170 that we probably haven't seen before. 105 00:03:43,170 --> 00:03:45,633 This one is called is permutation. 106 00:03:47,220 --> 00:03:48,840 This one is called is permutation 107 00:03:48,840 --> 00:03:50,700 and it's also non modifying. 108 00:03:50,700 --> 00:03:52,680 In this case I'm using triangles. 109 00:03:52,680 --> 00:03:55,560 So it tests whether two triangles are equivalent 110 00:03:55,560 --> 00:03:57,750 and we defining a triangle being equivalent 111 00:03:57,750 --> 00:04:01,470 as a triangle as a vector of three points in any order. 112 00:04:01,470 --> 00:04:03,210 So regardless of the order, if two triangles 113 00:04:03,210 --> 00:04:05,760 contain those same three points, they're equivalent. 114 00:04:05,760 --> 00:04:07,530 So pretty simple definition. 115 00:04:07,530 --> 00:04:09,210 So before we use the algorithm 116 00:04:09,210 --> 00:04:10,620 let's take a look at what we've done. 117 00:04:10,620 --> 00:04:12,510 Let me scroll up just a little bit here. 118 00:04:12,510 --> 00:04:13,620 So here's test two 119 00:04:13,620 --> 00:04:16,413 and you can see test two's output right over here. 120 00:04:17,370 --> 00:04:19,649 And I've got a structure that I've defined called point 121 00:04:19,649 --> 00:04:22,380 which is just an x, y attribute. 122 00:04:22,380 --> 00:04:24,330 And then I'm gonna create four points, point one, 123 00:04:24,330 --> 00:04:26,580 point two, point three, point four. 124 00:04:26,580 --> 00:04:29,130 And based on those points I'm gonna create a triangle. 125 00:04:29,130 --> 00:04:33,330 So a triangle is a vector of points, three points, 126 00:04:33,330 --> 00:04:35,160 triangle one is point one, point two, 127 00:04:35,160 --> 00:04:37,603 point three, triangle two is point two, 128 00:04:37,603 --> 00:04:39,420 point three, point one. 129 00:04:39,420 --> 00:04:42,270 And then triangle four includes that point four here. 130 00:04:42,270 --> 00:04:46,050 So according to our definition, these two are equivalent 131 00:04:46,050 --> 00:04:48,390 because we can change the order, right? 132 00:04:48,390 --> 00:04:49,320 I think everybody understands 133 00:04:49,320 --> 00:04:50,610 what a permutation is, 134 00:04:50,610 --> 00:04:52,110 we're just kind of mixing these things 135 00:04:52,110 --> 00:04:53,280 up in different orders. 136 00:04:53,280 --> 00:04:56,130 Eventually we can permute one of these and get one of those. 137 00:04:56,130 --> 00:04:58,710 'Cause we've got one, two, three in both of them. 138 00:04:58,710 --> 00:05:01,020 However, triangle three is not equivalent 139 00:05:01,020 --> 00:05:03,210 because that point four doesn't exist 140 00:05:03,210 --> 00:05:05,130 in either triangle one or triangle two. 141 00:05:05,130 --> 00:05:07,110 And again, the important thing is the Lambda here 142 00:05:07,110 --> 00:05:10,710 so I'm going to call stood is permutation. 143 00:05:10,710 --> 00:05:12,960 I'm going to pass in triangle one begin, 144 00:05:12,960 --> 00:05:14,757 triangle one end that's going 145 00:05:14,757 --> 00:05:17,580 from the beginning to the end of this vector. 146 00:05:17,580 --> 00:05:20,400 And then I'm passing in triangle two begin. 147 00:05:20,400 --> 00:05:22,560 Let me spread this out just a little so you can see it. 148 00:05:22,560 --> 00:05:25,110 So we are also expecting two points. 149 00:05:25,110 --> 00:05:27,600 Now the stood is permutation function. 150 00:05:27,600 --> 00:05:28,860 That's what it's doing. 151 00:05:28,860 --> 00:05:31,110 It's passing those two points into us 152 00:05:31,110 --> 00:05:34,020 and it's up to us to compare them however we like. 153 00:05:34,020 --> 00:05:35,340 So we're creating the left hand side 154 00:05:35,340 --> 00:05:36,173 and the right hand side 155 00:05:36,173 --> 00:05:39,270 and it's using the operator equal to compare. 156 00:05:39,270 --> 00:05:41,040 So what's gonna happen is I'm gonna compare my 157 00:05:41,040 --> 00:05:44,010 left hand side's x to my right hand side's x 158 00:05:44,010 --> 00:05:46,950 and the left hand side's y to the right hand side y. 159 00:05:46,950 --> 00:05:48,600 So our points are equivalent. 160 00:05:48,600 --> 00:05:51,180 So when we call is permutation 161 00:05:51,180 --> 00:05:54,330 and we get a permutation that matches one of those 162 00:05:54,330 --> 00:05:57,210 we've got an equivalence and that's pretty much it. 163 00:05:57,210 --> 00:05:58,822 So we're using that in the if statement 164 00:05:58,822 --> 00:06:00,360 and if they're equivalent, 165 00:06:00,360 --> 00:06:01,500 we're just displaying that. 166 00:06:01,500 --> 00:06:02,460 If they're not equivalent, 167 00:06:02,460 --> 00:06:03,293 we're displaying that. 168 00:06:03,293 --> 00:06:05,670 And you can see from this test case right here 169 00:06:05,670 --> 00:06:08,610 that triangle one and triangle two are equivalent. 170 00:06:08,610 --> 00:06:11,250 And that's exactly what's being displayed right over here. 171 00:06:11,250 --> 00:06:12,180 Let me move this back 172 00:06:12,180 --> 00:06:15,120 'cause I think everybody sees what's going on now. 173 00:06:15,120 --> 00:06:16,950 In the other example down here 174 00:06:16,950 --> 00:06:20,040 I'm using triangle one and triangle three. 175 00:06:20,040 --> 00:06:21,150 Now remember triangle three 176 00:06:21,150 --> 00:06:22,710 has that fourth point in there 177 00:06:22,710 --> 00:06:24,480 so it's not going to be equivalent. 178 00:06:24,480 --> 00:06:26,460 And that's exactly what happens right here. 179 00:06:26,460 --> 00:06:27,360 We've got triangle one, 180 00:06:27,360 --> 00:06:28,800 triangle three are not equivalent. 181 00:06:28,800 --> 00:06:32,430 So again, just another example of passing in a Lambda here 182 00:06:32,430 --> 00:06:36,660 this Lambda does not capture and it expects two parameters. 183 00:06:36,660 --> 00:06:38,910 In this case, this is a predicate Lambda 184 00:06:38,910 --> 00:06:41,010 because it's returning a true false value. 185 00:06:41,010 --> 00:06:43,620 Alright, so let's move on to test three. 186 00:06:43,620 --> 00:06:46,389 In test three, I'm using the transform algorithm. 187 00:06:46,389 --> 00:06:48,572 This is a modifying operation 188 00:06:48,572 --> 00:06:51,450 and if you recall in the previous examples, we created 189 00:06:51,450 --> 00:06:54,510 that Lambda bonus where we awarded bonus points 190 00:06:54,510 --> 00:06:56,220 to test scores and so forth. 191 00:06:56,220 --> 00:06:57,330 Well, we're gonna do the same thing 192 00:06:57,330 --> 00:06:59,100 except we're gonna use to transform. 193 00:06:59,100 --> 00:07:00,360 So let's do it. 194 00:07:00,360 --> 00:07:04,650 We've got test scores and it is a vector of integers. 195 00:07:04,650 --> 00:07:08,790 And those are my test scores, 93, 88, 75, 68, 65. 196 00:07:08,790 --> 00:07:11,670 And these are the bonus points that I want to add 197 00:07:11,670 --> 00:07:13,710 to each one of those test scores. 198 00:07:13,710 --> 00:07:16,200 Right away you know we've got a local variable here. 199 00:07:16,200 --> 00:07:17,550 If we're gonna use that in the Lambda 200 00:07:17,550 --> 00:07:19,380 we need to capture it. 201 00:07:19,380 --> 00:07:21,780 So that's what we're going to do in this example. 202 00:07:21,780 --> 00:07:26,220 We are going to transform that container test 203 00:07:26,220 --> 00:07:28,320 scores begin test scores dot end. 204 00:07:28,320 --> 00:07:30,330 So we're doing the entire container 205 00:07:30,330 --> 00:07:31,380 and we're gonna start writing 206 00:07:31,380 --> 00:07:34,890 back the information at test scores dot begin. 207 00:07:34,890 --> 00:07:36,690 So we're gonna start from here 208 00:07:36,690 --> 00:07:38,910 and we're gonna start updating from here as well. 209 00:07:38,910 --> 00:07:41,490 We're capturing bonus points by value. 210 00:07:41,490 --> 00:07:43,260 That's this guy right here. 211 00:07:43,260 --> 00:07:45,180 So we're gonna capture those bonus points 212 00:07:45,180 --> 00:07:47,130 by value and then it stood 213 00:07:47,130 --> 00:07:50,160 transform is going to pass into the Lambda 214 00:07:50,160 --> 00:07:53,250 each one of those scores as it iterates over it. 215 00:07:53,250 --> 00:07:56,200 So those scores will be passed into here, 93 216 00:07:56,200 --> 00:07:58,140 then 88 and 75 and so forth. 217 00:07:58,140 --> 00:08:01,350 So what we're doing now is we're returning score 218 00:08:01,350 --> 00:08:04,290 plus equals five in this case. 219 00:08:04,290 --> 00:08:07,140 So what we're going to do is we're gonna increment each one 220 00:08:07,140 --> 00:08:09,660 of those test scores by five points. 221 00:08:09,660 --> 00:08:11,910 So let's take a look at the output right over here 222 00:08:11,910 --> 00:08:14,670 right here you can see I'm in test three 223 00:08:14,670 --> 00:08:18,540 and you can see right there that we've gone from 93 to 98 224 00:08:18,540 --> 00:08:21,630 from 88 to 93 and so forth. 225 00:08:21,630 --> 00:08:24,960 So we've added five points to each one of those scores. 226 00:08:24,960 --> 00:08:27,450 I just used a range based for loop here. 227 00:08:27,450 --> 00:08:29,550 I could have used the for each algorithm as well. 228 00:08:29,550 --> 00:08:32,340 I'll leave that up to you guys to decide. 229 00:08:32,340 --> 00:08:34,681 Okay, so let's take a look at test four. 230 00:08:34,681 --> 00:08:37,530 I've got a vector of integer called nums 231 00:08:37,530 --> 00:08:39,000 and you can see I've initialized it here 232 00:08:39,000 --> 00:08:40,650 to one, two, three, four, five. 233 00:08:40,650 --> 00:08:42,845 Now what we're going to do is we're gonna remove 234 00:08:42,845 --> 00:08:45,780 the even numbers from that vector. 235 00:08:45,780 --> 00:08:49,080 Then once we remove them, we're going to erase them. 236 00:08:49,080 --> 00:08:53,220 So what we're doing here is first we're calling remove if 237 00:08:53,220 --> 00:08:55,650 and we're telling remove if that we want to want to iterate 238 00:08:55,650 --> 00:08:59,580 over the entire container numbs from beginning to end. 239 00:08:59,580 --> 00:09:02,850 And what remove if will do is it will pass in each one 240 00:09:02,850 --> 00:09:06,870 of those values from that container into this Lambda. 241 00:09:06,870 --> 00:09:08,370 This is a predicate Lambda 242 00:09:08,370 --> 00:09:11,100 and we're returning true for whatever we want to delete. 243 00:09:11,100 --> 00:09:14,580 So in this case, we're returning true for all even numbers. 244 00:09:14,580 --> 00:09:19,290 So two and four are what we want to eventually erase 245 00:09:19,290 --> 00:09:20,850 which right now we're just removing. 246 00:09:20,850 --> 00:09:22,419 What happens is it puts those numbers 247 00:09:22,419 --> 00:09:25,860 at the end and sets the end to it. 248 00:09:25,860 --> 00:09:28,860 So now we're telling nums dot erase 249 00:09:28,860 --> 00:09:30,540 to erase what it returned 250 00:09:30,540 --> 00:09:32,630 and use nums dot end as the new end. 251 00:09:32,630 --> 00:09:35,190 So it's basically they're gonna chop off those numbers. 252 00:09:35,190 --> 00:09:37,530 And then when we display our updated numbers 253 00:09:37,530 --> 00:09:40,830 you can see right there one, three and five. 254 00:09:40,830 --> 00:09:43,650 So again, another example of using a predicate Lambda 255 00:09:43,650 --> 00:09:46,200 this is using the erase remove idiom 256 00:09:46,200 --> 00:09:49,003 which is a very common idiom in the STL. 257 00:09:49,003 --> 00:09:50,670 Okay, so let's move on to test five. 258 00:09:50,670 --> 00:09:53,730 In test five, I'm using that same person class 259 00:09:53,730 --> 00:09:56,100 that I've used throughout this section. 260 00:09:56,100 --> 00:09:57,150 So it's right here. 261 00:09:57,150 --> 00:09:59,130 You can see it's that person class 262 00:09:59,130 --> 00:10:01,740 with our overloaded insertion operator. 263 00:10:01,740 --> 00:10:04,530 We've got our name, our age, and a bunch of constructors 264 00:10:04,530 --> 00:10:05,490 and getters and setters. 265 00:10:05,490 --> 00:10:07,650 So you can refer to the previous videos where 266 00:10:07,650 --> 00:10:09,840 I explained this in a little bit more detail. 267 00:10:09,840 --> 00:10:11,580 So what we're gonna do now is we're gonna sort it. 268 00:10:11,580 --> 00:10:13,770 Now you've seen this before in the previous video. 269 00:10:13,770 --> 00:10:16,440 I just wanted to include it again just to be complete 270 00:10:16,440 --> 00:10:19,920 because somebody may jump into this video and not 271 00:10:19,920 --> 00:10:21,270 have any idea how sort works. 272 00:10:21,270 --> 00:10:22,103 So now you know. 273 00:10:22,103 --> 00:10:24,000 So in this case, really easy. 274 00:10:24,000 --> 00:10:27,750 I've created three person objects, person one, person two 275 00:10:27,750 --> 00:10:28,680 and person three. 276 00:10:28,680 --> 00:10:32,040 You can see them right here, Larry Moe, Curly. 277 00:10:32,040 --> 00:10:34,609 Then I've created a person vector and I've just 278 00:10:34,609 --> 00:10:36,930 created it using person one, two and three. 279 00:10:36,930 --> 00:10:40,200 I've initialized it to those person objects. 280 00:10:40,200 --> 00:10:41,370 This is just another way to do it. 281 00:10:41,370 --> 00:10:44,320 I believe I've done it in the other videos just using move. 282 00:10:50,905 --> 00:10:52,650 And then we're going to call std sort 283 00:10:52,650 --> 00:10:53,850 and we're gonna tell std sort 284 00:10:53,850 --> 00:10:55,920 that we want to sort that people vector 285 00:10:55,920 --> 00:11:00,203 from begin to end and I want it to pass me pairs 286 00:11:00,203 --> 00:11:03,480 of persons where that I need to compare. 287 00:11:03,480 --> 00:11:07,170 So it's going to send to me two person objects 288 00:11:07,170 --> 00:11:08,820 one on the left, one on the right 289 00:11:08,820 --> 00:11:12,360 and we're using the less than operator to compare those two 290 00:11:12,360 --> 00:11:13,860 and we're gonna compare by name. 291 00:11:13,860 --> 00:11:16,440 So in this case you can see we're just comparing 292 00:11:16,440 --> 00:11:20,310 by name and we're sorting in increasing alphabetical order 293 00:11:20,310 --> 00:11:21,480 and then we're displaying these. 294 00:11:21,480 --> 00:11:23,580 So what's gonna happen is right here 295 00:11:23,580 --> 00:11:25,770 Curly, Larry and Moe, clm , right? 296 00:11:25,770 --> 00:11:28,170 So it's in alphabetical order. 297 00:11:28,170 --> 00:11:31,920 So in this case, if we wanted to display the people 298 00:11:31,920 --> 00:11:35,700 in that vector by age and in descending order 299 00:11:35,700 --> 00:11:37,560 then we would just have to say left hand side 300 00:11:37,560 --> 00:11:39,330 get age greater than right hand side 301 00:11:39,330 --> 00:11:42,900 get age and it will display Moe, Curly 302 00:11:42,900 --> 00:11:47,460 and Larry in that order 30, 25, 18 and descending age. 303 00:11:47,460 --> 00:11:48,630 Really powerful stuff. 304 00:11:48,630 --> 00:11:51,060 All we're doing is changing that Lambda, 305 00:11:51,060 --> 00:11:53,955 changing that predicate Lambda to do what we need. 306 00:11:53,955 --> 00:11:56,310 It's our functional requirements. 307 00:11:56,310 --> 00:11:58,018 If we have to sort by name ascending 308 00:11:58,018 --> 00:12:01,413 or by age descending, it's really, really easy to do. 309 00:12:02,880 --> 00:12:04,470 Alright, so let's move on 310 00:12:04,470 --> 00:12:06,330 to another one that's a little bit different. 311 00:12:06,330 --> 00:12:08,580 In this case, I've written a function 312 00:12:08,580 --> 00:12:11,160 and that function is called in between. 313 00:12:11,160 --> 00:12:13,920 That function expects a vector 314 00:12:13,920 --> 00:12:16,980 of numbers, integer in this case, and I'm passing 315 00:12:16,980 --> 00:12:19,470 that in by const ref, a start value 316 00:12:19,470 --> 00:12:22,410 and an end value is going to decide 317 00:12:22,410 --> 00:12:25,470 whether all the numbers in that vector are 318 00:12:25,470 --> 00:12:29,133 between that start value and that end value inclusive. 319 00:12:30,210 --> 00:12:33,330 I'm going to use the stood all of algorithm. 320 00:12:33,330 --> 00:12:36,120 It's a non modifying algorithm and what it does is it tests 321 00:12:36,120 --> 00:12:37,680 whether all the elements contained 322 00:12:37,680 --> 00:12:41,910 within the sequence satisfy the condition in the Lambda. 323 00:12:41,910 --> 00:12:44,130 So in this case I'm assuming that they don't. 324 00:12:44,130 --> 00:12:46,380 So I'm just initializing this result bullying 325 00:12:46,380 --> 00:12:51,380 to faults and then I'm saying result equals stood all 326 00:12:51,600 --> 00:12:53,880 of where do I want to start? 327 00:12:53,880 --> 00:12:55,560 I want to start at nums dot begin 328 00:12:55,560 --> 00:12:57,510 and I want to go all the way to nums to end. 329 00:12:57,510 --> 00:12:59,910 So again the entire container 330 00:12:59,910 --> 00:13:01,770 I need start value and end value. 331 00:13:01,770 --> 00:13:03,300 So I need to capture those guys 332 00:13:03,300 --> 00:13:07,050 and that's what's happening right here in the capture list. 333 00:13:07,050 --> 00:13:09,240 I need to know what they are to be able to use them 334 00:13:09,240 --> 00:13:11,163 in my Lambda right in here. 335 00:13:12,300 --> 00:13:15,510 And what stood all of is going to do is for each element 336 00:13:15,510 --> 00:13:19,050 in that container, it's gonna pass it into this Lambda 337 00:13:19,050 --> 00:13:20,970 and all I'm gonna do is I'm gonna say return. 338 00:13:20,970 --> 00:13:23,280 If the value is between start value and end value 339 00:13:23,280 --> 00:13:25,020 that's what that logic is saying right here 340 00:13:25,020 --> 00:13:26,940 I'm gonna return result. 341 00:13:26,940 --> 00:13:29,790 So really straightforward, really powerful too. 342 00:13:29,790 --> 00:13:31,980 We don't have to deal with any of this ourselves. 343 00:13:31,980 --> 00:13:33,600 We just here is test six 344 00:13:33,600 --> 00:13:35,820 let me scroll this up a little bit as well. 345 00:13:35,820 --> 00:13:39,090 So you can see the test six output right up here. 346 00:13:39,090 --> 00:13:41,610 And here's test six, I'm setting std bull alpha just 347 00:13:41,610 --> 00:13:44,640 so I can display true faults rather than zero one. 348 00:13:44,640 --> 00:13:49,200 So I'm starting with a vector of 10 integers called num. 349 00:13:49,200 --> 00:13:51,240 I'm using parenths here, not curly. 350 00:13:51,240 --> 00:13:53,400 So I'm not initializing the first element 351 00:13:53,400 --> 00:13:54,600 of that vector to 10. 352 00:13:54,600 --> 00:13:56,550 I'm creating a vector of 10 elements 353 00:13:56,550 --> 00:13:58,680 and then I'm using that iota function, 354 00:13:58,680 --> 00:14:00,990 which is in the numeric library. 355 00:14:00,990 --> 00:14:02,280 And what it does, it goes 356 00:14:02,280 --> 00:14:06,210 from nums dot begin to nums dot end and I'm providing one. 357 00:14:06,210 --> 00:14:08,190 So basically what it's going to do is it's gonna 358 00:14:08,190 --> 00:14:10,440 set the values in those 10 integers 359 00:14:10,440 --> 00:14:13,800 to one, two, three, four, five, all the way to 10. 360 00:14:13,800 --> 00:14:15,870 If I'd have started this at 10 in here 361 00:14:15,870 --> 00:14:18,930 it would go from 10, 11, 12 and so forth. 362 00:14:18,930 --> 00:14:21,000 Really handy way assigned values 363 00:14:21,000 --> 00:14:24,333 to an existing container in incrementing order. 364 00:14:25,350 --> 00:14:28,140 So I'm displaying that and you can see right there 365 00:14:28,140 --> 00:14:29,940 that's exactly what iota has done. 366 00:14:29,940 --> 00:14:33,210 One through 10 is, I'm gonna call in between 367 00:14:33,210 --> 00:14:36,000 that's my function that I wrote up here. 368 00:14:36,000 --> 00:14:37,410 I'm gonna pass in the vector 369 00:14:37,410 --> 00:14:39,750 I'm gonna pass in the start and the end. 370 00:14:39,750 --> 00:14:41,640 And what it's going to do is gonna check to see 371 00:14:41,640 --> 00:14:44,383 if every element in that container 372 00:14:44,383 --> 00:14:46,503 is between 50 and 60. 373 00:14:46,503 --> 00:14:48,240 Well that's not true, right? 374 00:14:48,240 --> 00:14:51,090 Because the container contains one through 10. 375 00:14:51,090 --> 00:14:53,160 So they're not all between 50 and 60 376 00:14:53,160 --> 00:14:55,380 in fact none of them are between 50 and 60. 377 00:14:55,380 --> 00:14:57,240 So that's gonna return false. 378 00:14:57,240 --> 00:14:58,650 In this case I'm gonna check to see 379 00:14:58,650 --> 00:15:00,660 if the numbers are between one and 10. 380 00:15:00,660 --> 00:15:03,570 Well they are, it's returning true 381 00:15:03,570 --> 00:15:05,190 and in this case I want to check to see 382 00:15:05,190 --> 00:15:07,800 if the numbers are between five and seven. 383 00:15:07,800 --> 00:15:08,760 Of course they're not. 384 00:15:08,760 --> 00:15:10,710 So that returns false as well. 385 00:15:10,710 --> 00:15:13,260 Okay, so then finally we're at the last test here 386 00:15:13,260 --> 00:15:15,960 test seven, and this was a little bit more complicated 387 00:15:15,960 --> 00:15:17,130 but it's pretty cool. 388 00:15:17,130 --> 00:15:20,130 We have a class called password validator 389 00:15:20,130 --> 00:15:23,850 and it contains an attribute called restricted symbol. 390 00:15:23,850 --> 00:15:26,220 And in this case this is a single character 391 00:15:26,220 --> 00:15:27,570 that's the dollar sign. 392 00:15:27,570 --> 00:15:30,963 So basically passwords cannot have dollar signs in them. 393 00:15:31,890 --> 00:15:34,290 Then I've got a method called is valid. 394 00:15:34,290 --> 00:15:37,020 So we're gonna pass in a password and this method 395 00:15:37,020 --> 00:15:39,960 is going to tell us whether it's a valid, well it is 396 00:15:39,960 --> 00:15:43,770 valid if it doesn't contain that one simple in this example. 397 00:15:43,770 --> 00:15:45,690 So what this is going to do, 398 00:15:45,690 --> 00:15:48,510 it's going to check all of the characters 399 00:15:48,510 --> 00:15:51,450 in the password and be sure that none of them 400 00:15:51,450 --> 00:15:53,067 are the dollar sign, right? 401 00:15:53,067 --> 00:15:55,073 And we could write loops and we could do all kinds 402 00:15:55,073 --> 00:15:56,280 of stuff to do this ourselves 403 00:15:56,280 --> 00:15:59,400 but why not use the algorithm all of. 404 00:15:59,400 --> 00:16:00,570 So let's do that. 405 00:16:00,570 --> 00:16:02,010 I'm checking password 406 00:16:02,010 --> 00:16:04,500 from begin to end password is a string. 407 00:16:04,500 --> 00:16:07,230 So it's basically a container of characters. 408 00:16:07,230 --> 00:16:08,820 And again, I'm using stood o 409 00:16:08,820 --> 00:16:13,140 of I need to capture this object 410 00:16:13,140 --> 00:16:15,360 because I need access to that guy right there. 411 00:16:15,360 --> 00:16:16,650 Restricted symbol. 412 00:16:16,650 --> 00:16:19,080 So remember we're capturing the object here 413 00:16:19,080 --> 00:16:21,090 the pointer all of will pass 414 00:16:21,090 --> 00:16:23,760 in each character of the password into this Lambda. 415 00:16:23,760 --> 00:16:25,920 So now we have everything we need 416 00:16:25,920 --> 00:16:29,010 and this will return character not equal 417 00:16:29,010 --> 00:16:30,513 to restricted symbol. 418 00:16:31,560 --> 00:16:33,240 So this, if the character is not equal 419 00:16:33,240 --> 00:16:36,300 to the restricted symbol, we've got a valid password 420 00:16:36,300 --> 00:16:38,730 and that has to happen for all of the characters. 421 00:16:38,730 --> 00:16:42,300 If any of those fail, it's going to return false. 422 00:16:42,300 --> 00:16:43,133 Pretty cool. 423 00:16:43,133 --> 00:16:44,490 So let's see how we would use it. 424 00:16:44,490 --> 00:16:46,050 I've got another example down here that I'll go over 425 00:16:46,050 --> 00:16:47,340 in a second, but let's take a look 426 00:16:47,340 --> 00:16:50,130 at the all of and how we would use it right there. 427 00:16:50,130 --> 00:16:51,270 So we've got a password 428 00:16:51,270 --> 00:16:52,890 we're creating called Hollywood one 429 00:16:52,890 --> 00:16:54,210 with a dollar sign at the end. 430 00:16:54,210 --> 00:16:57,003 So this is an invalid password because of that guy. 431 00:16:58,110 --> 00:17:00,790 Now I'm instantiating my password validator 432 00:17:01,650 --> 00:17:04,500 and I'm just simply calling PV one dot is valid 433 00:17:04,500 --> 00:17:06,839 and I'm passing the password into it. 434 00:17:06,839 --> 00:17:07,673 That's it. 435 00:17:07,673 --> 00:17:08,760 Simple as that. 436 00:17:08,760 --> 00:17:09,960 It's going to return true 437 00:17:09,960 --> 00:17:13,380 if the password is valid, false if it's invalid, well 438 00:17:13,380 --> 00:17:16,829 in this case we know that that password is not valid 439 00:17:16,829 --> 00:17:19,290 because it has the dollar sign in it. 440 00:17:19,290 --> 00:17:22,109 If we change the password to just Hollywood one 441 00:17:22,109 --> 00:17:25,623 and execute this code, it will display is valid. 442 00:17:27,030 --> 00:17:28,256 Okay, so hopefully that makes sense. 443 00:17:28,256 --> 00:17:30,150 You can see what's happening. 444 00:17:30,150 --> 00:17:31,770 It's pretty easy. 445 00:17:31,770 --> 00:17:33,000 We've got characters coming 446 00:17:33,000 --> 00:17:35,242 in from that password string 447 00:17:35,242 --> 00:17:38,130 and we're comparing 'em to that restricted symbol. 448 00:17:38,130 --> 00:17:40,440 We need to capture, we need 449 00:17:40,440 --> 00:17:41,670 to capture the this pointer 450 00:17:41,670 --> 00:17:45,240 otherwise we won't have access to a restricted symbol. 451 00:17:45,240 --> 00:17:47,430 Let's take a look at another example. 452 00:17:47,430 --> 00:17:49,140 This is very, very similar. 453 00:17:49,140 --> 00:17:51,270 This is password validator two. 454 00:17:51,270 --> 00:17:52,250 The big difference here 455 00:17:52,250 --> 00:17:54,120 is I don't have a single character 456 00:17:54,120 --> 00:17:55,620 of restricted symbols. 457 00:17:55,620 --> 00:17:57,930 I've got a vector of restricted symbols. 458 00:17:57,930 --> 00:18:00,360 So in this case I've got a vector of characters. 459 00:18:00,360 --> 00:18:01,710 The bang, the dollar 460 00:18:01,710 --> 00:18:03,840 and the plus symbol are all restricted. 461 00:18:03,840 --> 00:18:05,970 That would make it an invalid password. 462 00:18:05,970 --> 00:18:07,650 It changes our logic a little bit 463 00:18:07,650 --> 00:18:09,270 but you can see the power here, 464 00:18:09,270 --> 00:18:11,160 I've still got my is valid method 465 00:18:11,160 --> 00:18:13,140 that expects that password. 466 00:18:13,140 --> 00:18:15,330 But what's gonna happen now is I'm gonna use all 467 00:18:15,330 --> 00:18:17,640 of and none of together. 468 00:18:17,640 --> 00:18:19,380 Now that's pretty cool. 469 00:18:19,380 --> 00:18:20,580 So let me show you how that works. 470 00:18:20,580 --> 00:18:22,530 So you could walk through this really carefully 471 00:18:22,530 --> 00:18:23,760 but basically what it's doing, 472 00:18:23,760 --> 00:18:25,140 it's making sure that all 473 00:18:25,140 --> 00:18:28,890 of the characters in the password are not restricted. 474 00:18:28,890 --> 00:18:29,850 And in order to do that 475 00:18:29,850 --> 00:18:32,820 we're using stood all of and stood none of. 476 00:18:32,820 --> 00:18:34,650 So again, what we're doing is we're making sure 477 00:18:34,650 --> 00:18:36,115 that all of the characters 478 00:18:36,115 --> 00:18:38,460 in the password have none 479 00:18:38,460 --> 00:18:40,290 of the restricted symbols in them. 480 00:18:40,290 --> 00:18:41,880 And if we run this example 481 00:18:41,880 --> 00:18:44,340 we've got C++ rocks that has a bunch 482 00:18:44,340 --> 00:18:45,390 of illegal characters in it, right? 483 00:18:45,390 --> 00:18:47,520 It's got the plus and the bang. 484 00:18:47,520 --> 00:18:50,100 So we're initializing PV two 485 00:18:50,100 --> 00:18:52,950 and we're checking whether it's valid password or not. 486 00:18:52,950 --> 00:18:54,090 Well, it's not valid 487 00:18:54,090 --> 00:18:57,270 because it's got the pluses and the bang. 488 00:18:57,270 --> 00:18:58,800 In this case we've got a single 489 00:18:58,800 --> 00:19:01,290 bang that's also not valid. 490 00:19:01,290 --> 00:19:04,530 And then in this case we have none of those characters. 491 00:19:04,530 --> 00:19:06,213 Therefore the password is valid. 492 00:19:07,830 --> 00:19:11,008 In this video we've seen some common STL algorithms 493 00:19:11,008 --> 00:19:12,990 and how to use Lambda with them. 494 00:19:12,990 --> 00:19:14,190 But it's important to note 495 00:19:14,190 --> 00:19:16,530 that this in no way covers them all. 496 00:19:16,530 --> 00:19:18,000 There are many, many algorithms 497 00:19:18,000 --> 00:19:21,030 in the STL algorithms library that use Lambdas 498 00:19:21,030 --> 00:19:24,060 and as we saw what the class password validator too 499 00:19:24,060 --> 00:19:27,270 that can even be used in conjunction with one another. 500 00:19:27,270 --> 00:19:29,070 It would be impossible to cover them all. 501 00:19:29,070 --> 00:19:30,960 But hopefully this gives you a good understanding 502 00:19:30,960 --> 00:19:34,710 of why Lambdas are so important and their patterns of use. 503 00:19:34,710 --> 00:19:36,090 Learn the STL algorithms 504 00:19:36,090 --> 00:19:37,770 they'll become your best friends. 505 00:19:37,770 --> 00:19:39,870 Also, if you're a competitive programmer 506 00:19:39,870 --> 00:19:42,060 consider using the STL algorithms 507 00:19:42,060 --> 00:19:44,676 before you code your own solution. 508 00:19:44,676 --> 00:19:47,400 Okay, so that wraps up this section on Lambdas. 509 00:19:47,400 --> 00:19:49,200 I hope you find this section useful.