【问题标题】:C++ get Index value in integer from iteratorC ++从迭代器获取整数索引值
【发布时间】:2013-08-10 05:54:42
【问题描述】:

我正在尝试从迭代器中获取索引值。但我不断收到错误“间接需要指针操作数('long'无效)” 任何的想法?我需要获取索引。在下面的例子中,它应该输出 2。

template<typename T>
void practice(T begin, T end) {
    T it = begin;
    it++;
    it++;
    auto index = it - begin;
    cout << *index;

【问题讨论】:

  • 为什么要取消引用索引(它本质上是不可分割的)?由于您接受任何类型的迭代器,请使用std::distance。我建议您这样做,即使您不这样做,以防以后发生变化。

标签: c++ templates pointers indexing iterator


【解决方案1】:
cout << index;

不需要* 取消引用。

【讨论】:

    猜你喜欢
    • 2010-10-14
    • 2011-05-12
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 2015-04-03
    • 2010-09-13
    相关资源
    最近更新 更多