C++ begin() / end() functions
begin and end is the most important iterator function in vector, used toget the container's beginning and ending iterators.
begin and end are member functions of the container class:
beginreturns aniteratorendreturns aniterator(sentinel iterator)
These two functions are the foundation of C++ standard library algorithms and range-based for loops.
Word meaning: begin means 'begin',end means 'end'.
Basic syntax and parameters
begin and end are member functions of the container class; calling them requires no parameters.
Syntax format
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
Parameter Description
- Parameters: No parameters
- These functions take no arguments.
Function description
- Return Value:
begin()returns an iterator pointing to the first element.end()returns an iterator pointing to the position after the last element (sentinel).
- effect: The returned iterators can be used to traverse the container or work with algorithms.
- Note:
end()The iterator returned by points to a 'virtual' position that cannot be dereferenced and is only used for comparison.
Example
Let us thoroughly master begin and end through a series of examples from simple to complex.
Example 1: Basic usage - traversing the container
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
};
// Traverse using iterators
std
::
cout
<<
"Traverse using iterators: "
;
for
(
auto
it
=
numbers.
begin
(
)
;
it
!
=
numbers.
end
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
使用迭代器遍历: 10 20 30 40 50
Code analysis:
numbers.begin()returns an iterator pointing to the first element (10).numbers.end()returns an iterator pointing to the position after the last element.- The loop condition
it != numbers.end()ensures all elements are traversed.
Example 2: Range-based for loop
The range-based for loop introduced in C++11 uses under the hood begin and end.
Example
#include
#include
#include
int
main
(
)
{
std
::
vector
<
std
::
string
names
=
{
"Alice"
,
"Bob"
,
"Charlie"
}
;
// Range-based for loop (uses begin/end under the hood)
std
::
cout
<<
"Using range-based for loop: "
;
for
(
const
auto
&
name
:
names
)
{
std
::
cout
<<
name
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
使用范围 for 循环: Alice Bob Charlie
Code analysis:
- The range-based for loop is a concise way of using iterators.
- The compiler automatically calls
begin()andend().
Example 3: Using STL algorithms
Standard library algorithms need iterators to specify ranges.
Example
#include
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
5
,
2
,
8
,
1
,
9
,
3
};
// Sort
std
::
sort
(
numbers.
begin
(
)
, numbers.
end
(
)
)
;
std
::
cout
<<
"After sorting: "
;
for
(
int
n
:
numbers
)
{
std
::
cout
<<
n
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Find element
auto
it
=
std
::
find
(
numbers.
begin
(
)
, numbers.
end
(
)
,
8
)
;
if
(
it
!
=
numbers.
end
(
)
)
{
std
::
cout
<<
"Found element: "
<<
it
<<
std
::
endl
;
}
return
0
;
}
Expected output:
排序后: 1 2 3 5 8 9
找到元素: 8
Code analysis:
std::sort(begin, end)uses iterators to specify the sorting range.std::find(begin, end, value)uses iterators to specify the search range.
Example 4: Reverse iterators
Use rbegin and rend can traverse the container in reverse.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
1
,
2
,
3
,
4
,
5
};
// Forward traversal
std
::
cout
<<
"Forward: "
;
for
(
auto
it
=
numbers.
begin
(
)
;
it
!
=
numbers.
end
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Reverse traversal (using rbegin/rend)
std
::
cout
<<
"Reverse: "
;
for
(
auto
it
=
numbers.
rbegin
(
)
;
it
!
=
numbers.
rend
(
)
;
++
it
)
{
std
::
cout
<<
it
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
正向: 1 2 3 4 5
反向: 5 4 3 2 1
Code analysis:
rbegin()returns a reverse iterator pointing to the last element.rend()returns a reverse iterator pointing to before the first element.
Example 5: Iterator arithmetic
Iterators support arithmetic operations, allowing quick jumps to a specified position.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
,
60
,
70
};
// Get the third element (index 2)
auto
it
=
numbers.
begin
(
)
2
;
std
::
cout
<<
"begin() + 2 = "
<<
it
<<
std
::
endl
;
// Compute the distance between two iterators
auto
first
=
numbers.
begin
(
)
;
auto
last
=
numbers.
end
(
)
;
auto
distance
=
last
first
;
std
::
cout
<<
"Container size (distance): "
<<
distance
<<
std
::
endl
;
// Get the middle element
auto
mid
=
numbers.
begin
(
)
(
numbers.
size
(
)
/
2
)
;
std
::
cout
<<
"Middle element: "
<<
mid
<<
std
::
endl
;
return
0
;
}
Expected output:
begin() + 2 = 30
容器大小(距离): 7
中间元素: 40
Code analysis:
- Iterators support
+,-and other arithmetic operations. - You can use the
end() - begin()computes the container size.
Example 6: Conversion between iterators and indices
Iterators and indices can be converted to each other.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
};
// Convert an index to an iterator
size_t
index
=
2
;
auto
it
=
numbers.
begin
(
)
index
;
std
::
cout
<<
"Index "
<<
index
<<
" corresponding element: "
<<
it
<<
std
::
endl
;
// Convert an iterator to an index
auto
iter
=
numbers.
begin
(
)
3
;
size_t
idx
=
iter
numbers.
begin
(
)
;
std
::
cout
<<
"Index corresponding to the iterator: "
<<
idx
<<
std
::
endl
;
return
0
;
}
Expected output:
索引 2 对应的元素: 30
迭代器对应的索引: 3
Code analysis:
begin() + indexconverts an index to an iterator.iter - begin()converts an iterator to an index.
Example 7: Use an iterator to modify elements
The iterator returns a reference that can be used to modify elements.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
1
,
2
,
3
,
4
,
5
};
std
::
cout
<<
"Before modification: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
// Use iterators to multiply all elements by 2
for
(
auto
it
=
numbers.
begin
(
)
;
it
!
=
numbers.
end
(
)
;
++
it
)
{
it
=
2
;
}
std
::
cout
<<
"After modification: "
;
for
(
int
n
:
numbers
)
std
::
cout
<<
n
<<
" "
;
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
修改前: 1 2 3 4 5
修改后: 2 4 6 8 10
Code analysis:
- Iterator dereference (
*it) returns a reference to the element. - You can directly modify this reference to change the element's value.
The C++ container class