1 00:00:00,350 --> 00:00:07,270 C plus plus 11 introduce another way to use the for loop, which is intended to be used with the containers. 2 00:00:07,280 --> 00:00:11,930 So the C plus plus standard library contains templates for the container classes. 3 00:00:11,960 --> 00:00:18,430 These classes contain a collection of objects and provide access to those items in a standard way. 4 00:00:18,440 --> 00:00:23,790 The standard way is to iterate through a collection using an iterator object. 5 00:00:23,810 --> 00:00:30,260 More details about how to do it is will begin in the next lecture here, but this syntax requires an 6 00:00:30,260 --> 00:00:34,710 understanding of pointers and iterators so we will not cover them here. 7 00:00:34,730 --> 00:00:42,050 So the range based for loop gives a simple mechanism to access items in a container without explicitly 8 00:00:42,050 --> 00:00:43,490 using iterators. 9 00:00:43,490 --> 00:00:47,390 So the syntax is simple here for here, for loop. 10 00:00:47,390 --> 00:00:50,300 Here for declaration. 11 00:00:51,770 --> 00:00:52,940 An expression. 12 00:00:53,590 --> 00:00:56,860 And after that, the loop statement begins. 13 00:00:57,070 --> 00:00:58,960 Loop statement here. 14 00:00:58,990 --> 00:01:05,590 So the first thing is to pointed out that there are only two expressions and they are separated by the 15 00:01:05,590 --> 00:01:06,100 colon. 16 00:01:07,030 --> 00:01:12,790 So the first expression is used to declare the loop variable, which is of the type of the items in 17 00:01:12,790 --> 00:01:14,890 the collection being iterated throughout. 18 00:01:14,890 --> 00:01:21,280 And the second expression is gives an access to the collection in the C plus plus times. 19 00:01:21,280 --> 00:01:28,810 The collection that can be used are those that define a begin and end function that gives a access to 20 00:01:28,810 --> 00:01:34,780 iterators and also stack based arrays and the set based arrays that the compiler knows the size of. 21 00:01:34,780 --> 00:01:42,670 Here, the standard library defines a container object vector here in this here, let's create a vector 22 00:01:42,670 --> 00:01:51,010 and the vector template is a class that contains items of the type specified in the angled braces like 23 00:01:51,010 --> 00:01:53,110 that here vector. 24 00:01:55,390 --> 00:01:58,280 And we're going to create a string vector here. 25 00:01:58,300 --> 00:01:58,960 String vector. 26 00:01:58,960 --> 00:02:05,510 As you can see, we got an error because we have to include the vector library here. 27 00:02:05,530 --> 00:02:07,820 Import vector library, as you can see here. 28 00:02:07,840 --> 00:02:10,930 So we imported vector library and. 29 00:02:14,220 --> 00:02:19,890 So, you know, the vector is initialized in a special way that is new to a C plus plus 11. 30 00:02:19,890 --> 00:02:22,620 And this is called the list initialization. 31 00:02:22,620 --> 00:02:29,730 So this syntax allows you to specify the initial values of the vector in a list between curly braces 32 00:02:29,730 --> 00:02:30,180 here. 33 00:02:30,180 --> 00:02:38,520 So let's make an my cars variable and make it, for example, BMW, M. 34 00:02:39,990 --> 00:02:41,280 Fourth here. 35 00:02:43,360 --> 00:02:46,450 Portiere Fiat here. 36 00:02:47,200 --> 00:02:49,630 Volkswagen we're going to add here. 37 00:02:51,430 --> 00:02:52,570 Volkswagen. 38 00:02:52,750 --> 00:02:57,070 And we're going to add another Audi here. 39 00:02:57,490 --> 00:02:58,210 Here. 40 00:02:58,920 --> 00:03:03,690 And we're going to create a for loop for integer E. 41 00:03:04,820 --> 00:03:05,600 Zero. 42 00:03:05,630 --> 00:03:15,680 While Integer is less than my cast size and increase e by one and see out. 43 00:03:16,860 --> 00:03:17,250 Uh. 44 00:03:17,280 --> 00:03:18,000 Okay. 45 00:03:18,740 --> 00:03:20,770 See out my cars. 46 00:03:20,780 --> 00:03:23,200 Dot add e. 47 00:03:24,690 --> 00:03:26,250 And end line. 48 00:03:28,950 --> 00:03:30,030 And line here. 49 00:03:33,790 --> 00:03:34,510 So. 50 00:03:36,390 --> 00:03:37,230 Here. 51 00:03:39,000 --> 00:03:49,200 The vector class has a member of called member function called size that the called through the dot 52 00:03:49,200 --> 00:03:55,860 operator here which means the call this function on this object that returns the number of items in 53 00:03:55,860 --> 00:03:56,550 the vector. 54 00:03:56,550 --> 00:04:00,060 So each item is accessed. 55 00:04:01,160 --> 00:04:03,440 Using add function here. 56 00:04:04,240 --> 00:04:06,880 Passing the object index. 57 00:04:07,600 --> 00:04:11,550 The one big problem with this code is that it uses random access. 58 00:04:11,560 --> 00:04:15,790 That is, it accesses each item using its index. 59 00:04:15,790 --> 00:04:22,990 So this is a property of vector, but standard library container types do not have random access. 60 00:04:23,350 --> 00:04:27,100 Now we're going to create another range based loop here. 61 00:04:29,840 --> 00:04:31,020 Arranged baseball. 62 00:04:31,050 --> 00:04:32,520 This is going to be a range based loop. 63 00:04:34,420 --> 00:04:35,470 Based. 64 00:04:36,950 --> 00:04:41,420 Slope here and let's create the previous example we did here. 65 00:04:41,420 --> 00:04:48,770 So for string model, my car model. 66 00:04:50,390 --> 00:04:53,330 Oh, yes, my car model here. 67 00:04:54,970 --> 00:04:56,320 And my car's. 68 00:04:59,430 --> 00:05:00,870 And see out. 69 00:05:03,180 --> 00:05:07,320 My car model and inline. 70 00:05:07,500 --> 00:05:08,310 So. 71 00:05:09,590 --> 00:05:11,390 Let's print it again. 72 00:05:13,160 --> 00:05:13,940 And. 73 00:05:14,800 --> 00:05:16,120 Here say out. 74 00:05:17,810 --> 00:05:19,100 Uh, just to make. 75 00:05:19,100 --> 00:05:20,180 Make it look nice. 76 00:05:23,350 --> 00:05:24,460 Look here. 77 00:05:33,690 --> 00:05:34,350 Here. 78 00:05:34,710 --> 00:05:38,190 So this is the first example and this is the second. 79 00:05:38,280 --> 00:05:41,580 So actually the output is the same. 80 00:05:41,910 --> 00:05:47,350 They look, they look same, but each item is accessed using the add function here. 81 00:05:47,370 --> 00:05:53,460 But in this leg here, the we just use the range based loop here. 82 00:05:56,400 --> 00:06:01,110 So the standard library containers type, as I said earlier, do not have random access. 83 00:06:01,110 --> 00:06:05,430 And this for loop uses the range based for here. 84 00:06:05,700 --> 00:06:09,130 So actually we can create another work. 85 00:06:09,300 --> 00:06:16,020 So as you know, this type here works with any of the standard container types and for arrays allocated 86 00:06:16,020 --> 00:06:16,950 on the stack here. 87 00:06:16,950 --> 00:06:18,900 So we're going to create another here. 88 00:06:19,290 --> 00:06:27,120 Um, let's make it my car years here, my cars here. 89 00:06:28,410 --> 00:06:28,890 Here. 90 00:06:29,250 --> 00:06:31,980 And, for example. 91 00:06:33,210 --> 00:06:33,560 Um. 92 00:06:34,770 --> 00:06:36,030 2002. 93 00:06:36,060 --> 00:06:37,500 2005. 94 00:06:38,340 --> 00:06:39,960 1989. 95 00:06:41,230 --> 00:06:42,000 Okay. 96 00:06:42,240 --> 00:06:43,380 And how many? 97 00:06:43,440 --> 00:06:44,670 Yeah, we have five. 98 00:06:44,700 --> 00:06:46,350 Let's create a five to make it. 99 00:06:46,890 --> 00:06:51,090 Which are we going to print it here or make it for where you want to? 100 00:06:51,420 --> 00:06:53,130 Uh, you want access here. 101 00:06:53,910 --> 00:06:55,740 And 2010. 102 00:06:57,140 --> 00:07:01,190 So and let's print it out using this range based for. 103 00:07:01,520 --> 00:07:04,550 For loop here, for integer. 104 00:07:05,030 --> 00:07:06,830 Uh, here, here. 105 00:07:07,010 --> 00:07:10,820 And my cars here and here. 106 00:07:10,850 --> 00:07:15,560 C out my here and. 107 00:07:16,100 --> 00:07:16,900 And line. 108 00:07:19,750 --> 00:07:20,170 Here. 109 00:07:20,200 --> 00:07:22,120 As you can see, we printed this as well. 110 00:07:22,120 --> 00:07:28,450 In this case, the compiler knows the size of the array because the compiler has allocated the array 111 00:07:28,450 --> 00:07:29,050 here. 112 00:07:29,050 --> 00:07:31,360 And so it can determine the range. 113 00:07:31,390 --> 00:07:36,880 The range based for loop will iterate through all the items in container. 114 00:07:36,880 --> 00:07:43,720 But as with the previous version, you can leave the for loop using the break, return, throw or go 115 00:07:43,720 --> 00:07:44,200 to here. 116 00:07:44,200 --> 00:07:51,820 So and you can indicate that the next loop should be executed using the condition continue statement 117 00:07:51,820 --> 00:07:53,950 here so we can make it like that here. 118 00:07:54,820 --> 00:07:55,480 Okay. 119 00:07:55,480 --> 00:07:56,380 If. 120 00:07:57,990 --> 00:07:58,410 Make it. 121 00:07:58,560 --> 00:07:59,970 Let's make an if statement. 122 00:07:59,970 --> 00:08:01,710 Write an if statement here. 123 00:08:01,950 --> 00:08:02,760 So. 124 00:08:03,850 --> 00:08:12,400 If our year equals to two 1989, make it just an. 125 00:08:13,480 --> 00:08:14,050 Okay? 126 00:08:14,320 --> 00:08:16,000 No, actually. 127 00:08:16,870 --> 00:08:17,100 It. 128 00:08:17,230 --> 00:08:21,070 Uh, the year car is too old. 129 00:08:21,100 --> 00:08:21,970 See out. 130 00:08:23,090 --> 00:08:24,920 Car is to. 131 00:08:26,460 --> 00:08:26,940 All. 132 00:08:28,120 --> 00:08:29,590 Old classic. 133 00:08:31,000 --> 00:08:32,440 We'll make it classic car. 134 00:08:32,740 --> 00:08:36,700 And of course, we have to end line here to make it look nice. 135 00:08:36,700 --> 00:08:41,740 And else here, just print the year of the car here. 136 00:08:42,010 --> 00:08:43,210 Make it here. 137 00:08:43,210 --> 00:08:55,090 As you can see here, when it comes to the 1989, it shows the if statement is executed and else the 138 00:08:55,120 --> 00:08:58,300 C out here is just printed to the screen here.