Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 378
Cheshire Cat Avatar asked Dec 24 '22 23:12

Cheshire Cat


1 Answers

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 (i.e. unordered_set<pair<int,int>,CustomHash>).

list<decltype(Edges)::iterator> List; 
// same as list<unordered_set<pair<int,int>,CustomHash>::iterator> List;
like image 118
songyuanyao Avatar answered Jan 05 '23 18:01

songyuanyao