Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Random Access Iterators | Iterator Fundamentals
C++ STL Iterators

bookRandom Access Iterators

Deslize para mostrar o menu

Note
Definition

A random access iterator is an iterator that supports direct element access and arithmetic operations, such as adding or subtracting an integer to move multiple positions at once, and accessing elements by index using the [] operator.

Random access iterators are the most powerful iterator type in C++. They support moving forward and backward by arbitrary offsets, comparing distances, and accessing elements with the [] operator, similar to raw pointers. Containers like std::vector and std::deque provide random access iterators, while containers such as std::list, std::set, and std::map do not, because their internal structures do not allow efficient direct access by position.

main.cpp

main.cpp

copy
12345678910111213141516171819
#include <iostream> #include <vector> int main() { std::vector<int> numbers{10, 20, 30, 40, 50}; // Iterator arithmetic auto it = numbers.begin(); it = it + 3; // Move forward by 3 positions std::cout << "Element at position 3: " << *it << '\n'; // Using the [] operator std::cout << "Element at position 2 using iterator[2]: " << it[-1] << '\n'; // Difference between iterators auto diff = numbers.end() - numbers.begin(); std::cout << "Distance between begin and end: " << diff << '\n'; }

This code shows random access iterator features with std::vector. Adding an offset to begin() jumps directly to a specific element, and the [] operator accesses elements relative to the iterator position. Subtracting iterators gives the distance between them. These operations are efficient because vectors store elements in contiguous memory, enabling fast random access.

main.cpp

main.cpp

copy
123456789101112131415
#include <list> #include <iostream> int main() { std::list<int> lst{1, 2, 3, 4, 5}; auto it = lst.begin(); // The following lines would cause compilation errors if uncommented: // it = it + 2; // Error: std::list iterator does not support + // int x = it[1]; // Error: std::list iterator does not support [] // auto diff = lst.end() - lst.begin(); // Error: operator- not supported std::cout << "std::list iterators do not support random access operations.\n"; }

Only containers like std::vector and std::deque provide random access iterators because their storage allows direct position calculation. Vectors support arithmetic and [] operations due to contiguous memory, while std::list uses a linked structure and does not support random access. As a result, using +, -, or [] with a list iterator causes compilation errors.

question mark

Which container provides random access iterators?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 7

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 7
some-alt