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