How I discovered the C++ algorithm library and learned not to reinvent the wheel

(Steve Johnson on Unsplash)5.

generateThis function essentially changes the values of your collection, or a range of it, based on the generator you provide.

The generator is a function of the form T f(); where T is a compatible type with our collection.

In the above example, notice that we are actually changing our collection in-place.

And the generator here is the lambda function we provided.

6.

shuffleFrom the standard of C++17, random_shuffle has been removed.

Now we prefer shuffle which is more effective, given that it takes advantage of the header random.

Note that we are using Mersenne Twister, a pseudo-random number generator introduced in C++11.

Random number generators have become far more mature in C++ with the introduction of random library and inclusion of better methods.

7.

nth_elementThis function is quite useful, given that it has an interesting complexity.

Say you want to know the n-th element of your collection if it was sorted, but you do not want to sort the collection to make an O(n log(n)) operation.

What would you do?Then nth_element is your friend.

It finds the desired element in O(n).

Interestingly, nth_element may or may not make your collection sorted.

It will just do whatever order it takes to find the n-th element.

Here is an interesting discussion on StackOverflow.

And also, you can always add your own comparison function (like we added lambdas in previous examples) to make it more effective.

8.

equal_rangeSo let’s say you have a sorted collection of integers.

You want to find the range in which all the elements have a specific value.

For example:In this code, we are looking for a range in the vector that holds all 5.

The answer is (2~4).

Of course we can use this function for our own custom property.

You need to ensure that the property you have aligns with the order of the data.

See this article for reference.

Finally, lower_bound and upper_bound both can help you to achieve the same that you achieved using equal_range.

9.

merge inplace_mergeImagine you have two sorted collections (what a fun thing to imagine, right?), you want to merge them, and you also want the merged collection to remain sorted.

How would you do that?You can just add the second collection to the first one and sort the result again which adds an extra O(log(n)) factor.

Instead of that, we can just use merge.

On the other hand, do you remember when implementing merge sort, we need to merge two sides of our array?.inplace_merge can be conveniently used for that.

Look at this tiny merge sort based on the example given in cppreference:How cool is that!Speaking of cool, here is a cool guy.

????.(Dawid Zawiła on Unsplash)10.

minmax minmax_elementminmax returns the minimum and maximum of the given two values, or the given list.

It returns a pair and it can also provide the functionality of your own comparison method.

minmax_element does the same for your container.

11.

accumulate partial_sumaccumulate does what it says, it accumulates values of your collection in the given range, using the initial value and a binary operation function.

See for yourself:So how is the value of custom calculated?At the beginning, accumulate takes the initial value (0) to the argument x, the first value in the collection (6) to argument y, does the operation, then assigns it to the accumulated value.

In the second call, it passes the accumulated value to x and the next element in the collection to y, and thus proceeds.

partial_sum does things much like accumulate, but it also keeps the result of first n terms in a destination container.

And of course as you expected, you can use your own custom operation.

12.

adjacent_differenceYou want to find the adjacent differences in your values, you can simply use this function.

Pretty simple, right?But it can do much more.

Look at this:What do these two lines do?.They find the first 10 Fibonacci numbers!.Do you see how?.????So that was it for today.

Thanks for reading!.I hope you learned something new.

I would definitely like to bring some new stuff for ya’ll again in near future.

Cheers!.????.

. More details

Leave a Reply