简体   繁体   English

C++:如何从定义的容器对象中获取迭代器类型

[英]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.

相关问题 从C ++(STL)中的(it)迭代器类型获取容器类型 - Obtain container type from (its) iterator type in C++ (STL) C ++如何从迭代器获取向量中对象的地址 - c++ how to get address of an object in a vector from iterator 如何确定C ++模板函数中迭代器指向的对象的类型? - How to determine the type of the object an iterator is pointing to in c++ template function? 是否可以从任意迭代器(C ++)获取值类型? - Is it possible to get the value type from an arbitrary iterator (C++)? 使用String反向迭代器和迭代器作为相同类型的对象(C ++) - Use String reverse iterator and iterator as same type of object (C++) 我可以从迭代器中获取容器 object 吗? - Can I get a container object from an iterator? 如何从 C++ 中的迭代器中检索值类型? - How to retrieve value type from iterator in C++? 如何获得对容器中某个对象的迭代器? - How to get iterator to a certain object in a container? c++ 使用概念的具有特定值类型的任何容器的迭代器 - c++ iterator of any container with specific value type using concepts C ++-在期望类型为Iterator的向量(或其他容器)构造函数中使用数组/指针。 那怎么可能? - C++ - Using array/pointers in vector (or other container) constructor that expects type Iterator. How is that possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM