【问题标题】:what is the iterator type of map<T, U>.begin()map<T, U>.begin() 的迭代器类型是什么
【发布时间】:2013-05-27 23:46:12
【问题描述】:
template <class InputIterator, class Distance>
  void advance (InputIterator& it, Distance n);

    InputOutput < Forward < Bidirectional < Random Access

map<int, int> mapInts;
...

std::map<int, int>::iterator it = mapInts.begin();
std::advance (it,5);

Q> map.begin 返回的迭代器类型是什么? InputOut、Forward、Bidirectional?

谢谢

【问题讨论】:

    标签: c++


    【解决方案1】:

    来自Map::begin() documentation

    std::map.begin()

    将迭代器返回到开头 返回引用地图容器中第一个元素的迭代器。

    由于地图容器始终保持其元素有序,因此 begin 指向遵循容器排序标准的最先出现的元素。

    如果容器为空,则返回的迭代器值不应被解引用。

    返回值: 指向容器中第一个元素的迭代器。

    如果映射对象是 const 限定的,则该函数返回一个 const_iterator。否则,它返回一个迭代器。

    成员类型iterator 和const_iterator 是双向迭代器 类型,指向元素(类型为value_type)。 请注意,map 容器中的 value_type 是 pair 的别名。

    编辑:@Andy Prowl 提供: 根据 C++ 标准 § 23.2.4/6

    关联容器的迭代器属于双向迭代器类别

    【讨论】:

    • +1,打败我。您可能还想引用第 23.2.4/6 节,一般来说:“iterator of an associative container 属于双向迭代器类别
    猜你喜欢
    • 2013-03-12
    • 1970-01-01
    • 2013-10-25
    • 2012-05-29
    • 2020-10-14
    • 2010-10-04
    相关资源
    最近更新 更多