From the course: C++ Best Practices for Developers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Map, filter, and reduce - C++ Tutorial
From the course: C++ Best Practices for Developers
Map, filter, and reduce
- [Instructor] Many languages, libraries, and databases have three important methods: map, filter, and reduce. At first glance, C++ seems to be lacking these functions. And that surprises some developers, but the STL does have something close. If you wish to map over a collection, transform or for_each are your replacements. If you wish to remove or keep items in a collection, then copy_if or remove_if are what you're looking for. And finally, if you need to reduce a collection to a scalar, try accumulate. These functions are not immutable, but they work with all kinds of collections. Let's take a look at some code. Our sample begins with two lambda expressions. So here we have header. And here we have render. And these are just little helper functions to make the output look a little nicer. Then we have a collection called inCollection, and it's just got some numbers in there. And what we're going to do is transform. Now transform is similar to map, it allows you to go over a…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.