简体   繁体   中英

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<Edges::iterator_type> List;

InteliSense only suggests Edges::iterator which is defined as 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.

Edges is the name of object, not the name of the class. You can't get the nested typedef from it directly like Edges::iterator .

You can use decltype (since C++11) to get the type you want (ie unordered_set<pair<int,int>,CustomHash> ).

list<decltype(Edges)::iterator> List; 
// same as list<unordered_set<pair<int,int>,CustomHash>::iterator> List;

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