The Array Class

Contents

keyboard_tab
Introduction
1
Course Overview
PREVIEW1m 25s
Arrays and Vectors
2
Built-in Arrays
PREVIEW18m 31s
Conclusion

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 25m
Students
18
Ratings
5/5
starstarstarstarstar
Description

This course explores arrays and vectors which are types of data structures in C++. We'll focus on built-in arrays, which C++ inherits from the C programming language, and a couple of sequence containers including the array template class and the vector template class, which are part of the standard template library or STL.

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

To get the most out of this course, you should have a basic understanding of the fundamentals of C++.

 

Transcript

In the previous lecture, we looked at built-in arrays which are the older but still very often used construct with the syntax that C++ inherits from C. We discovered that arrays are fixed size homogeneous collections of elements meaning the elements they hold are of the same type. Also, with arrays, we can refer to multiple elements by a single name and reference the individual elements by their index. Additionally, we learned that the memory that is allocated for arrays is contiguous, that is, the blocks of memory allocated for each of the elements in the array are adjacent to one another. In this lecture, we look at the array class. It behaves very much like the built-in array but also has some additional features and also contains data and behaviors, making it more sophisticated than its built-in array counterpart. The array class is one of several container classes, specifically sequenced container classes that the C++ Standard Template Library or STL for short contains. The STL is full of all sorts of algorithms, data structures, data types, and other useful entities that can help us write code to be more robust. That is, to not break easily and also to write the same functionality more quickly. This section is not an exhaustive coverage of the STL but we discussed the array class in this lecture and the vector class in the next lecture because both are sequence containers. And pay very close attention because we do have some projects at the end of the section that I think you're going to enjoy and they do depend on arrays, the array class, and the vector class and also multidimensional arrays which we'll talk about a little later. So, let's get started working with the array class. Let's create a project called ArrayFunTest. So, I'm going to create a project, Empty project. We're going to call it ArrayFunTest, create, and I'm going to add a source file as usual and we will call it the filemain.cpp and then let's give it some skeleton here. array using namespace std; int main(). And we're going to create an array like this. So, array < int 5> and I'll explain the syntax in just a moment here. myIntArray;. Spell it correctly because case does matter and we're in C++. So, there we go and of course, main has a return 0. So, first we use special syntax for creating an array object because in C++, the class is what we call a Template which is a special type of class. If you're coming from another language like Java, you would probably know this as a generic. We use angle braces, which are the less than and greater than signs in other contexts that surround the type and in this case also, the size that the array object holds. With the array type, notice that the pieces of data inside the angle braces are called Template Parameters, template parameters. There are two of them here. The first of these is the Type Parameter which we have made an int so we can hold int. The second in this case is 5, which is the number of cells that the array should contain. That is, its capacity. How many elements it can hold. Since arrays are fixed size, this is why we must specify the size when we declare the array object. Now, if I immediately print it out, let's see what happens. So, we will run the application and you notice it does loop through the array and it does pronounce each of the elements in the array but this is wacky. This is a lot of weird numbers and this is again, the garbage data that had been mentioned before. This is just stuff residing in memory locations reserved for the array but they don't really have any meaning. We want to avoid garbage data so it's useful to initialize the data in an object such as, an array object. The easiest way to do this is with what's called an Initialization List. So, let's set the elements of our size five array to zero. So, here might be one way we would do it. So, {0, 0, 0, 0, };. That would be one way we could do it. And let's run it again and verify that all the elements are zero and in fact, they are. So, you see all are zeros. There's five of them. That's pretty good but it could be tedious if we have an array of say size 100 or 1000 or more so, there's thankfully shortcut syntax. So, let's check this out. You actually can do this. If you just leave the curly braces empty here next to the array, it will make it so that it will initialize the elements to zero. So, let's see what happens. Same thing as we got before and we haven't had to list each of the elements so that's much better. Now, whether we have five or 5000 elements, the array will be populated by that many zeros, however many we need. You can technically put any value in here less than the capacity of the array and it will fill zeroes in for the remaining elements. So, if we listed 1, 2 in the curly braces, there would still be three cells not accounted for. Those would get the value zero automatically. So, if we do that, just to show {1, 2}. So, even though this is size five, I've only listed two elements in the initialization list and we can see that it prints out 1, 2, first two elements at index zero and one and then the last three are automatically filled in with zeros because we used an initialization list. Now, how might we change the elements once they are initialized? So, I'm going to remove these, this 1 and this 2. You can access the elements of an array object the same way you access the elements of a built-in array, you use the square brackets, that is, the array index operator. Let's add some code to our ArrayFunTest project so that we can change the values of the array elements are initialized too. I'm also going to print out the size of the array after we print out its elements using the size member function also called the Size Method. You'll recall that I said that objects unlike the built-in array, this is an object so it will have behaviors that it can exhibit such as, telling you its size and different things like that. So, myIntArray[0] = 2; myIntArray[1] = 5;, myIntArray[2] = 10;, myIntArray[3] =1;. myIntArray[4] = 17;. So, we could do it this way. Even if we've initialized it with something else, we can change the values this way. So, I'm also going to near the bottom. Before the return, I'm going to put << "Size of array: << intArray.size() << endl;. There we go and we're going to run it. Once it compiles, we are all set. And it prints out 2, 5, 10, 1, 17. No shock there. And it says the size of the array is five because there's five elements. So, you can see the data in the array now matches the values that we set and that's pretty cool. So, we have control over this and we can change the values later on if we want to even more. Before moving on to vectors, I have a quick challenge for you. I'd like you to create a project called Twice Numbers, create an array class and sense of size 10 and I want you to use a regular for loop to initialize it with the multiples of through 9 and 2. For example, the first few numbers would be zero since 0*2=0, two, since times 2*1=2 and 4 since 2*2= 4. And, of course, six because that's three times two. Do not store these values manually by writing them out one by one like we did and here's your big hint. Consider what the loop control variable such as, i within a for loop can do to help you get your solution. You must use your knowledge of for loops and you can use the size member function on the array to control when the loop stops. Don't forget to use a range based for loop to print out the elements you store in the array near the end of the main function. So, pause the video and come back when you're finished or if you have a difficult time with the challenge. Did you solve that challenge? Great job if you were able to. Even if you weren't, that's okay, let's do it together and see how we might do it. So, we're going to create this twice numbers project. I'm goint to close this. Let's create a new project, Empty project. Next, I'm going to call it TwiceNumbers. And create our nice little file here. There we go. main.cpp < iostream >. We're also going to use array for our STL array and return 0, of course. Now, we want 10 elements so array<int, < int, 10>. Call myNums; or whatever you want to call it. And we used int because it was supposed to be integers and we will first populate it using a regular for loop and the size method to protect our boundary. So, I'm going to do this and i = 0;, i < myNums.size();. So, that's going to return the 10 and i++ end for loop myNums[i]. We're going to assign twice the index each time because we want to make it through 9 and then each of those numbers are going to be twice as large. So, the first time through the loop it will be 0*2 which is 0, then 1*2 which is 2, and 2*2 is 4, 3*2 is 6 etc. That's all we really need. So, we can actually calculate the value for each of these just like any variable on the fly as we need them. So, to print them out though, the requirements said we should use an enhanced or range based for loop and that's what we're going to do here and again you can use whatever valid and useful name you come up with. So, let's run it. So, we got 0, 2, 4, 6, 8, 10, 12, 14, 16, 18. It does, in fact, multiply the values by 2 and store those in the array and then we print out the elements in the array. So, what number is it multiplied by 2. Again, that is the index where we're using the loop iteration control variable to be used as an index and also storing that index times two at each index so, that should make sense that the element at zero contains 0*2, the element at one contains 1*2, the element at two contains 2*2, etc and then we print them out using the range based for loop. So that's great. So, that's awesome. We populated the arrays with multiples of two from through 10 exclusive using a regular fore loop and then we printed out the elements with a range based for loop. Very cool. In the next lecture, we'll look at a class similar to the array class called the Vector Class which resizes automatically to accommodate new elements. I'll see you in the next lecture.

About the Author
Students
297
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics