0
点赞
收藏
分享

微信扫一扫

c++ vector C++ vector存放结构体 并且排序



#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;

typedef struct Point{
int x;
int y;
}point;

bool cmp(point p1, point p2){
return p1.x < p2.x;
}
int main(){
vector<point> p;
point pp1, pp2;
pp1.x = 100;
pp1.y = 2;
pp2.x = 30;
pp2.y = 4;
p.push_back(pp1);
p.push_back(pp2);

sort(p.begin(), p.end(), cmp);

//vector<point>::iterator it = p.begin();
//cout << (*it).x << ' ' << (*it).y << ' ' << endl;

for(vector<point>::iterator it = p.begin(); it!=p.end(); it++)
{
//cout<<"迭代遍历:"<<(*it).x<<endl;
cout <<"迭代遍历:"<< (*it).x << ' ' << (*it).y << ' ' << endl;
}

return 0;
}

迭代遍历:30 4 
迭代遍历:100 2


举报

相关推荐

0 条评论