C++ Iterators (1)
C++ Iterators (1)
Iterators
- Love Babbar
·
& Iterators
- Love Babbar
crantor
it
#01/
C++ Iterators
&
• An iterator is a pointer-like object representing an element's position in a container. It is used to iterate over elements in a container.
=
-
• Suppose we have a vector named nums of size 4. Then, begin() and end()
are member functions that return iterators pointing to the beginning and
end of the vector respectively.
--• nums.begin() points to the first element in the vector i.e 0th index
• nums.begin() + i points to the element at the ith index.
• nums.end() points to one element past the final element in the vector
I
&-
-
i
-
&
itator nwns
end()
l
begin
-
Ye
>
-
nun
-
10 20
nurs. begin() #
+2
-
ends
nums
Do
Creating & Traversing - Iterators
-
-
-
-
- &
& -
-
=
-
⑮
L its =
it Y and
Iterator Operations
-
-
-
- v 0
20
I
30
2
no
3
its - m
or &
·
⑭
(i
Al +)om
+
its >
- iterator
Eiteog O
, its
erbein()
=
=
its ++
d
&
J
pair)
A k v inL
it -
first it
d
locatio adding
it s
As pair) edit) &
&
k -
v e second
second its A
-
&
it
A&
A
yair
Ake
& value at
where
position
its is
pair
<
k + j pointing
Iterator Types
~ -
- -
-
-
- -
-
-
-1 -
- -
-
-
-
- - -
-
-
T A 1 -
1
Input Iterator - donly=>
-
>
-
forward moving
•
These iterators can only be used for reading values from a container in a forward direction. They are typically used for
&&
algorithms that need to read data from a container, such as std::find or std::for_each.
&
-
Output Iterator i formed only moving
-
>
-
• These iterators can only be used for writing values to a container in a forward direction. They are less commonly used
compared to other iterator types.
-
- V
Forward Iterator
-
I Di
&
forward -
-
read T
• These iterators combine the capabilities of both input and output iterators. They allow reading and writing values in a
forward direction. Many container types, like lists, support forward iterators.
list. end()
begin
&
I I
list-
-
-
-D &
·
A 7 -
-
-
T
X
X
-
>
- list/& Forward &
-> it t
Site
I
Tail Intend
i-
gil ,
Head
- F
2 -
d -
#
&
A
3 -
18 >
- 20 >
2
>
-
30
&
-E
-
&
-
-
Random Access Iterator
>
- -porit/ forward/
read
Random
-
-
Backmail
•
These iterators offer full navigation capabilities, allowing you to move to any element within a container in constant
don
time. Vectors, arrays, and deques provide random access iterators
-
-
+ 3
&
it
to
*
* or
be begin
-
- begin &
by
-
- ⑳ - ) +
u
it
*
101201 so 50
0 2 Y
Operations supported by Iterators
Why use Iterators ?
• Working with Algorithms: C++ has many ready-to-use algorithms like finding
Be
elements, sorting, and summing values. Iterators help you apply these
algorithms to different types of data containers like arrays or lists.
• Saving Memory: Instead of loading a huge set of data all at once, iterators let
you deal with one item at a time, which saves memory.
• Uniform Approach: Iterators allow you to interact with different kinds of data
containers (like vectors or sets) in the same way. This makes your code more
consistent and easier to manage.
• Simpler Code: By using iterators, a lot of the repetitive details of going through
data are taken care of, making your code cleaner and easier to read.
Why use Iterators ?
• Working with Algorithms: C++ has many ready-to-use algorithms like finding
elements, sorting, and summing values. Iterators help you apply these
algorithms to different types of data containers like arrays or lists.
• Saving Memory: Instead of loading a huge set of data all at once, iterators let
you deal with one item at a time, which saves memory.
• Uniform Approach: Iterators allow you to interact with different kinds of data
containers (like vectors or sets) in the same way. This makes your code more
consistent and easier to manage.
• Simpler Code: By using iterators, a lot of the repetitive details of going through
data are taken care of, making your code cleaner and easier to read.
Why use Iterators ?
• Working with Algorithms: C++ has many ready-to-use algorithms like finding
elements, sorting, and summing values. Iterators help you apply these
algorithms to different types of data containers like arrays or lists.
• Saving Memory: Instead of loading a huge set of data all at once, iterators let
you deal with one item at a time, which saves memory.
• Uniform Approach: Iterators allow you to interact with different kinds of data
containers (like vectors or sets) in the same way. This makes your code more
consistent and easier to manage.
• Simpler Code: By using iterators, a lot of the repetitive details of going through
data are taken care of, making your code cleaner and easier to read.