remove remove_if
来源:互联网 发布:鄂州锦天软件 编辑:程序博客网 时间:2024/05/21 10:36
http://en.cppreference.com/w/cpp/algorithm/remove
Possible implementation
template< class ForwardIt, class T >ForwardIt remove(ForwardIt first, ForwardIt last, const T& value){ first = std::find(first, last, value); if (first != last) for(ForwardIt i = first; ++i != last; ) if (!(*i == value)) *first++ = std::move(*i); return first;}
template<class ForwardIt, class UnaryPredicate>ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p){ first = std::find_if(first, last, p); if (first != last) for(ForwardIt i = first; ++i != last; ) if (!p(*i)) *first++ = std::move(*i); return first;}
Examples
The following code removes all spaces from a string by shifting all non-space characters to the left and then erasing the extra. This is an example of erase-remove idiom.
Run this code
#include <algorithm>#include <string>#include <iostream>#include <cctype> int main(){ std::string str1 = "Text with some spaces"; str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end()); std::cout << str1 << '\n'; std::string str2 = "Text\n with\tsome \t whitespaces\n\n"; str2.erase(std::remove_if(str2.begin(), str2.end(), [](char x){return std::isspace(x);}), str2.end()); std::cout << str2 << '\n';}
Output:
TextwithsomespacesTextwithsomewhitespaces
http://www.cplusplus.com/reference/algorithm/remove_if/
1234567891011121314
template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred){ ForwardIterator result = first; while (first!=last) { if (!pred(*first)) { *result = std::move(*first); ++result; } ++first; } return result;}
12345678910111213141516171819202122
// remove_if example#include <iostream> // std::cout#include <algorithm> // std::remove_ifbool IsOdd (int i) { return ((i%2)==1); }int main () { int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = std::remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ? // ^ ^ std::cout << "the range contains:"; for (int* p=pbegin; p!=pend; ++p) std::cout << ' ' << *p; std::cout << '\n'; return 0;}
0 0
- remove remove_if
- C++ remove,remove_if
- STL remove和remove_if
- remove和remove_if()
- STL_Algorithm3: remove, remove_if, remove_copy, remove_copy_if
- STL 之remove,remove_if,remove_copy,remove_copy_if
- stl中remove和remove_if的陷阱
- STL 之remove,remove_if,remove_copy,remove_copy_if
- STL之remove、remove_copy、remove_if、remove_copy_if
- remove_if
- remove_if
- remove_if
- remove_if
- 变异算法之删除(remove, remove_if, remove_copy, remove_copy)
- C++标准库---移除性算法之一remove()&remove_if()
- C++ STL 算法:删除算法(1)remove、remove_if
- STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)
- stl内的remove_if函数和remove函数和list类的remove函数简单例子
- 将在一列的以逗号隔开的列数据转化为行数据显示的函数
- 通过 KVM+virt-manager配置双屏虚拟机(两套键盘。鼠标)
- 118 nginx+tomcat+session共享
- Ubuntu 12.04 如何使用其他用户登录
- vector排序问题<unresolved overloaded function type>
- remove remove_if
- WMS与PDA扫描业务的结合实现物流仓储管理
- 对#Hello,2015征文#+Android 热敏打印机打印二维码(图片)博客的尝试
- php将html倒成doc
- onActivityResult 的用法
- HIVE和Hbase区别
- jsp 中useBean type与class的区别
- Bugzilla Configuration
- vs无法识别的外部符号 main