Cheshire Cat
Cheshire Cat

Reputation: 335

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.

Upvotes: 4

Views: 2438

Answers (1)

songyuanyao
songyuanyao

Reputation: 172934

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;

Upvotes: 10

Related Questions