 Hey, everybody, it's Brian, and in this episode, we're going to talk about the algorithm QDeleteAll, which deletes all items in a range using the C++ delete operator. Now why do we need this? For example, let's say we want to work with a list of QObjects. Remember a QObject cannot be copied. So if we're going to put this in a container, we have to put a pointer to it in the container. Pointers, of course, come with the overhead that we manage the memory. QDeleteAll makes that super simple. Here's a great example. You can delete the entire container, and then you have to clear it out. The major caveat here is this will not remove the items, just free up that memory. For this example, I've already got some code written. We have just a simple test class as a constructor and a deconstructor. And if we switch over here, you can see we're just going to QInfo out, constructed and deconstructed so we can see that object's lifecycle. Let's dive in and take a look. First thing we're going to do here is get a list of pointers. So we've got that test class, and we're going to put that into a list. And because it's a QObject, we have to work with pointers. To make life super, super simple, I'm going to do a type def. If you're still new to C++, basically, this allows us to make some type of alias. So I'm going to say QList. And we want the test class, and we're going to be working with pointers. So we're just making a list of pointers. And we're going to call that test list. So basically, we're kind of like pseudo creating our own data type here. That being said, whenever we use test list, you guessed it, it's going to use this instead. Makes it super convenient so we don't have to type this out over and over again. Now from here, let's go ahead and say we want to make a function that's going to return our test list. And let's call it getList. Now inside of this, we're going to, you guessed it, make a test list, call it list. And let's just generate some pointers here. So I'm going to say four, and we'll hit i equals zero. i is less than, and I'm just going to arbitrarily pick a number five. And we're going to say i increment. So all we're going to do now is just append. So I'm going to say list.append. What we're going to add here is a new test class. Now just in case you skip the intro, our test class just simply has queue info in the constructor and deconstructors so we can see that object's lifecycle. That's really all we want to do is see when this is created and when it's destroyed. Now for convenience here, I'm going to say list.last. So we're going to get the last item in that list, the one we literally just added. And I'm going to set object name. That way when we print the object out, we can tell which one it actually is instead of just seeing a memory address. I'm going to say this is test. Now let's go ahead and make a queue string representation of our number. Now that we have that list, we simply need to return it. Special note, queue list is not a queue object. So we can simply return it and we're doing a copy here. We don't have to worry about that overhead that we have with queue object where we simply can't copy it. Now that we're able to generate a list of test classes, we want to really conveniently display it. So I'm just going to make a simple function. We're going to display a list of pointers here. So what we're going to do is say void display. And we want our test list type def there. Let's call this list. And we're just going to use the for each keyword. We're going to say for each test pointer. Let's call this item in our list. That's just going to go through each item in the list and then allow us to, you guessed it, just print it out. Super simple. Now we have a subtle problem here. Notice we're not testing to see if this pointer is even valid. We're just directly working with it. That could cause problems later as we're going to demonstrate. Now that we can get a list and display it, let's see this in action. So we are going to first things first, say test list, and we want a list here. And we're going to call our get list function, which is going to return that list. Now from here, we can go ahead and display it. So I'm just going to display our list. Notice there's a whole lot of copying going on. Generally in C++, I dislike copying, but I'm doing this for a reason, just to show you that we can copy that queue list without any problems whatsoever. So let's just go ahead and demonstrate that. And there we go. So we have a problem. You see how we've got all of these constructed. In case you're wondering what this object zero zero is, that's the parent in the background. It doesn't have a parent. So this will not follow the parent child relationship or that object tree, meaning we're not going to use queues built in automatic memory management. We have a memory leak. We've got these items that are just going to exist in memory until we destroy them. You can see here where we're displaying them, but they're not being deleted. Oh, okay, this is a problem, and this is a normal C++ problem. So let's look at a possible solution. First thing we're going to do is we're going to say queue info. And let's say we want to delete them. So we're going to use queue delete all. Now this is an algorithm baked right into queue. There is a little bit of a problem here. A lot of the algorithms are being deprecated, meaning you just really shouldn't use them. Why? Because a lot of the algorithms that are in queue didn't exist in the C++ standard library or dare I say the C++ standard library was subpar. That being said, a lot of that is really changing. So I put a link right here. Note that the standard template library functions starting with queue five are pretty much starting to be phased out with the exception of course, queue delete all. Now there is more than likely something out there and you can reimplement this very easily, but it's already baked into queue and it's ridiculously simple. So this method is going to do the entire list. Let's go ahead and just demonstrate that. Now you see we have the full object lifecycle. Our application is still running. You see this little cursor blinking, but we've demonstrated that we have using that algorithm destroyed those objects. But we have another issue that we have just created and this is unfortunate. Those items are not removed from our list. So if we try to use them again, we have a different issue. We have a bad pointer or what's called a dangling pointer, meaning that is a bad pointer and it will, of course, crash our application. And this gets a little bit well insane trying to figure out what just happened. In case you're curious, this is what's going on right here. We're not testing this pointer before we use it. We're just saying, hey, I'm trusting it and guess what? This is a bad pointer and program goes boom. So every time you use Q to lead all, you're going to want to clear that list. You may be asking, well, why doesn't the algorithm just do it for me? Well, there's a reason for that. You may not want that functionality and that may introduce other issues. So you as the programmer are expected to know what to do. So once we've now cleared that list, let's go ahead and save run and it should run perfectly fine. Ta-da! No crash and our objects have gone through their full life cycle. Now there is another way. So I'm going to comment this out. And when I do Q to lead all, you see we are at one of two. And the first one wants a container, the second one wants a forward iterator and beginning and an end. That's right, so you can do a chunk of a list if you wanted to. I'm just going to pretty much do the same thing we just did using this way. So I'm going to say LST dot, whoops, list dot begin and list dot end. So if you had special positions, you could go out and then start a custom beginning and custom ending and delete just a chunk of this list. But using this does basically the same thing as just clearing the entire container because we're starting at the beginning and then going to the end. Let's go ahead and demonstrate that. And there you go. It's really that simple. I'd love to have a long exhaustive conversation about this, but Q to lead all is just, well, very simple, very easy to use. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the Voidromes Facebook group. This is a large group with lots of developers. And we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.