64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
struct Point
{
double x;
double y;
};
void sort_point(vector<Point> & vp)
{
sort(vp.begin(), vp.end(), [](auto a,auto b)
{
if(a.x < b.x) //比较X
return true;
if(a.x == b.x) //X 相等时比较y
{
if(a.y < b.y)
return true;
}
return false;
});
}
int main()
{
vector<Point> vp;
for(int i=0; i < 10; ++i)
{
//输入10个坐标
Point tp;
cout<<"put in x y"<<endl;
cin>>tp.x >>tp.y;
vp.emplace_back(tp);
}
sort_point(vp); //排序
//save to point.txt
cout<<"排序完成,保存文件point.txt" << endl;
ofstream outfile("D:\\point.txt", std::ofstream::out);
for(auto p : vp)
{
outfile<<p.x << ' ' << p.y << '\n';
cout<<p.x << ' ' << p.y << endl;
}
outfile.close();
return 0;
}
#include <iostream>
#include <map>
#include <fstream>
using namespace std;
int main()
{
map<double, double> Point;
double x,y;
for(int i=0; i < 10; ++i)
{
//输入10个坐标
cout<<"put in x y"<<endl;
cin>>x >>y;
Point.insert(std::pair<double, double>(x, y) );
}
//save to point.txt
ofstream outfile("D:\\point.txt", std::ofstream::out);
for(auto p : Point)
{
outfile<<p.first << ' ' << p.second << '\n';
}
outfile.close();
return 0;
}
#include <iostream>
#include <map>
#include <fstream>
using namespace std;
int main()
{
map<double, double> Point;
double x,y;
for(int i=0; i < 5; ++i)
{
//输入10个坐标
cout<<"put in x y"<<endl;
cin>>x >>y;
Point.insert(std::pair<double, double>(x, y) );
}
//save to point.txt
ofstream outfile("D:\\point.txt", std::ofstream::out);
for(auto p : Point)
{
outfile<<p.first << ' ' << p.second << '\n';
}
return 0;
}