为什么range构造函数的iTerator类型Priority_queue是错误的?
代码如下。
#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;
typedef unordered_map<int,int>::iterator myIt;
class cmpHelper {
public:
bool operator()(myIt l, myIt r){return l->second > r->second;}
};
int main(int argc, char* argv[]){
unordered_map<int,int> freq_cnt({{3,1},{2,4},{5,2}});
priority_queue<myIt,vector<myIt>, cmpHelper> h(freq_cnt.begin(), freq_cnt.end());
//priority_queue<myIt, vector<myIt>, cmpHelper> h;
}
并在此显示相应的编译器信息
g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
默认构造函数是可以的。我试图通过目录 /usr/includs/c ++/7/bits/ 浏览代码,但仍然没有任何想法。请帮助指出问题是什么。谢谢。
The code is as following.
#include <iostream>
#include <queue>
#include <unordered_map>
using namespace std;
typedef unordered_map<int,int>::iterator myIt;
class cmpHelper {
public:
bool operator()(myIt l, myIt r){return l->second > r->second;}
};
int main(int argc, char* argv[]){
unordered_map<int,int> freq_cnt({{3,1},{2,4},{5,2}});
priority_queue<myIt,vector<myIt>, cmpHelper> h(freq_cnt.begin(), freq_cnt.end());
//priority_queue<myIt, vector<myIt>, cmpHelper> h;
}
and hereunder shows corresponding compiler info
g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Default constructor is OK. And I tried to go through code under directory /usr/include/c++/7/bits/, but still no any idea. Please help point out what the problem is. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[freq_cnt.begin(),freq_cnt.end())
是std :: pair&lt&const&const&int,int&gt;
lvalues的范围,int&gt; :: iterator 值。您可以使用C ++ 20的
std :: ranges :: iota_view
:否则,您可以默认构造队列并推动每个元素:
但请考虑具有参考的队列:
[freq_cnt.begin(), freq_cnt.end())
is a range ofstd::pair<const int, int>
lvalues, notunordered_map<int,int>::iterator
values.You can create a range of these iterators with C++20's
std::ranges::iota_view
:Otherwise, you can default construct the queue and push each element:
But consider instead having a queue of references:
这是因为将迭代器 dereferences的构造函数迭代器填充容器。
而是将实际的迭代器放入容器中:
您要做的事情与此相似:
进一步澄清。检查这个简化的示例:
您使用迭代器通过 dereferencing (
*beg
上面)填充容器。当使用迭代器时,您将获得IT 点的值。因此,当您将类型std :: vector&lt; int&gt; :: Iterator
的迭代器放置时,您将获得对int
的引用。如果将迭代器包裹在迭代器中,则当重新推荐返回原始迭代器时,可以使用这些迭代器构建
PRIRESITION_QUEUE
。示例:
然后
It's because the constructor that takes iterators dereferences the iterators to populate the container.
Put the actual iterators in the container instead:
What you are trying to do would be similar to this:
To clarify further. Check this simplified example:
You use iterators to populate containers by dereferencing (
*beg
above) the iterators. When dereferencing an iterator, you get the value it points at. So, when you dereference an iterator of typestd::vector<int>::iterator
you get a reference to anint
.If you wrap your iterators in iterators that when dereferenced return the original iterators, you can construct the
priority_queue
using those.Example:
then