Vector怎么对成员过滤啊?

tangxu12 2009-06-16 07:56:50
Point3d 是一个点的类,有三个变量X Y Z分别代表点的坐标
Class Point3d
{
public:
double X;
double Y;
double Z;
}

vector <Point3d> MyPointList;

请问怎么把X+Y+Z>100的点过滤去来。

情况一:在原来的vector中把不符合要求的点 删除了。
情况二:把符合要求的点组成一个新的vector。
...全文
207 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
winingsky 2009-06-16
  • 打赏
  • 举报
回复
帮顶!!
lori227 2009-06-16
  • 打赏
  • 举报
回复
还是用stl 里的算法比较好~~
mstlq 2009-06-16
  • 打赏
  • 举报
回复
过滤代码2楼已经有了,这个是加入新vector的……

#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;
};


输出如下:
MyPointList:
3 3 3
23 23 23
43 43 43
63 63 63
83 83 83
103 103 103
123 123 123
143 143 143
163 163 163
183 183 183
----------------
MynewPointList:
3 3 3
23 23 23
请按任意键继续. . .
Walf_ghoul 2009-06-16
  • 打赏
  • 举报
回复

#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;
}



结果:
9 9 8
90 9 0

Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.
T技术沙龙 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 red_berries 的回复:]
1.遍历一遍,不合要求的erase()
2.insert
[/Quote]
顶,vector这个部分学的不是很好
mstlq 2009-06-16
  • 打赏
  • 举报
回复
可以用remove_if函数和erase函数


#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;
};
red_berries 2009-06-16
  • 打赏
  • 举报
回复
1.遍历一遍,不合要求的erase()
2.insert

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧