容器remove_if的用法

更新时间:2023-05-25 07:12:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

容器remove_if和erase的用法

Erase的应用

1、用法1

/***********************begin******************************/ #include<iostream>

#include<algorithm>

#include<vector>

using namespace std;

class student

{

public:

int key;

int value;

bool operator()(student s)

{

if(s.value==2)

return true;

else

return false;

}

};

int main()

{

vector<student>vec(4);

vec[0].key=1;

vec[0].value=1;

vec[1].key=2;

vec[1].value=2;

vec[2].key=2;

vec[2].value=2;

vec[3].key=3;

vec[3].value=3;

for (size_ti = 0; i<vec.size(); )

{

if (vec[i].value <= 2)

vec.erase(vec.begin() + i);

else

i++;

}

vector<student>::iterator it;

for(it=vec.begin();it!=vec.end();it++)

cout<<it->value<<endl;

}

/**************************end****************************/ 结果:

Remove_if的应用

1、用法1

/*************************begin****************************/ #include<iostream>

#include<algorithm>

#include<vector>

usingnamespacestd;

class student

{

public:

int key;

int value;

bool operator()(student s)

{

if(s.value<2)

return true;

else

return false;

}

};

int main()

{

vector<student>vec(4);

vec[0].key=1;

vec[0].value=1;

vec[1].key=2;

vec[1].value=2;

vec[2].key=2;

vec[2].value=2;

vec[3].key=3;

vec[3].value=3;

vector<student>::iterator it,p;

p=remove_if(vec.begin(),vec.end(),student());

for(it=vec.begin();it!=p;it++)

cout<<(*it).value<<endl;

}

/*****************************end*************************/ 结果:

2、用法2

/*****************************begin************************/

#include<iostream>

#include<algorithm>

#include<vector>

usingnamespacestd;

class student

{

public:

int key;

int value;

booloperator()(student s)

{

if(s.value==2)

returntrue;

else

returnfalse;

}

};

bool TT(student s)

{

if (s.value==3)

return true;

else

return false;

}

int main()

{

vector<student>vec(4);

vec[0].key=1;

vec[0].value=1;

vec[1].key=2;

vec[1].value=2;

vec[2].key=2;

vec[2].value=2;

vec[3].key=3;

vec[3].value=3;

vector<student>::iterator it,p;

p=remove_if(vec.begin(),vec.end(),TT);

for(it=vec.begin();it!=p;it++)

cout<<(*it).value<<endl;

}

/*************************end******************************/ 结果:

3、用法3

/*****************************begin************************/ #include<iostream>

#include<algorithm>

#include<vector>

using namespace std;

class student

{

public:

int key;

int value;

bool operator()(student s)

{

if(s.value==2)

return true;

else

return false;

}

};

int T=1;

bool TT(student s)

{

if (s.value==T)

return true;

else

return false;

}

int main()

{

vector<student>vec(4);

vec[0].key=1;

vec[0].value=1;

vec[1].key=2;

vec[1].value=2;

vec[2].key=2;

vec[2].value=2;

vec[3].key=3;

vec[3].value=3;

vector<student>::iterator it,p;

p=remove_if(vec.begin(),vec.end(),TT);

for(it=vec.begin();it!=p;it++)

cout<<(*it).value<<endl;

}

/*************************end****************************/ 结果:

本文来源:https://www.bwwdw.com/article/i2t4.html

Top