给定一个迭代器,用正确的 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

迭代器不需要关联容器,因此无法仅从迭代器获取 "the container's size_type"。