65,211
社区成员
发帖
与我相关
我的任务
分享
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using std::vector;
class Point3d
{
public:
double X;
double Y;
double Z;
};
void shows(Point3d p)
{
std::cout<<p.X<<'\t'<<p.Y<<'\t'<<p.Z<<std::endl;
};
class insertIfGreater100
{
public:
void operator()(Point3d p)
{
if (p.X+p.Y+p.Z <=100)
PointList_.push_back(p);
};
insertIfGreater100(vector <Point3d> &PointLis):PointList_(PointLis){};
private:
vector <Point3d> &PointList_;
};
int main( )
{
using namespace std;
vector <Point3d> MyPointList;
//shows sp;
//在这里初始化MyPointList;
for(int i=3;i<200;i+=20)
{
Point3d tmp;
tmp.X=i;
tmp.Y=i;
tmp.Z=i;
MyPointList.push_back(tmp);
}
cout<<"MyPointList:"<<endl;
for_each(MyPointList.begin(),MyPointList.end(),shows);
vector <Point3d> MynewPointList;
MynewPointList.clear();
insertIfGreater100 iig(MynewPointList);
for_each(MyPointList.begin(),MyPointList.end(),iig);
cout<<"----------------"<<endl<<"MynewPointList:"<<endl;
for_each(MynewPointList.begin(),MynewPointList.end(),shows);
system("pause");
return 0;
};
#include <iostream>
#include <vector.h>
using namespace std;
class Point3d
{
public:
double x;
double y;
double z;
Point3d(double x1,double y1,double z1)
{
x = x1;
y = y1;
z = z1;
}
};
int main()
{
vector<Point3d> MyPointList;
vector<Point3d>::iterator it;
Point3d* p1 = new Point3d(90,90,90); //建立三组数据作为测试数据
Point3d* p2 = new Point3d(9,9,8);
Point3d* p3 = new Point3d(90,9,0);
MyPointList.push_back(*p1);
MyPointList.push_back(*p2);
MyPointList.push_back(*p3);
for( it = MyPointList.begin();it != MyPointList.end();it++) //删除不符合条件的元素
{
if((*it).x+(*it).y+(*it).z > 100)
{
MyPointList.erase(it);
}
}
for( it = MyPointList.begin();it != MyPointList.end();it++)//输出容器的内容
{
cout<<it->x<<" "<<it->y<<" "<<it->z<<endl;
}
return 0;
}
#include <vector>
#include <algorithm>
//#include <iostream>
class Point3d
{
public:
double X;
double Y;
double Z;
};
bool greater100 ( Point3d p )
{
return p.X+p.Y+p.Z >100;
};
int main( )
{
using namespace std;
vector <Point3d> MyPointList;
//在这里初始化MyPointList;
vector <Point3d>::iterator new_end;
new_end=remove_if(MyPointList.begin(),MyPointList.end(),greater100);
MyPointList.erase (new_end, MyPointList.end( ) );
return 0;
};