# 已知一组坐标点，怎么按照一定的顺序排列坐标点呢？

berlinpand 2017-11-23 04:57:44

...全文
691 8 点赞 打赏 收藏 举报

8 条回复

mirro 2019-06-02

• 打赏
• 举报

• 打赏
• 举报

berlinpand 2017-11-26

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

• 打赏
• 举报

makekone 2017-11-24
``````
#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;
}
``````
• 打赏
• 举报

berlinpand 2017-11-24

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

``````

• 打赏
• 举报

ooolinux 2017-11-24

• 打赏
• 举报

makekone 2017-11-23

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

``````
• 打赏
• 举报

makekone 2017-11-23
``````
#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;
}
``````
• 打赏
• 举报

6.0w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等

2017-11-23 04:57