gpt4 book ai didi

c++ - priority_queue中第一个模板参数有什么用

转载 作者:可可西里 更新时间:2023-11-01 15:17:35 24 4
gpt4 key购买 nike

对于 std::priority_queue,我假设第一个模板参数指定了类型,第二个应该是该类型的容器。示例:

priority_queue<int, vector<int>> someQueue;

但是,下面的代码可以编译并且运行良好:

class SomeClass
{
};

int main()
{
priority_queue <SomeClass, vector<int>> pq;
int x = 9;
pq.push(x);
int t = pq.top();
cout << t << endl;
pq.pop();
return 0;
}

上面的代码是否无效(即给UB)?

如果有效 - priority_queue 中使用的第一个模板参数(即 someClass)是什么。

最佳答案

通过 LWG issue 2566 在 jackson 维尔刚投票进入工作文件:

The first template parameter T of the container adaptors shall denote the same type as Container::value_type.

写作 std::priority_queue<SomeClass, std::vector<int>>相应地导致未定义的行为。

关于c++ - priority_queue中第一个模板参数有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com