C++vector容器+结构体
#include<iostream>#include<vector>#include<cstdlib>#include<ctime>#include<algorithm>using namespace std;const int n=210;struct A{int number;int score;...
·
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
const int n=210;
struct A
{
int number;
int score;
};
class Vector
{
private:
vector<A>a;//该容器,在做插入时有些缺陷->list容器则没有这个缺陷
vector<A>::iterator p;
public:
Vector()
{
A temp;
srand(time(0));
for(int i=0;i<n;++i)
{
temp.number=rand()%31;
temp.score=rand()%101;
a.push_back(temp);
}
}
static bool sort1(A a1,A a2)//建立规则函数,让它由大到小排序
{
return a1.number>a2.number;
}
void browse()//遍历输出
{
for(p=a.begin();p!=a.end();++p)
{
cout<<p->number<<"-"<<p->score<<endl;
}
}
void del()
{
for(p=a.begin();p!=a.end();++p)
{
if(p->number==0)
a.erase(p);
else
cout<<"no"<<endl;
}
}
void sort_v()
{
sort(a.begin(),a.end(),sort1);//结构体排序,按不同数据进行排序
}
};
int main()
{
Vector v;
v.sort_v();
v.browse();
return 0;
}
更多推荐
已为社区贡献1条内容
所有评论(0)