Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ begin() / end() functions

C++ begin() / end() functions

C++ Container Class The C++ container class


begin and end are the most important iterator functions in array, used toget the container's beginning and ending iterators.

begin and end are member functions of the container class:

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() noexcept;

const_iterator begin() const noexcept;



iterator end() noexcept;

const_iterator end() const noexcept;

Parameter Description

- These functions take no arguments.

Function description

- `begin()` returns an iterator pointing to the first element. - `end()` returns an iterator pointing to the position after the last element (sentinel).

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

::

array

<

int

,

5

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:

  1. numbers.begin() returns an iterator pointing to the first element (10).
  2. numbers.end() returns an iterator pointing to the position after the last element.
  3. 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

::

array

<

std

::

string

,

3

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:

Example 3: Using STL algorithms

Standard library algorithms need iterators to specify ranges.

Example

#include

#include

#include

int

main

(

)

{

    std

::

array

<

int

,

6

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:

Example 4: Reverse iterators

Use rbegin and rend can traverse the container in reverse.

Example

#include

#include

int

main

(

)

{

    std

::

array

<

int

,

5

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:

Example 5: Iterator arithmetic

Iterators support arithmetic operations, allowing quick jumps to a specified position.

Example

#include

#include

int

main

(

)

{

    std

::

array

<

int

,

7

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:

Example 6: Modifying elements using iterators

The iterator returns a reference that can be used to modify elements.

Example

#include

#include

int

main

(

)

{

    std

::

array

<

int

,

5

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:


C++ Container Class The C++ container class