C++ 与优先级队列中的第三个变量c++;

C++ 与优先级队列中的第三个变量c++;,c++,stl,priority-queue,C++,Stl,Priority Queue,在本页上: 作者给出了C++中使用优先级队列的一个很好的例子。在这篇文章中,作者展示了如何按顺序排列不同的时间。我有一个类似的问题,我想根据与给定时间点的接近程度来安排时间。我的问题是如何向比较器添加第三个输入,以便可以考虑额外的参数。也就是说,我们如何使t3内部比较器成为从外部传递的变量 #include <iostream> #include <queue> #include <iomanip> #include <cstdlib> usin

在本页上: 作者给出了C++中使用优先级队列的一个很好的例子。在这篇文章中,作者展示了如何按顺序排列不同的时间。我有一个类似的问题,我想根据与给定时间点的接近程度来安排时间。我的问题是如何向比较器添加第三个输入,以便可以考虑额外的参数。也就是说,我们如何使t3内部比较器成为从外部传递的变量

#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdlib>

using namespace std;

struct Time {
    int h; // >= 0
    int m; // 0-59
    int s; // 0-59
};

class CompareTime {
public:
    bool operator()(Time& t1, Time& t2)
    {
       Time t3 = {{2,9,0}};

       if (abs(t3.h - t2.h) > (t3.h - t1.h))
         return true;
       return false;
    }
};

int main()
{
    priority_queue<Time, vector<Time>, CompareTime> pq;

    // Array of 4 time objects:

    Time t[4] = { {3, 2, 40}, {3, 2, 26}, {5, 16, 13}, {5, 14, 20}};

    for (int i = 0; i < 4; ++i)
       pq.push(t[i]);

    while (! pq.empty()) {
       Time t2 = pq.top();
       cout << setw(3) << t2.h << " " << setw(3) << t2.m << " " <<
       setw(3) << t2.s << endl;
       pq.pop();
    }

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构时间{
int h;//>=0
int m;//0-59
int s;//0-59
};
类比较时间{
公众:
布尔运算符()(时间和t1、时间和t2)
{
时间t3={2,9,0};
if(abs(t3.h-t2.h)>(t3.h-t1.h))
返回true;
返回false;
}
};
int main()
{
优先级队列pq;
//4个时间对象的数组:
时间t[4]={{3,2,40},{3,2,26},{5,16,13},{5,14,20};
对于(int i=0;i<4;++i)
质量推力(t[i]);
而(!pq.empty()){
时间t2=pq.top();

cout比较器本身可以在实例化时参数化:

class CompareTime 
{
    Time t;
public:
    CompareTime(const Time& arg) : t{arg} {}

    bool operator()(const Time& t1, const Time& t2) const
    {
        return (abs(t.h - t2.h) > abs(t.h - t1.h));
    }
};
声明如下:

    Time myTime; // modify to your leisure...

    // ...then create your queue with myTime as the fixed param
    priority_queue<Time, vector<Time>, CompareTime> pq{CompareTime{myTime};
Time myTime;//修改您的空闲时间。。。
//…然后以myTime作为固定参数创建队列
优先级队列pq{CompareTime{myTime};

如果语法不正确,请提前道歉。目前我没有编译器,但我希望想法足够清楚。

最后,通过WhozCraig和user783920提供的有用指针,下面的解决方案似乎有效

#include <iostream>
#include <queue>
#include <iomanip>
#include <cstdlib>

using namespace std;


struct Time {
    int h; // >= 0
    int m; // 0-59
    int s; // 0-59
};

class CompareTime 
{
    Time t;
public:
    CompareTime(const Time& arg) {
        std::cout << "struct constructor \n";
        t=arg;
    }
//    CompareTime(){}

    bool operator()(const Time& t1, const Time& t2) const
    {
        return (abs(t.h - t2.h) > abs(t.h - t1.h));
    }
};

int main()
{
    Time mytime ={0};

    mytime.h=6;

    priority_queue<Time, vector<Time>, CompareTime> pq{CompareTime(mytime)};

    // Array of 4 time objects:

    Time t[4] = { {3, 2, 40}, {2, 2, 26}, {5, 16, 13}, {1, 14, 20}};

    for (int i = 0; i < 4; ++i)
       pq.push(t[i]);

    while (! pq.empty()) {
       Time t2 = pq.top();
       cout << setw(3) << t2.h << " " << setw(3) << t2.m << " " <<
       setw(3) << t2.s << endl;
       pq.pop();
    }

    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
结构时间{
int h;//>=0
int m;//0-59
int s;//0-59
};
类比较时间
{
时间t;
公众:
比较时间(常数时间和参数){
std::coutabs(t.h-t1.h));
}
};
int main()
{
时间mytime={0};
mytime.h=6;
优先级队列pq{CompareTime(mytime)};
//4个时间对象的数组:
时间t[4]={{3,2,40},{2,2,26},{5,16,13},{1,14,20};
对于(int i=0;i<4;++i)
质量推力(t[i]);
而(!pq.empty()){
时间t2=pq.top();

你能不能让t3成为CompareTime的一个成员并把它传递给构造函数。当你使用它的时候,修复你的比较器。操作数应该是const ref,
操作符()
本身也应该是
const
。你可以返回
abs(t3.h-t2.h)>(t3.h-t1.h)
也是。您应该将构造函数参数的名称从
t
更改。我希望将t3作为一个变量,可以在运行时进行控制。如果我正确理解了您的解决方案,这只能在编译时进行,而不能在运行时进行。不是吗?@Bonaqa您可以传递任何动态
时间声明优先级队列时使用
CompareTime
的构造函数。但是,一旦构建了优先级队列,就不能(也不应该)更改该构造函数。如果要更改比较器契约(这是您在执行此操作时设置的)您需要使用一个新的
CompareTime
Time
参数声明一个新队列,并将所有当前项目移动到新队列中。但是,没有任何东西可以阻止您从创建队列的那一刻起就开始创建此队列中使用的
Time
。感谢您的努力。尝试这样做时解析我得到了一个神秘的错误消息:
pq.cpp:在构造函数'CompareTime::CompareTime(const-Time&)':pq.cpp:18:41:错误:无法在初始化CompareTime(const-Time&arg):t{arg}中将'const-Time'转换为'int'^
Time
是否支持复制构造?这需要这样才能起作用。正如我所说,我是编译器不可知论者,对不对。很抱歉。我还可能错过了一组内部卷发。(对不起)。