已知一组坐标点,怎么按照一定的顺序排列坐标点呢?

berlinpand 2017-11-23 04:57:44
比如我需要将这些点按照x横坐标从小到大的顺序排列,放在txt文档中,还望大家指导
...全文
1238 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mirro 2019-06-02
  • 打赏
  • 举报
回复
先比较x坐标,再比较y坐标呢
一指禅心 2019-05-30
  • 打赏
  • 举报
回复
berlinpand 2017-11-26
  • 打赏
  • 举报
回复
引用 5 楼 ybs963 的回复:

#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
  • 打赏
  • 举报
回复
引用 2 楼 ybs963 的回复:
忘了最后关闭文件。。。。

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

非常感觉您的指导,不过跑了一下发现当x值输入相同时,另一个点就没有被保存,如果需要保留相同x,y较小的在前面,可以实现么
ooolinux 2017-11-24
  • 打赏
  • 举报
回复
用一个结构体 struct Point { int x; int y; } 一个Point数组,以x为关键字进行排序就是了,如果不考虑排序算法的稳定性,数据结构书里面任何一种排序算法都可以。
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;
}

64,654

社区成员

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

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