简体   繁体   中英

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).

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

在此处输入图像描述

is it a bug of Clion or MSVC?

PS I can still build and run it so it might not be a compiler error; (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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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