![](/img/trans.png)
[英]INVALID HEAP when using STL make_heap / push_heap / pop_heap
[英]make_heap and pop_heap are OK but push_heap not
我刚刚遇到一个奇怪的问题,它只发生在带有 Clion 的 MSVC 上,但不会发生在其他编译器上(我在 Linux 上尝试了 gcc,而 Visual Studio 都没有这样的问题,代码相同)。
使用这些代码:
#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());
}
错误“在 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 > > >'" 然后将显示
是 Clion 还是 MSVC 的错误?
PS 我仍然可以构建并运行它,所以它可能不是编译器错误; (让我更糊涂了)
看起来您无法使用以下命令初始化向量:
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++,那么您就不走运了……
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.