c++ - make_heap和pop_heap可以,但是push_heap不能

标签 c++ visual-studio c++11 heap clion

我只是遇到了一个奇怪的问题,该问题仅在带有Clion的MSVC上发生,而在其他编译器上没有发生(我在Linux和Visual Studio上尝试了gcc,但在相同的代码下都没有这种问题)。
使用以下代码:

#include <vector>
#include <algorithm>
using namespace std;
int main() {
    vector<int>v = {1,2,3,4,5};
    make_heap(v.begin(), v.end());
    v.push_back(6);
    push_heap(v.begin(), v.end());
}
错误“在实例化功能模板专门化'std::push_heapstd::_ Vector_iterator >>'std::indirect_ready_traitsstd::__ Vector_iterator >>'“随后将显示
enter image description here
是Clion或MSVC的错误吗?
附言
我仍然可以构建和运行它,因此它可能不是编译器错误。 (让我更加困惑)

最佳答案

看来您无法使用以下命令初始化vector:

vector<int>v = {1,2,3,4,5}; 
更改为:
vector<int> vect{ 1, 2, 3, 4, 5 };
编译并运行代码,看看它是否仍然有问题。
编辑:
有人说这不太可能,但是请查看链接:
What is the easiest way to initialize a std::vector with hardcoded elements?
如果向下滚动到第二个答案,它将显示:
If your compiler supports C++11, you can simply do:
std::vector<int> v = {1, 2, 3, 4};
由于您没有告诉我们您的编译器版本和环境,因此很难确定这是否是问题所在。另请注意:
This is available in GCC as of version 4.4. 
Unfortunately, VC++ 2010 seems to be lagging behind in this respect.
因此,如果您使用的是VC++的旧版本,那么您就不走运了...

关于c++ - make_heap和pop_heap可以,但是push_heap不能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62964237/

相关文章:

c++ - 获取特定日期的时差

c++ - 通过索引获取 __m128 的成员?

c++ - 如何从函数指针和参数调用C++中的函数

c++ - std::unique_ptr 中的不可复制删除器

c++ - boost 范围 weak_ptr

visual-studio - 如何在我的方法名称单词之间自动插入下划线?

visual-studio - 在授权 header visual studio 2013 负载测试中向变量添加文本

javascript - 在 Visual Studio 2015 中,有没有办法使用 Internet Explorer 以外的浏览器调试 Javascript?

c++ - 在 Ubuntu 16 上使用 clang++ 编译一个基本的 c++ 程序

c++ - 重载采用 chrono::duration 的函数