# 15 Different Ways to Filter Containers in Modern C++

DevFeed: [15 Different Ways to Filter Containers in Modern C++](<https://devfeed.tech/articles/15-different-ways-to-filter-containers-in-modern-c-53298.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2021/filter-cpp-containers/>)

Published: 2026-05-03T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [Code](<https://devfeed.tech/topics/code.md>), [Library](<https://devfeed.tech/topics/library.md>), [implementation](<https://devfeed.tech/topics/implementation.md>)

Tags: [2025](<https://devfeed.tech/tags/2025.md>), [algorithms](<https://devfeed.tech/tags/algorithms.md>), [article](<https://devfeed.tech/tags/article.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-23](<https://devfeed.tech/tags/c-plus-plus-23.md>), [code](<https://devfeed.tech/tags/code.md>), [library](<https://devfeed.tech/tags/library.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>)

## AI overview

A tutorial explores multiple ways to implement a filter function for C++ containers, including raw loops, standard-library algorithms, and modern C++ techniques. The article was updated with additional C++23 techniques.

## Source excerpt

Do you know how many ways we can implement a filter function in C++? While the problem is relatively easy to understand - take a container, copy elements that match a predicate, and return a new container - it's good to exercise with the C++ Standard Library and check a few ideas. We can also apply some Modern C++ techniques, including C++23. Let's start! The article was written in 2021 and recently updated in late 2025 to include additional techniques from C++23. Additionally, the text is also republished on the ACCU website: ACCU Overload, 33: 15 Different Ways to Filter Containers in Modern C++. The Problem Statement To be precise, by a filter I mean a function with the following interface: auto Filter(const Container& cont, UnaryPredicate p) {} It takes a container and a predicate, and creates an output container with elements (copies) that satisfy the predicate. We can use it like the following: const std::vector<std::string> vec{ "Hello", "**txt", "World", "error", "warning", "C++", "****" }; auto filtered = Filter(vec, [](auto& elem) { return !elem.starts_with('*'); }); // filtered should have "Hello", "World", "error", "warning", "C++" Writing such a function can be a good exercise with various options and algorithms in the Standard Library. What's more, our function hides internal mechanisms like iterators, so it's more like a range-based version. Let's start with the first option: Good old Raw Loops While it's good to avoid raw loops, they might help us to fully understand the problem. For our filtering problem, we can write the following code: // filter v1 template <typename T, typename Pred> auto FilterRaw(const std::vector<T>& vec, Pred p) { std::vector<T> out; for (auto&& elem : vec) if (p(elem)) out.push_back(elem); return out; } Simple yet very effective. Please note some nice features of this straightforward implementation. The code uses auto return type deduction, so there's no need to write the explicit type (although it could be just std::vector<