MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
remove.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ALGORITHM_REMOVE_HPP__
2#define MSTL_CORE_ALGORITHM_REMOVE_HPP__
3
11
15
21
38template <typename Iterator1, typename Iterator2, typename T,
40constexpr Iterator2 remove_copy(Iterator1 first, Iterator1 last, Iterator2 result, const T& value) {
41 for (; first != last; ++first) {
42 if (*first != value) {
43 *result = *first;
44 ++result;
45 }
46 }
47 return result;
48}
49
64template <typename Iterator1, typename Iterator2, typename Predicate,
66constexpr Iterator2 remove_copy_if(Iterator1 first, Iterator1 last, Iterator2 result, Predicate pred) {
67 for (; first != last; ++first) {
68 if (!pred(*first)) {
69 *result = *first;
70 ++result;
71 }
72 }
73 return result;
74}
75
91template <typename Iterator, typename T, enable_if_t<is_ranges_fwd_iter_v<Iterator>, int> = 0>
92constexpr Iterator remove(Iterator first, Iterator last, const T& value) {
93 first = _MSTL find(first, last, value);
94 Iterator next = first;
95 return first == last ? first : _MSTL remove_copy(++next, last, first, value);
96}
97
113template <typename Iterator, typename Predicate, enable_if_t<is_ranges_fwd_iter_v<Iterator>, int> = 0>
114constexpr Iterator remove_if(Iterator first, Iterator last, Predicate pred) {
115 first = _MSTL find_if(first, last, pred);
116 Iterator next = first;
117 return first == last ? first : _MSTL remove_copy_if(++next, last, first, pred);
118}
119
134template <typename Container, typename U>
135constexpr size_t erase(Container& cont, const U& value) {
136 using T = decltype(*cont.begin());
137 static_assert(
138 declval<T>().operator =(declval<U>()),
139 "U must be comparable to the value type of Container");
140
141 const auto old_size = cont.size();
142 const auto end = cont.end();
143 auto removed = _MSTL remove_if(cont.begin(), end,
144 [&value](const auto& iter) {
145 return *iter == value;
146 });
147 cont.erase(removed, end);
148 return old_size - cont.size();
149}
150
165template <typename Container, typename Predicate>
166constexpr size_t erase_if(Container& cont, Predicate pred) {
167 const size_t old_size = cont.size();
168 const auto end = cont.end();
169 auto removed = _MSTL remove_if(cont.begin(), end,
170 [ref_pred = _MSTL ref(pred)](const auto& iter) {
171 return ref_pred(*iter);
172 });
173 cont.erase(removed, end);
174 return old_size - cont.size();
175}
176 // RemoveAlgorithms
178
180#endif // MSTL_CORE_ALGORITHM_REMOVE_HPP__
add_rvalue_reference_t< T > declval() noexcept
获取类型的右值引用,仅用于非求值上下文
MSTL_NODISCARD constexpr Iterator find(Iterator first, Iterator last, const T &value)
查找范围内第一个等于指定值的元素
constexpr Iterator find_if(Iterator first, Iterator last, Predicate pred)
查找范围内第一个满足谓词的元素
MSTL_INLINE17 constexpr bool is_ranges_fwd_iter_v
检查是否为范围前向迭代器
constexpr Iterator next(Iterator iter, iter_difference_t< Iterator > n=1)
获取迭代器的后一个位置
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
MSTL_NODISCARD constexpr reference_wrapper< T > ref(T &val) noexcept
创建引用包装器
constexpr Iterator2 remove_copy(Iterator1 first, Iterator1 last, Iterator2 result, const T &value)
复制范围中不等于指定值的元素
constexpr Iterator remove_if(Iterator first, Iterator last, Predicate pred)
移除范围中满足谓词的元素
constexpr Iterator2 remove_copy_if(Iterator1 first, Iterator1 last, Iterator2 result, Predicate pred)
复制范围中不满足谓词的元素
constexpr size_t erase_if(Container &cont, Predicate pred)
从容器中删除所有满足谓词的元素
constexpr Iterator remove(Iterator first, Iterator last, const T &value)
移除范围中等于指定值的元素
constexpr size_t erase(Container &cont, const U &value)
从容器中删除所有等于指定值的元素
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) end(Container &cont) noexcept(noexcept(cont.end()))
获取容器的结束迭代器
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL引用包装器
MSTL查找和搜索算法