您的位置:首页 > 运维架构

变异算法之删除(remove, remove_if, remove_copy, remove_copy)

2011-04-29 10:50 309 查看
函数原型

//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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: