You could write your own code to find something in a collection. But why? Use algorithms instead.
We use algorithms all the time and change them whenever needed. You might not even realize how many algorithms you use each day. An algorithm is a series of steps to follow. That’s it really.
I have an algorithm for brushing my teeth. And I recently had to change it when I bought a different brand of toothpaste. I normally like the toothpaste with a flip cap. I have tremors and don’t need to worry about dropping a cap that flips open. But I recently bought a tube with a screw-on cap and had to change everything. I now have a new place to set the tube down. And I switch hands to put the cap back on because my right hand shakes less.
You might want to disagree and say these aren’t algorithms. That you don’t have steps you follow. I say to look closer. How do you pick up your cup? Do you have a favorite chair to sit in. Do you have a routine each night to fall asleep? Do you tend to watch the same TV shows each day? Is there a certain way you walk that your friends would recognize? Which shoe do you normally put on first? Do you write or throw a ball with just one hand?
Now introduce something to change things up a bit and watch how slow you become. Try to take a different path to work one day but give yourself some extra time or you’ll be late. Try to put your cup on the other side of your plate but be careful not to spill it. Try to move your furniture but be careful not to stub your toes.
We don’t just have algorithms. We rely on them. Without them, everything would be done slowly and deliberately as if we were stuck doing things for the first time.
Back to programming, whenever you write a method, you’re bundling up a series of steps and giving those instructions a name. This is technically an algorithm. But it’s not what we normally think of as an algorithm. Maybe that’s why we don’t think of our own internal programming as an algorithm either.
The things we normally recognize in programming as algorithms tend to be widely applicable. If you have a binary tree structure and need to keep it balanced, how you go about this is an algorithm. If you have a list of items and need to add a new item to the middle of the list, then how you go about doing this is an algorithm.
You’ll find that a lot of algorithms are designed to work with collections. C++ has a header file called algorithm that contains about 85 algorithms and each one is designed to work with the Standard Template Library collection classes. These are what C++ programmers normally think of when discussing algorithms.
The purpose of these algorithms is not really to do things faster like how we rely on internal algorithms to make our daily tasks faster. The main purpose is to provide a consistent and replaceable way to do things.
So if you want to sort a list or a vector, you have a common and consistent way to do this. Once you learn how sorting works, then not only can you sort different collection types, but you can start using algorithms together. In other words, they can stack up to give you even more capabilities.
There’s no way to go through all these algorithms in one or even two classes. So I picked out five of the more useful algorithms that you’ll likely need more often than the others.
This class is divided into two parts.
The first part will show you how to use the find and search algorithms.
This will let you locate items you want within collections. What you do with the items once you find them is up to you.
And the second part will show you how to use the copy, erase, and unique algorithms.
This will let you copy items to other locations and do things like removing duplicate items from a collection.
You might wonder why you would have duplicate items in the first place? I mean, if you don’t want duplicates, then why let duplicates into your collection in the first place? Well, one good example is when you want to combine items from two or more collections into a single collection. It’s possible that each original collection had no duplicates. But when you combine them into a single collection, you now have duplicates. You might write your code so that you allow these duplicate items temporarily knowing that you’re going to remove them once they’re all in one place.