[英]C++: How to get iterator type from defined container object
I have a code In a form:我有一个代码形式:
unordered_set<pair<int,int>,CustomHash> Edges;
typedef unordered_set<pair<int,int>,CustomHash>::iterator EdgesIt;
...
for(auto it=Edges.begin();it!=Edges.end();it++){
list<EdgesIt> List;
}
etc. How can I avoid defining a new type EdgesIt to be used in List declaration and get it in some smarter way, for example:等。如何避免定义要在 List 声明中使用的新类型 EdgesIt 并以更智能的方式获取它,例如:
list<Edges::iterator_type> List;
InteliSense only suggests Edges::iterator
which is defined as typedef std::iterator pair<int,int> iterator
. InteliSense 只建议Edges::iterator
定义为typedef std::iterator pair<int,int> iterator
。 More to say, it doesn't work.更要说,它不起作用。 I also tried to use unordered_set::iterator
, but it also doesn't work.我也尝试使用unordered_set::iterator
,但它也不起作用。
Edges
is the name of object, not the name of the class. Edges
是对象的名称,而不是类的名称。 You can't get the nested typedef
from it directly like Edges::iterator
.您不能像Edges::iterator
那样直接从中获取嵌套的typedef
。
You can use decltype (since C++11) to get the type you want (ie unordered_set<pair<int,int>,CustomHash>
).您可以使用decltype (C++11 起)来获取您想要的类型(即unordered_set<pair<int,int>,CustomHash>
)。
list<decltype(Edges)::iterator> List;
// same as list<unordered_set<pair<int,int>,CustomHash>::iterator> List;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.