-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherase_where.hpp
More file actions
45 lines (37 loc) · 1.15 KB
/
erase_where.hpp
File metadata and controls
45 lines (37 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/** @file
*
* @brief Convenience functions for the erase-remove idiom in C++.
*
* It's a common error to forget to erase an element from a container after
* calling @c remove. This wraps the two calls together.
*
* This functionality is mostly (if not completely) obsolete due to the C++ 20
* `erase_if` function.
*
* @note Thanks goes to Richard Hodges. Most of this code is copied directly
* from a post of his on StackOverflow.
*
* @authors Richard Hodges, Cliff Green
*
* @copyright (c) 2017-2025 by Cliff Green
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#ifndef ERASE_WHERE_HPP_INCLUDED
#define ERASE_WHERE_HPP_INCLUDED
#include <algorithm>
#include <utility> // std::forward
namespace chops {
template <typename C>
auto erase_where(C& c, const typename C::value_type& val) {
return c.erase(std::remove(c.begin(), c.end(), val), c.end());
}
template<typename C, typename F>
auto erase_where_if(C& c, F&& f) {
return c.erase(std::remove_if(c.begin(), c.end(), std::forward<F>(f)),
c.end());
}
} // end namespace
#endif