gpt4 book ai didi

c++ - 为什么 CLion 给我警告?

转载 作者:搜寻专家 更新时间:2023-10-31 00:50:29 29 4
gpt4 key购买 nike

我在 CLion IDE 上声明了一个 std::priority_queue。

priority_queue<int, vector<int>, greater<int>> pq;

沃尼格说

Clang-Tidy: Prefer transparent functors 'greater<>'

并且这段代码没有警告。

priority_queue<int, vector<int>, greater<>> pq;

我按照引用文献所说的那样做了:

std::priority_queue<int, std::vector<int>, std::greater<int> > third (myints,myints+4);

为什么 CLion 会给我这个警告,我应该使用什么代码?

最佳答案

Why is CLion giving me this warning

警告消息对此进行了解释。首先,它以 Clang-Tidy 开头: 这意味着该建议是由 Clang-Tidy 静态分析工具生成的。

其次,它继续 Prefer transparent functors :这意味着 Clang-Tidy 建议您应该使用透明(比较)仿函数而不是非透明仿函数。它还演示了如何声明透明比较仿函数:'greater<>' .

what code should I use?

理想情况下,在这种情况下遵循 Clang-Tidy 的建议是个好主意,除非您的目标是标准容器不支持透明比较仿函数的旧的 C++14 之前的系统。

虽然透明仿函数的核心优势,即避免在查找时构造元素类型的大型对象并不适用(因为 int 很小且构造速度很快),但透明仿函数避免了不必要的元素类型重复,这提高了可维护性。

有关该主题的更多详细信息和基本原理,请参阅 this standard proposal (n3421)关于透明仿函数,this proposal (n3465)关于异构查找,and this one (n3657)本质上是在 n3421 的帮助下修改了 n3465。

关于c++ - 为什么 CLion 给我警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376691/

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