C++ 函数以检索结构中最近的X&Y?

C++ 函数以检索结构中最近的X&Y?,c++,C++,文本文件 头文件 进展 使用CreateRopeStructure函数,我将所有文本文件信息放入LRopeData结构中 我需要一个使用三个整数值的函数;使用所有三个整数值的ID ex.10000、X ex.-500和Y ex.400将基于ID检索最接近的X、Y1和Y2值 我有一个结构LRopeSearch,它可以用于这样做,但是我需要更多的帮助来实际使用函数中的结构来检索所述值 谢谢。啊。花了一段时间来想象输入数据的含义。在您最后的评论之后,我现在了解到输入是由ID标识的几组空绳索 绳索是有限

文本文件

头文件

进展

使用CreateRopeStructure函数,我将所有文本文件信息放入LRopeData结构中

我需要一个使用三个整数值的函数;使用所有三个整数值的ID ex.10000、X ex.-500和Y ex.400将基于ID检索最接近的X、Y1和Y2值

我有一个结构LRopeSearch,它可以用于这样做,但是我需要更多的帮助来实际使用函数中的结构来检索所述值


谢谢。

啊。花了一段时间来想象输入数据的含义。在您最后的评论之后,我现在了解到输入是由ID标识的几组空绳索

绳索是有限的垂直线段,由X,Y1-X,Y2给出

我简化了数据结构,因为在LRData中包含LRID是多余的:

typedef int                    LRID;
typedef std::vector<LRopeData> LRData;
typedef std::map<LRID, LRData> LRMap;
当然,最有趣的部分是:LRopeData::distanceToPoint:

用于OP中的查询

完整代码清单 包括我对分析输入的看法:

#include <vector>
#include <map>
#include <cmath>
//#include <tr1/functional> // bind (for c++03)

struct LRopeData {
    int X;
    int Y1;
    int Y2;

    double distanceToPoint(double px, double py) const
    {
        int y1(Y1), y2(Y2);
        // normalize segment endpoints (y1, y2)
        if (y1>y2) std::swap(y1, y2);

        double dx = (px - X);
        double dy = 0;

        if (py<y1)   dy = (py - y1);
        if (py > y2) dy = (py - y2);

        return sqrt(dx * dx + dy * dy);
    }
};

typedef int                    LRID;
typedef std::vector<LRopeData> LRData;
typedef std::map<LRID, LRData> LRMap;
typedef std::pair<LRID, LRData> LRPair;

LRMap CreateRopeStructure();

#include <algorithm>
#include <iostream>

int main()
{
    LRMap data = CreateRopeStructure();

    // ex. from OP
    int const ID = 10000;
    int const  X =  -500;
    int const  Y =   400;

    // select rope data by ID:
    auto& ropes = data[ID];

    if (ropes.empty())
    {
        std::cout << "infinite" << std::endl;
    } else
    {
        // get the distance to each rope
        std::vector<double> distances(ropes.size());
        std::transform(
                ropes.begin(), ropes.end(), 
                distances.begin(),
                [=](LRopeData const& rope) { return rope.distanceToPoint(X, Y); });
            // for c++03:
            // std::bind(std::mem_fn(&LRopeData::distanceToPoint), std::placeholders::_1, X, Y));

        // print the shortest distance
        std::cout << *std::min_element(distances.begin(), distances.end()) << std::endl;
    }
}

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <fstream>
#include <cassert>

BOOST_FUSION_ADAPT_STRUCT(LRopeData, (int,X)(int,Y1)(int,Y2))

LRMap CreateRopeStructure()
{
    // input
    std::ifstream ifs("input.txt");
    ifs >> std::noskipws;
    typedef boost::spirit::istream_iterator It;
    It f(ifs), l;

    // grammar
    using namespace boost::spirit::qi;
    rule<It, LRPair(), blank_type, locals<int> > lrmap;

    lrmap %= '[' >> int_ >> ']' >> +eol
          >> "total" >> '=' >> omit [ int_ [ _a = _1 ] ] >> +eol
          >> repeat(_a) [ int_ >> int_ >> int_ >> +eol ]
          ;

    // parse
    LRMap data;
    assert(phrase_parse(f, l, +lrmap, blank, data));

    if (f!=l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";

    // done
    return data;
}

你自己试过什么?还有,离什么最近?您能举个例子吗?提示:首先陈述您的问题,然后添加实现作为补充信息,这样读者就可以更快地了解需要查找的内容。还有,最接近的指标应该是什么?在函数中输入int X和int Y时,我需要它找到与我输入的最接近的X和Y。如果我要输入ID=10000 X=500 Y=400,它应该给我以下值-593 427 683什么公式确定一个LRopeData是否比另一个LRopeData更接近输入?我将使用什么公式确定?c++03版本Too我知道它代表距离,我需要它把最短距离绳子的x和y变成整数,明白吗?@user3513035你问的问题太多了,很明显。我没有用boost。我只在main之后使用它,因为您没有显示解析代码:。你说你已经有那个角色了!我特别清楚地说明了支持什么,甚至发布了一个c++03版本@用户3513035我当然理解。我相信你应该可以从这里开始。这是家庭作业吗?我想我知道如何在没有助推的情况下完成,谢谢!
typedef int                    LRID;
typedef std::vector<LRopeData> LRData;
typedef std::map<LRID, LRData> LRMap;
LRMap CreateRopeStructure();

#include <algorithm>
#include <iostream>

int main()
{
    LRMap data = CreateRopeStructure();

    // ex. from OP
    int const ID = 10000;
    int const  X =  -500;
    int const  Y =   400;

    // select rope data by ID:
    auto& ropes = data[ID];

    if (ropes.empty())
    {
        std::cout << "infinite" << std::endl;
    } else
    {
        // get the distance to each rope
        std::vector<double> distances(ropes.size());
        std::transform(
                ropes.begin(), ropes.end(), 
                distances.begin(),
                [=](LRopeData const& rope) { return rope.distanceToPoint(X, Y); });
            // for c++03:
            // std::tr1::bind(&LRopeData::distanceToPoint, std::tr1::placeholders::_1, X, Y));

        // print the shortest distance
        std::cout << *std::min_element(distances.begin(), distances.end()) << std::endl;
    }
}
struct LRopeData {
    int X;
    int Y1;
    int Y2;

    double distanceToPoint(double px, double py) const
    {
        int y1(Y1), y2(Y2);
        // normalize segment endpoints (y1, y2)
        if (y1>y2) std::swap(y1, y2);

        double dx = (px - X);
        double dy = 0;

        if (py<y1)   dy = (py - y1);
        if (py > y2) dy = (py - y2);

        return sqrt(dx * dx + dy * dy);
    }
};
96.8401
#include <vector>
#include <map>
#include <cmath>
//#include <tr1/functional> // bind (for c++03)

struct LRopeData {
    int X;
    int Y1;
    int Y2;

    double distanceToPoint(double px, double py) const
    {
        int y1(Y1), y2(Y2);
        // normalize segment endpoints (y1, y2)
        if (y1>y2) std::swap(y1, y2);

        double dx = (px - X);
        double dy = 0;

        if (py<y1)   dy = (py - y1);
        if (py > y2) dy = (py - y2);

        return sqrt(dx * dx + dy * dy);
    }
};

typedef int                    LRID;
typedef std::vector<LRopeData> LRData;
typedef std::map<LRID, LRData> LRMap;
typedef std::pair<LRID, LRData> LRPair;

LRMap CreateRopeStructure();

#include <algorithm>
#include <iostream>

int main()
{
    LRMap data = CreateRopeStructure();

    // ex. from OP
    int const ID = 10000;
    int const  X =  -500;
    int const  Y =   400;

    // select rope data by ID:
    auto& ropes = data[ID];

    if (ropes.empty())
    {
        std::cout << "infinite" << std::endl;
    } else
    {
        // get the distance to each rope
        std::vector<double> distances(ropes.size());
        std::transform(
                ropes.begin(), ropes.end(), 
                distances.begin(),
                [=](LRopeData const& rope) { return rope.distanceToPoint(X, Y); });
            // for c++03:
            // std::bind(std::mem_fn(&LRopeData::distanceToPoint), std::placeholders::_1, X, Y));

        // print the shortest distance
        std::cout << *std::min_element(distances.begin(), distances.end()) << std::endl;
    }
}

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <fstream>
#include <cassert>

BOOST_FUSION_ADAPT_STRUCT(LRopeData, (int,X)(int,Y1)(int,Y2))

LRMap CreateRopeStructure()
{
    // input
    std::ifstream ifs("input.txt");
    ifs >> std::noskipws;
    typedef boost::spirit::istream_iterator It;
    It f(ifs), l;

    // grammar
    using namespace boost::spirit::qi;
    rule<It, LRPair(), blank_type, locals<int> > lrmap;

    lrmap %= '[' >> int_ >> ']' >> +eol
          >> "total" >> '=' >> omit [ int_ [ _a = _1 ] ] >> +eol
          >> repeat(_a) [ int_ >> int_ >> int_ >> +eol ]
          ;

    // parse
    LRMap data;
    assert(phrase_parse(f, l, +lrmap, blank, data));

    if (f!=l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";

    // done
    return data;
}