变异算法之删除(remove, remove_if, remove_copy, remove_copy)
2011-04-29 10:50
309 查看
函数原型
example
粗细指针,细指针指向修改后的最后的下一个位置。
remove_copy_if student
//remove template<class FwdIt, class T> FwdIt remove(FwdIt first, FwdIt lasst, const T & val); 即:if(!(*(first+N)==val)) N(-[0,last-first) *X++ = *(first+N); //remove_if template<class FwdIt, class Pred> FwdIt remove_if(FwdIt first, FwdIt last, Pred pr); 即:if(!pr(*(first+N))) N(-[0, last-first) *X++=*(first+N); //remove_copy template<class InIt, class OutIt, class T> OutIt remove_copy(InIt first, InIt last, OutIt x, const % & T); 即:if(!(*(first+N)==val)) N(-[0, last-first) *X++=*(first+N); //remove_copy_if template<class InIt, class OutIt, class Pred> OutIt remove_copy_if(InIt first, InIt last, OutIt x, Pred pr); 即:if(!pr(*(first+N))) N(-[0, last-first) *X++=*(first+N)
example
#include <iostream> #include <vector> #include <algorithm> #include <functional> #include <cstdlib> #include <iterator> using namespace std; int main(int argc, char *argv[]) { int a[] = {1, 2, 2, 4, 5, 4, 3, 2, 1}; vector<int> v1(a, a+9); cout << "删除前向量 v1 = "; copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " ")); cout << endl; vector<int>::iterator first = v1.begin(); vector<int>::iterator last; last = remove(v1.begin(), v1.end(), 2); cout << "删除后向量 v1 = "; copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " ")); cout << endl; cout << "删除后有效数据 = "; copy(first, last, ostream_iterator<int>(cout, " ")); cout << endl; system("pause"); return 0; }
粗细指针,细指针指向修改后的最后的下一个位置。
#include <cstdlib> #include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; int main(int argc, char *argv[]) { int a[] = {1, 2, 3, 4, 5}; vector<int> v1(a, a+5); vector<int>::iterator last = remove(v1.begin(), v1.end(), 3); copy(v1.begin(), last, ostream_iterator<int>(cout, " ")); //last 表示删除后有效数据的下个空间 cout << endl; vector<int> v2(a, a+5); vector<int> v3; remove_copy(v2.begin(), v2.end(), back_inserter(v3), 3); copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, " ")); cout << endl; system("pause"); return 0; }
remove_copy_if student
#include <iostream> #include <vector> #include <cstdlib> #include <algorithm> #include <functional> #include <fstream> #include <iterator> using namespace std; class Student { private: string stuName; string strNo; int stuChinese; int stuMath; public: Student(const string name = "none", const string no = "0000", int chinese = 0, int math = 0 ) { stuName = name; strNo = no; stuChinese = chinese; stuMath = math; } bool operator<(const int &total) const { return ((stuChinese+stuMath)<total); } const string name() const { return stuName; } const int chinese() const { return stuChinese; } const int math() const { return stuMath; } friend ostream & operator <<(ostream &, Student &); }; bool MyCompare(const Student &s) //此参数为const时,<重载时为常函数 { return s < 150; } ostream & operator << (ostream & os, const Student &s) { os << s.name() << "/t" << s.chinese() << "/t" << s.math(); return os; } int main(int argc, char *argv[]) { Student s1("zhang", "10001", 60, 70); Student s2("li", "10002", 70, 80); Student s3("zhao", "10003", 75, 85); Student s4("wang", "10004", 68, 78); Student s5("zhou", "10005", 86, 76); Student s6("qian", "10006", 30, 80); vector<Student> v; v.push_back(s1); v.push_back(s2); v.push_back(s3); v.push_back(s4); v.push_back(s5); v.push_back(s6); ofstream out("stud.dat"); remove_copy_if(v.begin(), v.end(), ostream_iterator<Student>(out, "/n"), MyCompare); out.close(); return 0; }
相关文章推荐
- STL_算法_删除(remove、remove_if、remove_copy、remove_copy_if)
- C++ STL 算法:删除算法(2)remove_copy、remove_copy_if
- 删除算法 2-remove_copy()
- remove/remove_if算法配合容器的erase方法实现容器删除元素功能
- STL常用算法: remove,remove_if,remove_copy,remove_copy_if,remove系列算法和partition的区别...
- STL remove_copy_if算法
- 变异算法之替换(replace, replace_if, replace_copy, replace_copy_if)
- C++ STL 算法:删除算法(1)remove、remove_if
- remove_copy/remove_copy_if详解
- STL源码剖析——STL算法之remove删除算法
- 【LeetCode-面试算法经典-Java实现】【203-Remove Linked List Elements(删除单链表中的元素)】
- list::remove_if和泛型算法remove_if浅析
- 变异算法之反转(reverse(), reverse_copy)
- 数据算法之二叉树删除(BinaryTreeL Remove)的Java实现
- remove_copy_if
- STL_算法_替换(replace、replace_copy、replace_if、replace_copy_if)
- vector删除元素erase和通用算法remove
- remove_copy_if
- STL算法之—————remove_if, remove, remove_copy_if
- 非变异算法之计数(count, count_if)