Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Concept RandomAccessIterator 隨機訪問迭代器概念

RandomAccessIterator 隨機訪問迭代器

Description 說明

A random access iterator is an iterator that can read through a sequence of values. It can move in either direction through the sequence (by any amount in constant time), and can be either mutable (data pointed to by it can be changed) or not mutable.
隨機訪問迭代器是一種可以讀入一組值的序列的迭代器。它可以從兩個方向遍歷序列(在常量時間內跳過任意數量),可以是可寫的(所指數據可以改變)或不可寫的。

An iterator represents a position in a sequence. Therefore, the iterator can point into the sequence (returning a value when dereferenced and being incrementable), or be off-the-end (and not dereferenceable or incrementable).
迭代器表示了在一個序列中的某個位置。因此,迭代器可以指向序列內部(在提領時返回一個值且可以遞增),或者指向序列末端之後(不可提領且不可遞 增)

Refinement of 精化自

Associated types 關聯類型

  • value_type

    std::iterator_traits<Iter>::value_type

    The value type of the iterator
    迭代器的值類型

  • category

    std::iterator_traits<Iter>::iterator_category

    The category of the iterator
    迭代器的類別

  • difference_type

    std::iterator_traits<Iter>::difference_type

    The difference type of the iterator (measure of the number of steps between two iterators)
    迭代器的距離類型(以兩個迭代器間的步數來測量)

Notation 符號

Iter
A type playing the role of iterator-type in the RandomAccessIterator concept.
隨機訪問迭代器 概念中擔任迭代器類型角色的類型。
i, j
Objects of type Iter
類型 Iter 的對象
x
Object of type value_type
類型 value_type 的對象
n
Object of type difference_type
類型 difference_type 的對象
int_off
Object of type int
類型 int 的對象

Type expressions 類型表達式

Category tag 類別標記

category must be derived from std::random_access_iterator_tag.
category 必須派生自 stdrandom_access_iterator_tag。

Valid expressions 有效表達式

Name 名字 Expression 表達式 Type 類型 Semantics 語義

Motion 移動

i += n

Iter &

Equivalent to applying i++ n times if n is positive, applying i-- -n times if n is negative, and to a null operation if n is zero.

如果 n 為正,則等價於執行 ni++,如果 n 為負,則等價於執行 -ni-- ,如果 n 為零則無操作。

Motion (with integer offset)

移動(以整數偏移量)

i += int_off

Iter &

Equivalent to applying i++ n times if n is positive, applying i-- -n times if n is negative, and to a null operation if n is zero.
如果 n 為正,則等價於執行 ni++,如果 n 為負,則等價於執行 -ni-- ,如果 n 為零則無操作。

Subtractive motion

負移動

i -= n

Iter &

Equivalent to i+=(-n)

等價於 i+=(-n)

Subtractive motion (with integer offset)
負移動(以整數偏移量)

i -= int_off

Iter &

Equivalent to i+=(-n)

等價於 i+=(-n)

Addition 加法

i + n

Iter

Equivalent to {Iter j = i; j += n; return j;}

等價於 {Iter j = i; j += n; return j;}

Addition with integer

加整數

i + int_off

Iter

Equivalent to {Iter j = i; j += n; return j;}

等價於 {Iter j = i; j += n; return j;}

Addition (count first)

加法(數量在前)

n + i

Iter

Equivalent to i + n

等價於 i + n

Addition with integer (count first)

加整數(數量在前)

int_off + i

Iter

Equivalent to i + n
等價於
i + n

Subtraction 減法

i - n

Iter

Equivalent to i + (-n)

等價於 i + (-n)

Subtraction with integer

減整數

i - int_off

Iter

Equivalent to i + (-n)
等價於
i + (-n)

Distance 距離

i - j

difference_type

The number of times i must be incremented (or decremented if the result is negative) to reach j. Not defined if j is not reachable from i.

i 遞增(或遞減,如果結果為負)至 j 的次數。如果 j 不可從 i 到達則無定義。

Element access

元素訪問

i[n]

const-if-not-mutable value_type &

Equivalent to *(i + n)

等價於 *(i + n)

Element access with integer index

以整數索引訪問元素

i[int_off]

const-if-not-mutable value_type &

Equivalent to *(i + n)
等價於
*(i + n)

Complexity 複雜度

All iterator operations must take amortized constant time.
迭代器的所有操作必須為分期常量時間複雜度。

Models 模型

  • T *
  • std::vector<T>::iterator
  • std::vector<T>::const_iterator
  • std::deque<T>::iterator
  • std::deque<T>::const_iterator

See also 參見


PrevUpHomeNext