C++算法 remove_if()函数用于在给定参数内(第一个,最后一个)消除所有满足谓词的元素,而不会干扰其余元素的顺序。
template <class ForwardIterator, class UnaryPredicate> ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
first:前向迭代器,指向要从中删除元素的参数内第一个元素的位置。
last:正向迭代器,在要删除元素的参数内,将位置指向最后一个元素之后的位置。
pred :接受元素作为参数的一元谓词函数,必须替换元素的值。
如果first和last相等,则前向迭代器指向修改参数或第一个元素的新结束位置(最后)。
让我们看一个简单的示例来演示remove_if()的用法:
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool IsOdd(int i) { return ((i % 2) == 1); } int main () { vector <int> vec2 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //store the position of last element vector <int>::iterator pend; //Print original vector cout << "\nOriginal vector : "; for(int i=0; i < vec2.size(); i++) cout << " " << vec2[i]; cout << "\n"; //remove_if function call pend = remove_if (vec2.begin(), vec2.end() , IsOdd); //Print the vector cout << "After remove_if : "; for ( vector<int> ::iterator q=vec2.begin(); q != pend; ++q) cout << ' ' << *q; cout << '\n'; return 0; }
输出:
Original vector : 1 2 3 4 5 6 7 8 9 10 After remove_if : 2 4 6 8 10
让我们看另一个简单的例子:
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-remove-if-function.html
来源:LearnFk无涯教程网
#include <vector> #include <algorithm> #include <iostream> using namespace std; bool greater6 ( int value ) { return value >6; } int main( ) { vector <int> v1, v2; vector <int>::iterator Iter1, Iter2, new_end; int i; for ( i = 0 ; i <= 9 ; i++ ) v1.push_back( i ); int ii; for ( ii = 0 ; ii <= 3 ; ii++ ) v1.push_back( 7 ); random_shuffle ( v1.begin( ), v1.end( ) ); cout << "Vector v1 is ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; //Remove elements satisfying predicate greater6 new_end = remove_if (v1.begin( ), v1.end( ), greater6 ); cout << "Vector v1 with elements satisfying greater6 removed is\n ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; //To change the sequence size, use erase v1.erase (new_end, v1.end( ) ); cout << "Vector v1 resized elements satisfying greater6 removed is\n ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")." << endl; return 0; }
输出:
Vector v1 is ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ). Vector v1 with elements satisfying greater6 removed is ( 4 0 5 1 6 3 2 1 6 9 3 7 8 2 ). Vector v1 resized elements satisfying greater6 removed is ( 4 0 5 1 6 3 2 ).
让我们看另一个简单的例子:
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-remove-if-function.html
来源:LearnFk无涯教程网
#include <iostream> #include <functional> #include <vector> #include <algorithm> typedef std::vector<std::string>::iterator iterator; struct startsWithA : public std::unary_function<std::string, bool> { bool operator() (std::string s) { if(s[0] == 'A') { return true; } else return false; } }; void print(iterator b, iterator e) { iterator i; for(i = b; i != e; i++) { std::cout << *i << " "; } std::cout << std::endl; } int main() { startsWithA s; std::vector<std::string> v; v.push_back("China"); v.push_back("India"); v.push_back("Korea"); v.push_back("America"); v.push_back("Australia"); v.push_back("Pakistan"); std::cout << "Vector : "; print(v.begin(), v.end()); iterator i = remove_if(v.begin(), v.end(), s); std::cout << "Vector : "; print(v.begin(), i); return 0; }
输出:
Vector : China India Korea America Australia Pakistan Vector : China India Korea Pakistan
让我们看另一个简单的示例,通过将所有非空格字符向左移动,然后擦除多余的空格,来删除字符串中的所有空格。
#include <algorithm> #include <string> #include <iostream> #include <cctype> using namespace std; int main() { string str1 = "Text with some spaces"; str1.erase(remove(str1.begin(), str1.end(), ' '), str1.end()); cout << str1 << '\n'; string str2 = "Text\n with\tsome \t whitespaces\n\n"; str2.erase(remove_if(str2.begin(), str2.end(), [](unsigned char x){return std::isspace(x);}), str2.end()); cout << str2 << '\n'; return 0; }
输出:
Textwithsomespaces Textwithsomewhitespaces
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)