1 00:00:05,800 --> 00:00:08,800 In this video, we'll learn about c++ vectors. 2 00:00:09,600 --> 00:00:12,700 Suppose we want to store test scores for students in a class 3 00:00:13,400 --> 00:00:15,600 but registration hasn't started yet, 4 00:00:15,600 --> 00:00:17,900 so I don't know how many students I'll have in class. 5 00:00:18,700 --> 00:00:21,900 Also students drop and add as the semester progresses. 6 00:00:21,900 --> 00:00:26,000 How can I model this information using an array as we've seen to this point? 7 00:00:26,500 --> 00:00:29,400 Remember, c++ arrays are fixed in size 8 00:00:29,400 --> 00:00:32,000 and I need to specify a size when I declare them. 9 00:00:32,200 --> 00:00:33,800 So what are my options? 10 00:00:33,800 --> 00:00:37,680 Well, I can always set the fixed size of the array to a size large enough, 11 00:00:37,680 --> 00:00:40,680 so that it's unlikely I'll have more than that number of students. 12 00:00:41,280 --> 00:00:43,180 Many times this option is okay, 13 00:00:43,180 --> 00:00:45,620 but you'll probably make it too big and waste space. 14 00:00:45,620 --> 00:00:48,620 For example, we may have 30 students in the class 15 00:00:48,620 --> 00:00:50,980 but we allocated 50 students in the array. 16 00:00:51,380 --> 00:00:55,180 Not only have I wasted storage space but now I need to keep track of how many 17 00:00:55,180 --> 00:00:56,780 students are actually in the array. 18 00:00:57,880 --> 00:01:02,280 And of course sooner or later, you'll get that one student that exceeds the size of the array. 19 00:01:03,780 --> 00:01:08,080 So what do we do? Well, we can use a dynamic array, such as a vector 20 00:01:08,080 --> 00:01:09,680 to solve some of these problems. 21 00:01:11,680 --> 00:01:16,280 A c++ vector is part of the c++ standard template library. 22 00:01:16,530 --> 00:01:20,890 This is a library of powerful containers, algorithms, functions and iterators. 23 00:01:20,890 --> 00:01:24,390 This means that we have available to us pre-written, 24 00:01:24,390 --> 00:01:26,890 pre-tested, easy to use components 25 00:01:26,890 --> 00:01:28,890 that we can use to help us solve our problems. 26 00:01:29,690 --> 00:01:34,090 We'll go over the main components of the standard template library toward the end of this course. 27 00:01:34,990 --> 00:01:38,350 We've talked about c++ being an object-oriented language. 28 00:01:38,550 --> 00:01:43,250 Well, when we create a c++ vector, we are creating a c++ object. 29 00:01:43,650 --> 00:01:46,950 And we can ask the object to perform operations for us. 30 00:01:47,750 --> 00:01:51,450 Vectors can grow and shrink in size at runtime, so it's a perfect choice 31 00:01:51,450 --> 00:01:52,750 to model my students. 32 00:01:53,410 --> 00:01:57,290 Vectors also provide syntax and semantics similar to arrays. 33 00:01:57,290 --> 00:02:00,990 So now that we've learned about arrays, understanding vectors will be pretty easy. 34 00:02:01,650 --> 00:02:03,950 In addition to the array-like syntax, 35 00:02:03,950 --> 00:02:07,550 we can also use lots of really powerful functions like sort, 36 00:02:07,550 --> 00:02:10,550 reverse, erase, find and more. 37 00:02:11,150 --> 00:02:15,750 Vectors also give us the ability to use methods that provide bounds checking if we wish. 38 00:02:15,750 --> 00:02:17,750 Let's see how we can declare vectors. 39 00:02:18,550 --> 00:02:20,550 There are several ways to declare a vector. 40 00:02:21,100 --> 00:02:25,500 The syntax for declaring a vector changes slightly from that of declaring an array 41 00:02:25,500 --> 00:02:27,300 because vectors are objects. 42 00:02:27,300 --> 00:02:29,900 First, we must include the vector library. 43 00:02:30,300 --> 00:02:33,900 Also the vector type is part of the standard library, 44 00:02:34,260 --> 00:02:36,560 so we must either use the namespace 45 00:02:36,560 --> 00:02:39,260 or use standard and the scope resolution operator. 46 00:02:40,060 --> 00:02:43,960 We can create a vector of any type we want, just like we did with arrays. 47 00:02:43,960 --> 00:02:45,760 Here's the syntax for vectors. 48 00:02:45,760 --> 00:02:49,960 Don't worry at this point what the differences are. We'll cover this in a future section 49 00:02:49,960 --> 00:02:53,260 since the vector is an object-oriented template class. 50 00:02:53,260 --> 00:02:57,760 We must include the type of the elements of the vector inside the angle brackets. 51 00:02:58,010 --> 00:03:02,610 So for example, vowels is a vector or a collection of characters. 52 00:03:03,410 --> 00:03:06,310 Test scores is a vector of integers. 53 00:03:06,710 --> 00:03:10,810 Both these examples create an empty vector that contains no elements. 54 00:03:11,010 --> 00:03:13,610 Let's look at a few other ways to declare vectors. 55 00:03:13,910 --> 00:03:17,360 In the first example, we declare vowels to be a vector containing 56 00:03:17,360 --> 00:03:18,360 five characters. 57 00:03:18,860 --> 00:03:23,160 In this case, I'm not providing an initializer list. We'll do that in the next slide. 58 00:03:23,460 --> 00:03:26,960 Instead, I'm using a constructor initialization syntax 59 00:03:26,960 --> 00:03:29,560 which provides information inside parentheses. 60 00:03:30,160 --> 00:03:32,860 This tells the compiler that you want 5 characters. 61 00:03:33,260 --> 00:03:36,460 In the second example, we declare a vector named test scores 62 00:03:36,460 --> 00:03:37,660 of 10 integers. 63 00:03:38,160 --> 00:03:42,260 Unlike arrays, these ten integers will be automatically set to 0. 64 00:03:42,510 --> 00:03:44,810 I don't have to do that explicitly myself. 65 00:03:45,810 --> 00:03:49,010 We can also use initializer lists as we did with arrays 66 00:03:49,010 --> 00:03:51,010 to initialize vector elements. 67 00:03:51,010 --> 00:03:55,310 In this first example, we declare vowels to be a vector containing 5 characters. 68 00:03:55,310 --> 00:03:59,310 In the first example, we declare and initialize the vowels vector to 69 00:03:59,310 --> 00:04:01,310 the characters a, e, i, o, u. 70 00:04:01,910 --> 00:04:05,710 Note that there are single quotes around the character literals, 71 00:04:06,210 --> 00:04:07,510 not double quotes. 72 00:04:07,510 --> 00:04:11,870 In the second example, we declare and initialize a vector named test scores to the integers 73 00:04:11,870 --> 00:04:15,470 100, 98, 89, 85 and 93. 74 00:04:16,670 --> 00:04:17,899 In the third example, 75 00:04:17,899 --> 00:04:20,700 notice that there are two values in the parentheses. 76 00:04:20,700 --> 00:04:24,600 The first value or parameter is the initial size of the vector. 77 00:04:24,600 --> 00:04:26,960 In this case 365. 78 00:04:27,560 --> 00:04:30,060 The second value is what you want to initialize 79 00:04:30,060 --> 00:04:32,360 all the 365 doubles to. 80 00:04:32,860 --> 00:04:37,220 In this case, we're declaring a vector of 365 doubles 81 00:04:37,220 --> 00:04:41,650 and we're initializing all of those doubles to 80.0 degrees fahrenheit. 82 00:04:41,950 --> 00:04:44,250 There are also many other ways to declare vectors. 83 00:04:44,250 --> 00:04:48,450 You can declare a vector to be a copy of an array or a copy of another vector. 84 00:04:48,450 --> 00:04:52,050 I think you're beginning to see how powerful vectors can be compared to arrays. 85 00:04:53,710 --> 00:04:56,070 So let's do a quick recap about vectors. 86 00:04:56,770 --> 00:04:59,570 We learned that vectors are an object-oriented container, 87 00:04:59,570 --> 00:05:01,770 defined in the standard template library. 88 00:05:01,770 --> 00:05:04,470 We must include the vector include file 89 00:05:04,470 --> 00:05:07,070 to use them and they belong to the standard namespace. 90 00:05:07,870 --> 00:05:11,970 Like arrays, vectors allow you to tell the compiler to give you a collection of as 91 00:05:11,970 --> 00:05:15,570 many elements as you want and give the entire collection a single name. 92 00:05:16,370 --> 00:05:19,930 Vectors are dynamic in size which means they can grow and shrink 93 00:05:19,930 --> 00:05:21,530 as needed at runtime. 94 00:05:21,530 --> 00:05:25,130 Like arrays, vector data is stored contiguously in memory. 95 00:05:25,790 --> 00:05:28,390 Vector elements are individually accessible. 96 00:05:28,750 --> 00:05:32,850 Vector indexes start at 0 and end at position size minus one. 97 00:05:33,350 --> 00:05:36,450 If you use the subscript operator, that's the square brackets 98 00:05:36,450 --> 00:05:38,250 to access vector elements, 99 00:05:38,250 --> 00:05:41,250 then vectors will provide no bounds checking. 100 00:05:42,150 --> 00:05:44,450 This provides the same behavior as arrays. 101 00:05:44,450 --> 00:05:49,050 However, vectors provide a rich set of functions that do provide type checking. 102 00:05:49,450 --> 00:05:52,450 As a programmer, you can decide what you need to use. 103 00:05:52,810 --> 00:05:56,210 Unlike arrays, when you declare a vector, the vector elements 104 00:05:56,210 --> 00:05:59,810 will automatically be initialized to 0 unless you specify otherwise. 105 00:05:59,810 --> 00:06:02,310 So if you declare a vector of 10 integers, 106 00:06:02,310 --> 00:06:05,970 these integers will be initialized to 0. They won't contain garbage. 107 00:06:06,570 --> 00:06:08,270 Vectors are very efficient. 108 00:06:08,670 --> 00:06:12,970 And finally, we commonly use iteration or looping to process vectors 109 00:06:12,970 --> 00:06:14,570 just like we do in arrays. 110 00:06:14,570 --> 00:06:17,770 Later in this section, we'll talk about c++ vectors. 111 00:06:18,270 --> 00:06:22,270 In the next video, we'll look at how we can access individual vector elements, 112 00:06:22,270 --> 00:06:25,630 and we'll also see a few of those useful vector functions I mentioned.