繁体   English   中英

给定一个迭代器,使用正确的size_type声明一个索引

[英]given an iterator, declare an index with correct size_type

在C ++模板中,我很难定义具有正确的size_type的变量。 基本上,这将是容器的索引类型。 我知道int可以工作,但希望以干净的形式进行处理。

template<typename ForwardIt> 
void test(ForwardIt it) {
    // this yields the underlying type
    decltype(typename std::iterator_traits<ForwardIt>::value_type()) T;

    // what I would like is something like above, but yielding the
    // correct size_type for the container.

    // Something like this but with the type extracted from ForwardIt:
    std::vector<int>::size_type i; 

    ...
}

详细说明@NathanOliver所说的:迭代器没有size_type 它们具有一个difference_type ,代表两个迭代器之间的距离。 容器具有size_type

迭代器不必具有关联的容器,因此无法仅从迭代器获取“容器的size_type ”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM