简体   繁体   English

make_heap 和 pop_heap 可以,但 push_heap 不行

[英]make_heap and pop_heap are OK but push_heap not

I just came across a weird problem that happens ONLY on MSVC with Clion but not on other compilers(I tried gcc on Linux and Visual Studio both no such problem, with the same code).我刚刚遇到一个奇怪的问题,它只发生在带有 Clion 的 MSVC 上,但不会发生在其他编译器上(我在 Linux 上尝试了 gcc,而 Visual Studio 都没有这样的问题,代码相同)。

With these codes:使用这些代码:

#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());
}

an error "In instantiation of function template specialization 'std::push_heapstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >' no type named 'value_type' in 'std::indirectly_readable_traitsstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >'" will then be shown错误“在 function 模板专业化的实例化中‘std::push_heapstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >’在‘std::indirectly_readable_traitsstd::_Vector_iterator<std’中没有名为‘value_type’的类型::_Vector_val<std::_Simple_types<int > > >'" 然后将显示

在此处输入图像描述

is it a bug of Clion or MSVC?是 Clion 还是 MSVC 的错误?

PS I can still build and run it so it might not be a compiler error; PS 我仍然可以构建并运行它,所以它可能不是编译器错误; (Making me even more confused) (让我更糊涂了)

It looks like you cannot intialize vector with the following command:看起来您无法使用以下命令初始化向量:

vector<int>v = {1,2,3,4,5}; 

Change it to:将其更改为:

vector<int> vect{ 1, 2, 3, 4, 5 };

Compile and run the code and see if it still has problems.编译并运行代码,看看是否还有问题。

EDIT: Some people are saying it is unlikely however look at the link: What is the easiest way to initialize a std::vector with hardcoded elements?编辑:有些人说这不太可能,但请查看链接: What is the easiest way to initialize a std::vector with hardcoded elements?

If you scroll down to the second answer it says:如果您向下滚动到第二个答案,它会显示:

If your compiler supports C++11, you can simply do:
std::vector<int> v = {1, 2, 3, 4};

As you did not tell us your compiler version and environment it is very hard to determine if this is the problem.由于您没有告诉我们您的编译器版本和环境,因此很难确定这是否是问题所在。 Also note that:另请注意:

This is available in GCC as of version 4.4. 
Unfortunately, VC++ 2010 seems to be lagging behind in this respect.

So if you are using an older version of VC++ then you are out of luck...因此,如果您使用的是旧版本的 VC++,那么您就不走运了……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM