To see the youtube video explaining this topic, click here.
Introduction: The Standard Library Algorithms
The C++ Standard Template Library has a bunch of functions that are useful to know, since expert programmers designed them to be efficient. You should use them every opportunity you get.
I've written a cheat sheet on github for the STL algorithms, but you need a bit of starting information to be able to use them effectively. You need to know about Pairs, Lambda Expressions, and Iterators. In this blog, I'll review them briefly so that you can use the STL algorithms effectively. C++ Pairs
Pairs are just mini containers provided by the standard library to group two variables (of any type) together.
The output of the above program is "5 srcmake". You can initialize a pair with the make_pair function, and you can access the elements by calling the variable name's first and second properties.
C++ Lambda Expressions
A lambda expression is literally just a different way of writing a function. For example, a lambda expression that checks if a number is positive is the following:
The lambda expression is named "srcLambda", it's type is "auto", and it's equal to that thing on the right.
The brackets [] capture any variables you need that exist outside of the lambda expression. The parenthesis accept whatever input the lamba expression is given, like a normal function. The inside of the lambda function does some work, and possibly returns a value. We need to pass lambda expressions to certain STL algorithms to do certain work. For example, to check count how many elements in a vector are greater than 2, we could use a lambda expression and the "count_if" algorithm.
For each element of "v", "count_if" will use call the lambda expression on that element. If the lambda expression returns "true", then count_if will add one to it's counter, and give the final answer to the "count" variable.
It's much simpler to have those lines of code than it would be to write our own loops and other variables. But what are those "v.begin()" and "v.end()" things? C++ Iterators
Iterators are just special variables that point to a certain element in a container. They seem useless for vector's since vectors can be accessed by index, but other container types (like stacks) don't have indices, and so iterators are used to indicate the place in the container.
The type of an iterator looks something like this: vector<int> v{ 1, 2, 3, 4, 5 }; std::vector<int>::iterator it = v.begin();
An iterator that's for a vector<int> container named "it" is equal to v's "begin()".
A vector's "begin()" will point to the first element in the vector (in this case, the element 1 (at index 0)), and a vector's "end()" will point to the element after the last element in a vector (in this case, an invisible element after 5, which would be at index 5). You can increment an iterator (move the position to the next element) with a simple ++ operation. it++;
You can also simply add to a beginning iterator to get the position you want too.
std::vector<int>::iterator it = srcVector.begin() + 4; // Accesses the 5th element in the container.
You can also subtract iterators (which can help you get a position's index).
int index = it - srcVector.begin();
Keep these simple tricks in mind and iterators won't be scary. Remember, iterators just point to the elements in a container.
The STL Cheat Sheet
You're now a C++ pro, and can use basically every STL algorithm. Go to github and look at the C++ Standard Template Library Algorithm Cheat Sheet that I made, and star/bookmark it for later.
While you're at it, glance at the Data Structure cheat sheet that I wrote, as well.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|