#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
auto cmp = [](int a, int b) {
return a > b;
};
class Test {
private:
std::priority_queue<int, std::vector<int>, decltype(cmp)> pq(cmp);
};
int main(int argc, char const *argv[])
{
Test test;
return 0;
}
However, when I use the priority_queue
in the main function, it can use the cmp.
Moreover, when I delete the 'cmp' in the 'pq'
auto cmp = [](int a, int b) {
return a > b;
};
class Test {
private:
std::priority_queue<int, std::vector<int>, decltype(cmp)> pq;
};
int main(int argc, char const *argv[])
{
Test test;
return 0;
}
the complier told me:
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
auto cmp = [](int a, int b) {
return a > b;
};
int main(int argc, char const *argv[])
{
std::priority_queue<int, std::vector<int>, decltype(cmp)> pq(cmp);
pq.push(1);
std::cout << pq.top() << std::endl;
return 0;
}
I do not know why? I know lambda is a anonymous class, but why in a class it cannot use, in the main function it can use?