简体   繁体   中英

Iterator and *Iterator type

if I have an iterator of some container with unknown class within it, can I declare a variable of that unknown type by:

*Iterator unknown_type_var;

The unknown_type has default ctor, so that isn't the issue. But do a method like this exist ? Or is there any other method to declare a variable of a type that I have an iterator of container, which holds this type.

If it's a standard-conforming container it has a nested typedef named value_type that gives the type of the contained values. That typedef is also available through iterator_traits: std::iterator_traits<iterator_type>::value_type . And the iterator type is available as, you guessed it, a nested typedef in the container.

如果您没有可用的value_type ,并且您不能使用auto因为您没有立即分配(尝试找到方法),那么您可以使用decltype:

decltype(*declval<Iterator>()) unknown_type_var;

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