CS/C++

[Mastering C++ Programming] - Iterators

sliver__ 2022. 12. 3. 11:27
728x90
  • 개발하는 입장에서 STL 내부의 data가 어디에 저장되고 iterator가 어떻게 순회하는지 몰라도 됀다.
  • iterator의 순회는 아래와 같다.

 

  • STL은 begin(), end() API를 제공한다.
  • begin() 첫 번째 element를 가리키는 iterator를 반환하고 end() 는 마지막 element 다음을 가리키는 itereator를 반환한다.

 

  • iterator는 크게 5가지 종류가 있다.
The type of iterator Description
Input iterator It is used to read from the pointed element
It is valid for single-time navigation, and once it reaches the end of the container, the iterator will be invalidated
It supports pre- and post-increment operators
It does not support decrement operators
It supports dereferencing
It supports the == and != operators to compare with the other iterators
The istream_iterator iterator is an input iterator
All the containers support this iterator
Output iterator It is used to modify the pointed element
It is valid for single-time navigation, and once it reaches the end of the container, the iterator will be invalidated
It supports pre- and post-increment operators
It does not support decrement operators
It supports dereferencing
It doesn't support the == and != operators
The ostream_iterator, back_inserter, front_inserter iterators are examples of output iterators
All the containers support this iterator
Forward iterator It supports the input iterator and output iterator functionalities
It allows multi-pass navigation
It supports pre-increment and post-increment operators
It supports dereferencing
The forward_list container supports forward iterators
Bidirectional iterator It is a forward iterator that supports navigation in both directions
It allows multi-pass navigation
It supports pre-increment and post-increment operators
It supports pre-decrement and post-decrement operators
It supports dereferencing
It supports the [] operator
The list, set, map, multiset, and multimap containers support bidirectional iterators
Random-access iterator Elements can be accessed using an arbitrary offset position
It supports pre-increment and post-increment operators
It supports pre-decrement and post-decrement operators
It supports dereferencing
It is the most functionally complete iterator, as it supports all the functionalities of the other types of iterators listed previously
The array, vector, and deque containers support random-access iterators
A container that supports random access will naturally support bidirectional and other types of iterators
728x90