1 00:00:05,480 --> 00:00:08,580 In this video, I'd like to go over 1 of the most basic of the stl 2 00:00:09,120 --> 00:00:11,818 containers, the array container. 3 00:00:12,290 --> 00:00:14,430 I'll call it std array in this video, so we don't 4 00:00:14,430 --> 00:00:15,929 confuse it with a raw array. 5 00:00:16,910 --> 00:00:20,020 In order to use the std array container, we must include 6 00:00:20,020 --> 00:00:21,189 the array header file. 7 00:00:22,250 --> 00:00:26,640 The std array container allows us to create fixed size arrays of any type. 8 00:00:27,369 --> 00:00:31,180 So what makes this different from a raw array, a lot. 9 00:00:31,580 --> 00:00:33,099 First, it's an object type. 10 00:00:33,460 --> 00:00:36,710 Std array always has its size associated with it. 11 00:00:37,110 --> 00:00:40,740 Unlike a raw array, a std array doesn't decay to a pointer to 12 00:00:40,740 --> 00:00:43,610 the first element of the array when we pass it into a function. 13 00:00:44,920 --> 00:00:46,169 It's also very efficient. 14 00:00:46,450 --> 00:00:48,910 Element access happens in constant time. 15 00:00:49,259 --> 00:00:52,269 Constant time means that the time it takes to access an element 16 00:00:52,279 --> 00:00:54,980 is independent of the number of elements in the container. 17 00:00:55,419 --> 00:00:59,160 So if std array has 5 elements and you wanted to access 1 of them. 18 00:00:59,370 --> 00:01:02,880 The time required would be the same as if it had 100,000 elements. 19 00:01:03,740 --> 00:01:07,099 Also since a std array is implemented behind the scenes by 20 00:01:07,099 --> 00:01:12,530 the stl as a wrapper class around a raw array, we can get access 21 00:01:12,539 --> 00:01:15,970 to that raw array address if we need to and we can use it with 22 00:01:15,970 --> 00:01:17,940 libraries that use raw pointers. 23 00:01:18,830 --> 00:01:22,280 But the biggest benefit is that it's part of the stl, so we can use 24 00:01:22,280 --> 00:01:24,080 it with iterators and algorithms. 25 00:01:24,220 --> 00:01:27,099 The std array class wasn't part of the original stl. 26 00:01:28,080 --> 00:01:30,070 It was added in c++11. 27 00:01:30,889 --> 00:01:34,929 You should always first consider using std array in modern c++ when you 28 00:01:34,930 --> 00:01:36,790 have a need for a fixed size array. 29 00:01:37,480 --> 00:01:40,780 Don't use raw arrays unless you have a compelling reason to do so. 30 00:01:42,020 --> 00:01:44,800 All of the stl iterators are available with std array, 31 00:01:45,030 --> 00:01:46,239 and they don't invalidate. 32 00:01:46,430 --> 00:01:49,440 And that makes sense because std array is a fixed size structure. 33 00:01:50,170 --> 00:01:52,343 Okay, so first, let's look at a few ways to initialize std 34 00:01:52,380 --> 00:01:56,080 arrays and then we'll see some of the common member methods. 35 00:01:58,990 --> 00:02:01,479 Std array is a very simple template class. 36 00:02:01,969 --> 00:02:05,790 Since the size of the array must be known at compile time, we have 37 00:02:05,790 --> 00:02:09,729 to provide both the type we want to store in it as well as the size. 38 00:02:10,699 --> 00:02:13,010 This is exactly what we did a few videos ago when we 39 00:02:13,010 --> 00:02:14,540 created a my array class. 40 00:02:14,840 --> 00:02:19,230 In the first example in the slide, I declared arr1 as a std array 41 00:02:19,230 --> 00:02:24,229 of 5 integers, and I initialize these 5 integers to 1 2 3 4 and 5. 42 00:02:24,800 --> 00:02:28,130 If we have more than 5 integers in the initializer list, we'll get an error. 43 00:02:29,030 --> 00:02:33,150 If we have fewer than 5, the remaining integers omitted will be set to 0. 44 00:02:33,710 --> 00:02:36,090 Now there is a quark with the initializer list. 45 00:02:36,280 --> 00:02:39,820 In c++11, you need to provide double curly braces. 46 00:02:40,029 --> 00:02:44,100 In c++14 and forward you don't, just a single curly brace. 47 00:02:45,270 --> 00:02:48,380 So in the second example, I'm declaring stooges as 48 00:02:48,380 --> 00:02:50,500 a std array of 3 strings. 49 00:02:50,970 --> 00:02:53,220 And I'm initializing them to Larry Moe and Curly. 50 00:02:53,540 --> 00:02:55,713 Note that I can use c-style string literals and they'll 51 00:02:55,750 --> 00:02:57,360 be converted to std strings. 52 00:02:58,320 --> 00:03:01,420 There's also a very handy way to assign multiple values 53 00:03:01,470 --> 00:03:02,799 to an existing std array. 54 00:03:03,210 --> 00:03:06,280 We can use an assignment statement and an initializer list. 55 00:03:06,440 --> 00:03:08,269 This is really cool and extremely useful. 56 00:03:08,970 --> 00:03:12,330 So now let's see some common methods provided by std array. 57 00:03:14,330 --> 00:03:17,710 In this section, I'm not going to exhaustively go over all the methods 58 00:03:17,719 --> 00:03:19,989 provided by all of the stl containers. 59 00:03:20,520 --> 00:03:22,550 That would be pretty boring for me and, it would probably 60 00:03:22,560 --> 00:03:23,870 be even more boring for you. 61 00:03:24,390 --> 00:03:26,809 So what I want to do is I'll go over some of the most common 62 00:03:26,809 --> 00:03:30,779 methods used and some of the methods particular to specific containers. 63 00:03:31,250 --> 00:03:33,140 So let's start with the size method. 64 00:03:33,540 --> 00:03:35,820 This method is available to all containers. 65 00:03:36,150 --> 00:03:37,780 In the std array, it's pretty simple. 66 00:03:38,030 --> 00:03:40,570 It's the size of the container and it's fixed. 67 00:03:41,860 --> 00:03:44,530 Arrays also let us access individual array elements. 68 00:03:44,530 --> 00:03:49,269 We can use either the subscript syntax or the .at method. 69 00:03:49,940 --> 00:03:52,939 Now if you remember from the vector, subscript operator 70 00:03:53,070 --> 00:03:54,490 does no bounds checking. 71 00:03:55,020 --> 00:03:58,549 But the at method does do bounce checking, and an exception is 72 00:03:58,550 --> 00:04:00,400 thrown if we're out of bounds. 73 00:04:01,110 --> 00:04:05,840 You can see in the example code that arr at 0 will refer to 1 in the array 74 00:04:05,950 --> 00:04:08,709 arr, and arr sub-1 refers to the 2. 75 00:04:10,249 --> 00:04:13,340 The std array also provides front and back methods. 76 00:04:13,570 --> 00:04:17,050 Now these methods return references to the element at the front of the 77 00:04:17,060 --> 00:04:20,470 array and of the element at the back of the array, respectively. 78 00:04:21,010 --> 00:04:26,879 In this example arr.front refers to the 1 and arr.back refers to the 5. 79 00:04:29,539 --> 00:04:32,169 The empty method will return true if the std array is 80 00:04:32,170 --> 00:04:33,590 empty and false otherwise. 81 00:04:34,150 --> 00:04:36,580 This doesn't mean how many elements out of the total elements 82 00:04:36,580 --> 00:04:39,750 you're using, it basically means it's the size of the array. 83 00:04:40,330 --> 00:04:42,730 This is rarely ever used with std array but it is 84 00:04:42,750 --> 00:04:44,199 with other stl containers. 85 00:04:45,040 --> 00:04:48,110 The fill method is very handy, it fills the std array with 86 00:04:48,110 --> 00:04:49,460 whatever element is provided. 87 00:04:50,280 --> 00:04:52,920 Remember, I'm using ints here for these examples, but these 88 00:04:52,920 --> 00:04:54,500 could be almost any data type. 89 00:04:54,980 --> 00:04:58,130 We can also swap 2 std arrays of the same size and type. 90 00:04:58,620 --> 00:05:01,899 All we have to do is simply call swap with 1 and pass the other in. 91 00:05:02,730 --> 00:05:06,440 And finally, we can use the dot data method to get access 92 00:05:06,440 --> 00:05:08,200 to the underlying raw array. 93 00:05:08,680 --> 00:05:13,000 In this case, data returns the address of the raw array of integers, which 94 00:05:13,000 --> 00:05:14,390 we store in a pointer variable. 95 00:05:16,160 --> 00:05:18,690 As you can see, std arrays are very straightforward. 96 00:05:18,990 --> 00:05:21,299 Now let's head over to the IDE and we'll see some more 97 00:05:21,299 --> 00:05:22,769 examples of using std array. 98 00:05:24,980 --> 00:05:28,284 Before we head off to the IDE and run some examples with the 99 00:05:28,400 --> 00:05:31,430 std array, I'd like to refer you to this website and I've mentioned 100 00:05:31,790 --> 00:05:35,990 this website a couple times in the course, it's cppreference.com. 101 00:05:36,330 --> 00:05:39,140 I'm here in the United States, so I'm getting the english version. 102 00:05:39,420 --> 00:05:41,949 I'm not sure if this is localized for other countries. 103 00:05:41,949 --> 00:05:44,690 It seems like it would be because you see the EN at the beginning here. 104 00:05:45,430 --> 00:05:49,330 In any case, go to cppreference.com and it's got all kinds of 105 00:05:49,330 --> 00:05:51,300 good information about c++. 106 00:05:51,469 --> 00:05:54,230 Up to now, we really haven't needed it too much, but with the 107 00:05:54,230 --> 00:05:58,500 containers library and the stl, your algorithms and your iterators, 108 00:05:58,500 --> 00:06:00,330 this is a really useful reference. 109 00:06:00,779 --> 00:06:02,539 I'm right at the main page here. 110 00:06:02,539 --> 00:06:05,020 And you can see the containers library is right here. 111 00:06:05,020 --> 00:06:10,169 So I'll just select that, and it shows you all the containers in the stls. 112 00:06:10,190 --> 00:06:12,439 For example, array that's one we're going to talk about now. 113 00:06:12,799 --> 00:06:14,540 A vector, a deck, a forward list. 114 00:06:14,540 --> 00:06:16,370 We'll talk about all of these in this section. 115 00:06:16,570 --> 00:06:17,780 But let's just look at the array. 116 00:06:18,180 --> 00:06:20,320 And this gives you a lot of really good reference and 117 00:06:20,320 --> 00:06:21,890 documentation on the array. 118 00:06:21,890 --> 00:06:24,280 You can see it's implemented as a template class here. 119 00:06:24,880 --> 00:06:28,530 And if you scroll down just a little bit all of these containers will 120 00:06:28,530 --> 00:06:30,310 talk about iterator and validation. 121 00:06:30,310 --> 00:06:33,240 So if you're not sure about -- if you've got an iterator and you've 122 00:06:33,240 --> 00:06:35,150 removed an item is it invalid now. 123 00:06:36,010 --> 00:06:38,789 it'll say it all throughout the documentation here. 124 00:06:39,350 --> 00:06:42,169 Also the other important piece are these, right here, 125 00:06:42,170 --> 00:06:43,380 these member functions. 126 00:06:43,690 --> 00:06:45,909 This tells you what member functions are available for 127 00:06:45,910 --> 00:06:47,060 this particular container. 128 00:06:47,060 --> 00:06:50,349 Now you can see here I can construct it and destruct it and so forth. 129 00:06:50,630 --> 00:06:52,459 But these are the real important ones here. 130 00:06:52,959 --> 00:06:55,450 This is a really simple container, the array container. 131 00:06:55,680 --> 00:06:59,090 So you've got the at method, you've got the overloader subscript operator. 132 00:06:59,550 --> 00:07:03,170 Notice this one does balance checking, and the operator subscript does 133 00:07:03,170 --> 00:07:04,910 not, the front, the back, the data. 134 00:07:04,910 --> 00:07:06,730 The same things I talked about in the slides. 135 00:07:07,130 --> 00:07:08,250 We've also got iterators. 136 00:07:08,250 --> 00:07:10,709 You can see all the iterators are available to us here. 137 00:07:11,039 --> 00:07:13,980 And then we've got some capacity methods and the fill and the 138 00:07:14,020 --> 00:07:15,330 swap that I also mentioned. 139 00:07:15,530 --> 00:07:18,260 So this gives you a lot of really good information. 140 00:07:18,510 --> 00:07:20,900 Now if you're not sure about what the front method does, I mean it 141 00:07:20,900 --> 00:07:24,119 says here access the first element, which is makes sense right the front. 142 00:07:24,429 --> 00:07:26,500 But if you want more information, you can click on here. 143 00:07:26,950 --> 00:07:30,840 And not all of these things are going to be easy reads when you're first 144 00:07:30,840 --> 00:07:34,400 starting out with c++, but you'll understand this soon enough and little 145 00:07:34,400 --> 00:07:37,340 by little, you'll understand more and more and more, and then these 146 00:07:37,340 --> 00:07:39,340 references become very, very handy. 147 00:07:39,440 --> 00:07:41,150 In this case, you can see what does it do. 148 00:07:41,440 --> 00:07:44,400 Well, it provides a reference to the first element. 149 00:07:44,450 --> 00:07:45,929 That's kind of what I said earlier, right. 150 00:07:46,150 --> 00:07:49,020 The complexity is constant time, which is cool. 151 00:07:49,240 --> 00:07:51,890 Usually most of these methods will have a complexity 152 00:07:51,890 --> 00:07:53,000 associated with them. 153 00:07:53,400 --> 00:07:54,299 And it'll be listed here. 154 00:07:54,670 --> 00:07:57,940 I mentioned before that with the stl you know what you get. 155 00:07:58,510 --> 00:08:01,000 So when you're using these methods, you know their complexity. 156 00:08:01,290 --> 00:08:04,020 So if you're using something that's a linear time, you'll understand that. 157 00:08:04,020 --> 00:08:06,199 If it's constant time, it'll say it as well. 158 00:08:06,440 --> 00:08:09,280 And then it's got usually some kind of sample code here that you 159 00:08:09,280 --> 00:08:11,010 can run right inside your browser. 160 00:08:11,340 --> 00:08:12,980 So it's a very handy site. 161 00:08:13,230 --> 00:08:14,799 I think you're going to refer to this a lot. 162 00:08:14,980 --> 00:08:17,639 When I go to the ide, I'll be playing with a lot of source code 163 00:08:17,639 --> 00:08:20,640 that you can play around with and it covers the majority of these, 164 00:08:20,640 --> 00:08:23,929 but obviously, it's impossible to cover them all, especially as the 165 00:08:23,940 --> 00:08:27,280 containers and the algorithms and the iterators all come together, the 166 00:08:27,280 --> 00:08:29,450 permutations just go skyrocketing. 167 00:08:29,720 --> 00:08:30,870 So play on your own. 168 00:08:30,870 --> 00:08:32,530 And go through here, study this. 169 00:08:32,539 --> 00:08:36,770 This is the best way to really learn the stl, but watch the videos 170 00:08:36,770 --> 00:08:39,159 first and then you'll be able to understand this a lot better. 171 00:08:39,380 --> 00:08:40,899 All right, so let's head over to the IDE. 172 00:08:43,150 --> 00:08:44,188 Okay, so I'm back in the IDE. 173 00:08:44,590 --> 00:08:47,699 I'm in the section 20 workspace in the array project. 174 00:08:48,870 --> 00:08:53,319 And what I've done is I've broken this project up into 9 test functions. 175 00:08:53,559 --> 00:08:56,300 And each one of these functions is independent and deals with 176 00:08:56,300 --> 00:09:01,189 specific methods or iterators or algorithms that we can really learn. 177 00:09:01,590 --> 00:09:06,130 My perspective on the stl is the more examples you can see, the 178 00:09:06,140 --> 00:09:07,290 better you're going to learn it. 179 00:09:07,690 --> 00:09:11,690 And the more you can apply what you're learning to other examples that you haven't seen yet. 180 00:09:11,690 --> 00:09:13,270 Okay, so why don't we start. 181 00:09:13,270 --> 00:09:15,500 Rather than jump back and forth and running and compiling and 182 00:09:15,670 --> 00:09:16,830 so forth, I've already run this. 183 00:09:17,570 --> 00:09:19,140 And the output is over here on the right. 184 00:09:19,570 --> 00:09:22,880 I've run all 9 functions over here on the right, and there's the output. 185 00:09:23,090 --> 00:09:25,090 And what I'll do is I'll go through them, so you can 186 00:09:25,090 --> 00:09:26,760 understand exactly what's going on. 187 00:09:26,760 --> 00:09:29,260 This will save a little time, and that way you can stop the 188 00:09:29,260 --> 00:09:30,560 video and look at them as well. 189 00:09:31,080 --> 00:09:32,210 So let's start with test 1. 190 00:09:33,440 --> 00:09:37,260 First thing we're going to do here is we're going to create 2 std arrays. 191 00:09:37,620 --> 00:09:40,010 They're both the same size, and they're both of the same type. 192 00:09:40,370 --> 00:09:47,065 So I'm creating an array right here called arr1 of 5 integers, and I'm 193 00:09:47,120 --> 00:09:50,234 initializing it to 1 2 3 4 and 5. 194 00:09:50,530 --> 00:09:54,530 I'm also creating array 2, same size same type, but 195 00:09:54,530 --> 00:09:55,619 I'm not initializing it. 196 00:09:56,450 --> 00:09:57,670 And then I'm displaying both arrays. 197 00:09:58,070 --> 00:10:00,250 Now the display function is right up here. 198 00:10:00,470 --> 00:10:02,170 Let me clear this and show that to you. 199 00:10:02,590 --> 00:10:06,970 And the way the display functions work for std arrays, notice the parameter 200 00:10:06,970 --> 00:10:08,079 right here, the parameter type. 201 00:10:08,700 --> 00:10:10,600 You've got to supply the information. 202 00:10:11,379 --> 00:10:13,459 So you've got to supply the type and the size. 203 00:10:13,590 --> 00:10:16,670 For all it's doing is just using a range-based for loop to display it. 204 00:10:17,420 --> 00:10:19,140 All right, so let's get back to this. 205 00:10:19,350 --> 00:10:21,040 Let's go through test case 1 here. 206 00:10:21,610 --> 00:10:25,055 So we've got those 2 arrays, arr1 arr2. 207 00:10:25,360 --> 00:10:26,689 I'm displaying both of them. 208 00:10:27,179 --> 00:10:28,349 They're out right here. 209 00:10:28,350 --> 00:10:30,460 You can see there's my test 1 output right here. 210 00:10:30,779 --> 00:10:34,260 Notice the first 1 prints out 1 2 3 4 5, exactly like we expect. 211 00:10:34,330 --> 00:10:37,090 Now notice arr2, we're displaying it right here. 212 00:10:37,400 --> 00:10:39,180 We've got 5 garbage values. 213 00:10:39,180 --> 00:10:40,770 So nothing is initialized. 214 00:10:41,130 --> 00:10:44,095 When we do something like this, we're initializing the std 215 00:10:44,095 --> 00:10:47,259 array, but we're not initializing the elements of the std array. 216 00:10:47,770 --> 00:10:49,080 That's important to understand. 217 00:10:49,240 --> 00:10:52,210 Now we have here is a really nice way to assign a bunch 218 00:10:52,210 --> 00:10:54,160 of values to a std array. 219 00:10:54,450 --> 00:10:57,530 And the syntax is dead simple and it's so handy. 220 00:10:57,809 --> 00:11:01,120 So what we're doing here is we're assigning 10 20 30 40 and 50 to 221 00:11:01,170 --> 00:11:04,200 arr2, that's the one that had all those garbage values in it. 222 00:11:04,480 --> 00:11:06,570 And then we're displaying those 2 arrays again. 223 00:11:06,880 --> 00:11:12,540 So you can see them displaying right here, 1 2 3 4 5 and now 10 20 30 40 and 50, 224 00:11:12,540 --> 00:11:13,540 which is really nice. 225 00:11:14,590 --> 00:11:16,370 Here we're just displaying both sizes. 226 00:11:16,380 --> 00:11:17,839 Now they're both the same size, right. 227 00:11:17,840 --> 00:11:21,940 So we should -- we expect a 5 and a 5, and that's exactly what we get here. 228 00:11:22,379 --> 00:11:23,519 They're both size 5. 229 00:11:23,969 --> 00:11:26,620 Size isn't really used very often with std arrays. 230 00:11:27,080 --> 00:11:28,320 The size is fixed, right. 231 00:11:28,320 --> 00:11:29,400 So it is what it is. 232 00:11:29,490 --> 00:11:30,220 It's right here. 233 00:11:30,910 --> 00:11:33,660 But it's used a lot in other containers. 234 00:11:34,090 --> 00:11:36,980 Let me clear this a little bit, and let me scroll up just a bit so we 235 00:11:36,980 --> 00:11:39,680 can get to these guys right down here, the last couple of lines. 236 00:11:39,910 --> 00:11:44,890 You can see here in this example that we can subscript into 237 00:11:45,880 --> 00:11:47,709 arr1 or arr2 any std array. 238 00:11:48,340 --> 00:11:50,360 This does not do bounce checking. 239 00:11:50,790 --> 00:11:55,439 So what we're doing here is we're storing a 1000 into arr1 sub-0. 240 00:11:56,049 --> 00:11:57,189 Okay, that's this guy. 241 00:11:57,240 --> 00:11:59,660 We're basically replacing that 1 with a 1000. 242 00:12:00,299 --> 00:12:06,190 And then over here what I'm doing is I'm replacing arr at 1 with 243 00:12:06,190 --> 00:12:07,360 2000 so that would be the 2. 244 00:12:08,270 --> 00:12:10,209 So I'm replacing the 2 with 2000. 245 00:12:11,580 --> 00:12:15,670 This does no bounce checking, this does do bounce checking. 246 00:12:16,010 --> 00:12:18,920 So if I'm out of bounds right here, I'm going to get an exception 247 00:12:18,930 --> 00:12:20,390 thrown an out of range exception. 248 00:12:21,220 --> 00:12:23,840 And all I'm doing now is displaying arr1, and you can 249 00:12:23,840 --> 00:12:26,270 see arr1 displaying right here. 250 00:12:26,880 --> 00:12:29,690 Notice the 1000 and the 2000 have been updated. 251 00:12:29,740 --> 00:12:30,999 I still have the 3 4 and the 5. 252 00:12:32,170 --> 00:12:37,449 And then the last 2 examples are right here at the bottom where I'm saying 253 00:12:37,490 --> 00:12:42,990 what is arr.front and what is arr -- sorry, arr2.front and arr2.back. 254 00:12:43,270 --> 00:12:45,900 So what's the front element of array 2, and what's the 255 00:12:45,910 --> 00:12:47,090 back element of array 2. 256 00:12:47,700 --> 00:12:49,099 Well, array 2 is right here. 257 00:12:49,370 --> 00:12:52,100 The front element is a 10, the back element is a 50. 258 00:12:52,100 --> 00:12:53,510 That's exactly what we expect. 259 00:12:54,039 --> 00:12:55,710 That's exactly what prints right here. 260 00:12:57,450 --> 00:12:59,350 Okay, so some really simple examples. 261 00:12:59,360 --> 00:13:01,750 And again, I'm going to do a lot of examples in this section 262 00:13:01,750 --> 00:13:04,120 so you can better understand how some of this stuff works. 263 00:13:04,219 --> 00:13:05,790 Let's take a look at the next one now. 264 00:13:06,799 --> 00:13:09,649 And you can see the output for test 2 right over here 265 00:13:09,650 --> 00:13:11,239 on the right, right here. 266 00:13:11,819 --> 00:13:14,500 All right, so what are we doing here, same idea. 267 00:13:14,510 --> 00:13:16,310 I've got 2 arrays, array1 and array2. 268 00:13:16,670 --> 00:13:21,129 I'm initializing the first one 1 to 5 and 10 20 30 40 and 50 for array2. 269 00:13:22,300 --> 00:13:23,530 I'm displaying both of them. 270 00:13:23,980 --> 00:13:26,960 And there they are right there, just what you would expect 1 2 3 4 271 00:13:26,960 --> 00:13:28,959 5 and 10, all the way through 50. 272 00:13:30,090 --> 00:13:31,850 Now I'm using the fill method. 273 00:13:32,230 --> 00:13:34,410 I'm filling array1 with 0s. 274 00:13:34,410 --> 00:13:36,079 And this could be any integer. 275 00:13:36,080 --> 00:13:37,410 I'm just passing a 0 into it. 276 00:13:37,800 --> 00:13:40,230 And again, remember, I'm using integers for these examples. 277 00:13:40,230 --> 00:13:41,619 These could be almost any type. 278 00:13:42,059 --> 00:13:43,080 So I'm using a 0 here. 279 00:13:43,080 --> 00:13:45,949 I'm filling array1 with a 0, and then I'm displaying those guys again. 280 00:13:46,289 --> 00:13:50,430 Notice how array1 right here is all 0s now. 281 00:13:50,670 --> 00:13:53,780 So it's a real handy way to fill an array with whatever value we want. 282 00:13:55,160 --> 00:13:59,380 Now we're coming here, and what we're saying is let's swap those 2 arrays. 283 00:13:59,440 --> 00:14:03,889 I could say array1.swap array2 or array2.swap array1, right. 284 00:14:03,900 --> 00:14:05,090 I mean swapping is swapping. 285 00:14:05,299 --> 00:14:09,410 So in this case, I'm swapping those 2 arrays, and I'm displaying them again. 286 00:14:09,420 --> 00:14:11,839 And you can see that they have indeed been swapped, right. 287 00:14:12,220 --> 00:14:14,340 First it was a 0, then it was 10 to 50. 288 00:14:14,349 --> 00:14:16,980 Now it's 10 to 50 and the 0s are second. 289 00:14:18,090 --> 00:14:21,029 Okay, so let's move on to the third test. 290 00:14:21,599 --> 00:14:22,939 And let's take a look at this one. 291 00:14:23,129 --> 00:14:24,659 The results are right here. 292 00:14:25,880 --> 00:14:29,190 I had another line that printed that header right here, but I removed it. 293 00:14:29,840 --> 00:14:31,470 That's why you're seeing that test 3 twice. 294 00:14:31,810 --> 00:14:34,240 All right, so I've got array1 1 2 3 4 5. 295 00:14:34,270 --> 00:14:36,630 Again, it's a std array of 5 integers. 296 00:14:36,940 --> 00:14:39,170 Now what's going on here. 297 00:14:39,250 --> 00:14:44,219 The data method returns the raw pointer that points to the 298 00:14:44,310 --> 00:14:46,510 behind the scenes raw array. 299 00:14:47,070 --> 00:14:52,550 What happens is, remember, arr1 is an object. 300 00:14:53,469 --> 00:14:56,289 So it has some data associated with it. 301 00:14:56,450 --> 00:14:58,219 One of the pieces of data is 5, right. 302 00:14:58,219 --> 00:14:59,200 It knows its size. 303 00:15:00,109 --> 00:15:02,640 The other piece of data that it has is the actual array. 304 00:15:03,420 --> 00:15:04,770 In this case, 5. 305 00:15:04,930 --> 00:15:09,870 So this guy is what's returned the address of that element. 306 00:15:10,380 --> 00:15:14,820 So when we say arr1.data, what we're returning is the address 307 00:15:14,820 --> 00:15:18,010 of that first element in the raw array behind the scenes. 308 00:15:18,029 --> 00:15:21,469 Remember, the std array is really a wrapper around a raw array. 309 00:15:21,479 --> 00:15:25,390 And I'm storing that into pointer which is a pointer to an integer, 310 00:15:25,390 --> 00:15:26,290 the address of an integer. 311 00:15:26,290 --> 00:15:29,790 Now if I display pointer, you can see right there, that's the 312 00:15:29,790 --> 00:15:31,170 memory address being displayed. 313 00:15:31,460 --> 00:15:34,380 That's the memory address of that first element in the array. 314 00:15:35,629 --> 00:15:37,270 Now I can change that through the pointer. 315 00:15:37,270 --> 00:15:39,900 I could say dereference pointer equals 10000. 316 00:15:39,900 --> 00:15:41,870 And I just put a 10000 in here. 317 00:15:43,550 --> 00:15:46,549 And if I display arr1, you can see it right here, see that 318 00:15:46,630 --> 00:15:48,480 10000 being updated right there. 319 00:15:48,969 --> 00:15:50,910 Now you don't do this very often. 320 00:15:51,570 --> 00:15:55,180 Obviously, the whole point of using a std array is not to use a raw array. 321 00:15:55,390 --> 00:16:00,800 But sometimes we use libraries that use raw arrays. 322 00:16:00,800 --> 00:16:03,709 So we can still use std arrays for our work. 323 00:16:03,719 --> 00:16:06,959 And then when we need to pass in the raw array, we can use the data 324 00:16:06,960 --> 00:16:09,000 method and send them what they want. 325 00:16:09,700 --> 00:16:12,780 But we can still have the safety and ease of use on our end. 326 00:16:12,940 --> 00:16:16,350 And what's really cool about using the std arrays on our end is because 327 00:16:16,640 --> 00:16:19,610 they are stl containers, so we can use them with iterators, we can use 328 00:16:19,620 --> 00:16:21,000 them with algorithms and so forth. 329 00:16:21,010 --> 00:16:22,150 So that's that's the real power. 330 00:16:22,800 --> 00:16:24,080 We'll go to test 4. 331 00:16:24,910 --> 00:16:26,829 And test 4 is a real simple example. 332 00:16:26,830 --> 00:16:27,960 We're going to sort this array. 333 00:16:27,960 --> 00:16:29,109 It's really, really cool. 334 00:16:29,389 --> 00:16:31,510 Here's the array 2 1 4 5 3. 335 00:16:31,510 --> 00:16:32,910 Again, all of these are std arrays. 336 00:16:33,280 --> 00:16:35,750 And I've defined this 1 to 5 integers again. 337 00:16:36,170 --> 00:16:37,520 I'm displaying it right here. 338 00:16:37,520 --> 00:16:39,970 And this is test 4 so you can see the output right here. 339 00:16:40,790 --> 00:16:43,830 So I'm displaying array 1 2 1 4 5 3. 340 00:16:43,840 --> 00:16:46,140 That's what we get right here, 2 1 4 5 3. 341 00:16:47,139 --> 00:16:47,930 Then I'm sorting it. 342 00:16:48,350 --> 00:16:50,439 Now this sort is an algorithm, right. 343 00:16:50,450 --> 00:16:54,030 That's my sort function and it expects 2 iterators, where 344 00:16:54,030 --> 00:16:55,640 do I start, where do I end. 345 00:16:56,330 --> 00:16:59,489 Well, I'm starting at the beginning, and I'm ending at the end. 346 00:16:59,490 --> 00:17:01,020 I'm sorting the entire array. 347 00:17:01,570 --> 00:17:02,479 And then I'm displaying it. 348 00:17:02,479 --> 00:17:05,450 So you can see right here it's displaying the sorted array, 349 00:17:05,450 --> 00:17:07,440 1 2 3 4 5, easy as that. 350 00:17:08,819 --> 00:17:10,990 All right, so let's take a look at test 5. 351 00:17:11,130 --> 00:17:13,560 And for each one of these tests, I tried to add a little 352 00:17:13,560 --> 00:17:15,160 bit different functionalities. 353 00:17:15,200 --> 00:17:17,628 Again, the more examples, you see the better. 354 00:17:17,858 --> 00:17:20,500 Hopefully, you'll start seeing the patterns of use, and it makes it 355 00:17:20,509 --> 00:17:21,739 much much easier to understand. 356 00:17:22,098 --> 00:17:23,180 All right, so here's test 5. 357 00:17:23,190 --> 00:17:25,750 You can see the output right here is just 1 line of output. 358 00:17:26,030 --> 00:17:27,770 Here's my array arr1 2 1 4 5 3. 359 00:17:27,770 --> 00:17:33,150 And what we want to do is we want to find the minimum the maximum in there. 360 00:17:33,889 --> 00:17:37,189 So we have an algorithm called min element, and we have 361 00:17:37,190 --> 00:17:38,770 another one called max element. 362 00:17:39,259 --> 00:17:42,460 It expects iterators, right, it's an algorithm. 363 00:17:42,470 --> 00:17:45,240 So array1 begin array1.end. 364 00:17:45,470 --> 00:17:49,530 I want to search through the entire array for the minimum element. 365 00:17:49,830 --> 00:17:51,590 And this doesn't return the minimum element. 366 00:17:51,620 --> 00:17:54,119 This returns an iterator to the minimum element. 367 00:17:54,450 --> 00:17:56,470 So this is how you would declare that variable. 368 00:17:56,470 --> 00:18:01,776 Min num is an iterator to a std array of 5 integers. 369 00:18:01,776 --> 00:18:02,990 Now that's ugly, right. 370 00:18:03,000 --> 00:18:05,080 That's really ugly looking code. 371 00:18:05,080 --> 00:18:06,450 It's readable, but it's kind of ugly. 372 00:18:06,860 --> 00:18:07,970 This is much nicer. 373 00:18:08,860 --> 00:18:10,029 Let the compiler figure it out. 374 00:18:10,210 --> 00:18:12,430 So I did it the long way for the minimum number. 375 00:18:12,430 --> 00:18:15,219 And I did it the short way using auto for the max number. 376 00:18:15,920 --> 00:18:20,690 So all I'm saying is auto max num hey compiler figure out that max number 377 00:18:20,690 --> 00:18:22,170 is type based on the return here. 378 00:18:22,690 --> 00:18:23,710 And I'm doing the same thing. 379 00:18:23,940 --> 00:18:27,120 And remember, we get back iterators so we dereference the iterators 380 00:18:27,969 --> 00:18:30,990 and we get back the min number, which in this case is 1 and the 381 00:18:31,000 --> 00:18:32,549 max number which in this case is 5. 382 00:18:33,320 --> 00:18:35,399 And that's exactly what we expected here. 383 00:18:36,459 --> 00:18:38,379 So let's move on to test 6. 384 00:18:38,840 --> 00:18:40,590 And test 6 is a real simple test. 385 00:18:40,590 --> 00:18:43,080 You can see it again right over here, there's only one line of output. 386 00:18:43,810 --> 00:18:47,030 There is another algorithm called adjacent find. 387 00:18:47,830 --> 00:18:52,270 And what it does it searches through a container and returns the first 388 00:18:52,270 --> 00:18:54,709 occurrence of 2 adjacent values. 389 00:18:55,330 --> 00:18:58,480 Now these 2 adjacent values are compared, right, with the equality 390 00:18:58,480 --> 00:19:01,650 operator, which we would overload if we had our own classes, but 391 00:19:01,650 --> 00:19:02,860 here we're using integers again. 392 00:19:03,059 --> 00:19:04,210 So you can see here it is. 393 00:19:04,210 --> 00:19:06,270 Right here, I've got 2 3s, one right next to another. 394 00:19:06,320 --> 00:19:07,210 That's what I expect. 395 00:19:07,420 --> 00:19:10,299 So this also returns an iterator. 396 00:19:10,549 --> 00:19:15,639 I'm using auto and I'm saying adjacent equals std adjacent 397 00:19:15,640 --> 00:19:17,600 find from beginning to the end. 398 00:19:17,610 --> 00:19:20,370 Give me the first occurrence of any 2 adjacent items. 399 00:19:20,740 --> 00:19:22,729 It's possible there are no adjacent items. 400 00:19:22,730 --> 00:19:26,989 So I would check if adjacent is not equal to array end, I found something. 401 00:19:27,259 --> 00:19:30,120 If it is equal to array end, there were no adjacent elements. 402 00:19:30,340 --> 00:19:31,930 So in this case, we found them. 403 00:19:31,960 --> 00:19:33,856 And there's the iterator, which we dereference. 404 00:19:33,856 --> 00:19:37,730 And you can see right here adjacent element found with value 3. 405 00:19:38,880 --> 00:19:41,310 Pretty cool and pretty easy and pretty powerful as well. 406 00:19:41,349 --> 00:19:43,850 Hopefully, you're starting to see the real power of the stl. 407 00:19:44,490 --> 00:19:45,660 It's actually awesome. 408 00:19:45,929 --> 00:19:46,899 You can do so much with it. 409 00:19:47,190 --> 00:19:48,330 All right, test 7. 410 00:19:48,670 --> 00:19:50,830 Here's the output for test 7 right here. 411 00:19:51,090 --> 00:19:51,659 Real easy. 412 00:19:51,660 --> 00:19:53,220 We're using the accumulate algorithm. 413 00:19:53,260 --> 00:19:55,030 We've used this before as an example. 414 00:19:55,200 --> 00:19:57,800 In order to use this, you have to include numeric. 415 00:19:59,200 --> 00:20:00,110 Okay, it's not an algorithm. 416 00:20:00,360 --> 00:20:01,310 It's an included numeric. 417 00:20:01,410 --> 00:20:02,469 So make sure you do that. 418 00:20:02,469 --> 00:20:07,440 And all it does is it accumulates from this iterator to that iterator, 419 00:20:08,070 --> 00:20:09,300 and this is where it starts. 420 00:20:09,300 --> 00:20:11,590 If you started at 10, it would start adding from 10. 421 00:20:12,170 --> 00:20:13,550 In this case, I'm starting from 0. 422 00:20:13,940 --> 00:20:19,990 What's really important here is that this type and this type and this type 423 00:20:20,360 --> 00:20:23,370 are the same, so double double 0.0. 424 00:20:23,379 --> 00:20:24,610 Make sure they're all the same. 425 00:20:24,610 --> 00:20:27,650 Otherwise, you can run into some really, really weird little errors. 426 00:20:28,010 --> 00:20:28,750 We do this. 427 00:20:29,000 --> 00:20:32,399 It sums them up 1 plus 2 plus 3 plus 4 plus 5, we display it 428 00:20:32,400 --> 00:20:35,809 and we get the sum is 15, which is just what we would expect. 429 00:20:36,160 --> 00:20:39,259 And let's do a couple of more we're almost done with these. 430 00:20:39,460 --> 00:20:41,780 Hopefully, you're following along. 431 00:20:41,780 --> 00:20:43,960 I think you are because it's pretty straightforward stuff. 432 00:20:44,180 --> 00:20:46,430 The last 2 we'll do is the count and then we'll do a count 433 00:20:46,460 --> 00:20:48,480 if using a lambda in test 9. 434 00:20:48,730 --> 00:20:49,780 So here's test 8. 435 00:20:50,150 --> 00:20:52,980 This time I've got a std array of 10 integers. 436 00:20:52,980 --> 00:20:54,500 And again i called it array1. 437 00:20:54,500 --> 00:21:00,750 And in this case, I've got 1 2 3 4 5 6 3s. 438 00:21:01,220 --> 00:21:02,320 I just want to count them. 439 00:21:02,680 --> 00:21:04,220 So I'm going to use the algorithm count. 440 00:21:04,220 --> 00:21:07,730 So I'm going to say std count, they always use iterators 441 00:21:07,760 --> 00:21:08,889 from beginning to end. 442 00:21:08,950 --> 00:21:11,229 What am I looking for, the 3. 443 00:21:11,719 --> 00:21:12,069 That's it. 444 00:21:12,080 --> 00:21:14,299 What do I expect back, I expect back 6, right. 445 00:21:14,299 --> 00:21:15,470 I just counted them up here. 446 00:21:16,109 --> 00:21:17,439 So this is an integer. 447 00:21:17,449 --> 00:21:19,730 You get back the integer of the number of occurrences. 448 00:21:20,480 --> 00:21:22,870 It says I found 3 6 times. 449 00:21:22,870 --> 00:21:24,590 This would same thing we would expect. 450 00:21:25,870 --> 00:21:30,760 And then the last one is the count if, which is very similar to count except. 451 00:21:31,299 --> 00:21:33,829 It's using a predicate, and what we're going to do is we're going 452 00:21:33,849 --> 00:21:35,509 to supply a lambda expression. 453 00:21:35,840 --> 00:21:37,150 So here's what I've got. 454 00:21:37,389 --> 00:21:42,890 I've got arr1 again is 10 integers it's just std array, and those are the 455 00:21:42,890 --> 00:21:44,420 10 inches you can see them right here. 456 00:21:44,670 --> 00:21:46,520 And what I want to do is I want to find out how many 457 00:21:46,520 --> 00:21:49,010 numbers are between 10 and 200. 458 00:21:50,549 --> 00:21:53,410 So there's 1, there's 2, there's 3, there's 4. 459 00:21:53,410 --> 00:21:55,660 I don't want to include 200 in this case. 460 00:21:56,070 --> 00:21:57,620 So I expect back a 4. 461 00:21:58,040 --> 00:22:00,360 So what do we use we use count if. 462 00:22:01,030 --> 00:22:02,840 Where do we start, at the beginning. 463 00:22:02,900 --> 00:22:04,670 Where do we end, at the end. 464 00:22:05,330 --> 00:22:06,919 And what's the predicate. 465 00:22:06,980 --> 00:22:10,849 Well, I want to return true when x is greater than 10 466 00:22:10,930 --> 00:22:12,700 and x is less than 200. 467 00:22:14,520 --> 00:22:14,929 That's it. 468 00:22:14,930 --> 00:22:16,250 And there's my lambda expression. 469 00:22:16,250 --> 00:22:19,660 You can see my x is being passed into here for each element. 470 00:22:19,900 --> 00:22:22,469 So the 1 will be passed in here as x, and we're going to check 471 00:22:22,480 --> 00:22:26,510 that the 2, the 3 all the way from whatever our iterators say. 472 00:22:27,440 --> 00:22:29,780 So in this case how many matches we expect, 4. 473 00:22:30,309 --> 00:22:32,389 We'll display them right here and that's what you get 474 00:22:32,400 --> 00:22:33,750 right there in the output. 475 00:22:35,370 --> 00:22:37,470 Okay, so again, a lot of examples. 476 00:22:37,680 --> 00:22:40,350 I did a lot of examples on purpose because I want you to have a lot 477 00:22:40,350 --> 00:22:43,240 of examples on your end in the source code that you can play with. 478 00:22:43,690 --> 00:22:45,940 And as I said, you're probably going to get tired of seeing 479 00:22:45,940 --> 00:22:48,720 these examples, but the more examples you see the better. 480 00:22:49,500 --> 00:22:51,530 All right, so that covers std array. 481 00:22:51,720 --> 00:22:54,460 In the next video, we'll start talking about std vector, which 482 00:22:54,460 --> 00:22:55,770 you know a lot about already.