![](/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.