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

C++ at() function

C++ at() function

C++ Container Class The C++ container class


Among all ways to access array elements,at is the safest one, because it providesbounds checking.

at is a member function of the container class, used toreturn the element at the specified position, while performing bounds checking. If the index is out of range, it throws std::out_of_range exception.

at implements safe element access, allowing you to catch and handle out-of-bounds errors at runtime.

Word meaning: at means 'at... position', i.e., to get the element at the specified position.


Basic syntax and parameters

at is a member function of the container class, so you need to have a container object first, then use the dot operator . to call it.

Syntax format



reference at(size_type pos);

const_reference at(size_type pos) const;

Parameter Description

- Type: `size_type` (unsigned integer type, usually `size_t`) - Description: The position (index) of the element to access. Indexing starts at 0, and the maximum valid index is `size() - 1`.

Function description


Example

Let us thoroughly master at through a series of examples from simple to complex.

Example 1: Basic usage - accessing elements

Example

#include

#include

#include

int

main

(

)

{

   

// 1. Create and initialize an array

    std

::

array

<

int

,

5

numbers

=

{

10

,

20

,

30

,

40

,

50

}

;

    std

::

cout

<<

"array size is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 2. Use at to access elements

    std

::

cout

<<

"First element (at(0)): "

<<

numbers.

at

(

0

)

<<

std

::

endl

;

    std

::

cout

<<

"Second element (at(1)): "

<<

numbers.

at

(

1

)

<<

std

::

endl

;

    std

::

cout

<<

"Third element (at(2)): "

<<

numbers.

at

(

2

)

<<

std

::

endl

;

    std

::

cout

<<

"Last element (at(4)): "

<<

numbers.

at

(

4

)

<<

std

::

endl

;

   

// 3. Use a loop to access all elements

    std

::

cout

<<

"All elements: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers.

at

(

i

)

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

array的大小是: 5

第一个元素 (at(0)): 10

第二个元素 (at(1)): 20

第三个元素 (at(2)): 30

最后一个元素 (at(4)): 50

所有元素: 10 20 30 40 50

Code analysis:

  1. numbers.at(0) returns the first element 10(indexing starts at 0).
  2. numbers.at(4) returns the last element 50(because size() is 5, and the valid indices are 0-4).
  3. Using a loop and at can safely traverse all elements.

Example 2: Catching out-of-bounds exceptions

at The main advantage of is that it can throw exceptions, allowing us to handle out-of-bounds errors.

Example

#include

#include

#include

int

main

(

)

{

    std

::

array

<

int

,

3

numbers

=

{

10

,

20

,

30

}

;

   

// Try to access an out-of-bounds position

   

try

{

        std

::

cout

<<

"Trying to access at(10)..."

<<

std

::

endl

;

       

int

value

=

numbers.

at

(

10

)

;

// This will throw an exception

        std

::

cout

<<

"Value: "

<<

value

<<

std

::

endl

;

   

}

   

catch

(

const

std

::

out_of_range

&

e

)

{

        std

::

cout

<<

"Caught exception: "

<<

e.

what

(

)

<<

std

::

endl

;

   

}

   

// Normal access

   

try

{

        std

::

cout

<<

"Accessing at(2): "

<<

numbers.

at

(

2

)

<<

std

::

endl

;

   

}

   

catch

(

const

std

::

out_of_range

&

e

)

{

        std

::

cout

<<

"Caught exception: "

<<

e.

what

(

)

<<

std

::

endl

;

   

}

   

return

0

;

}

Expected output:

尝试访问 at(10)...

捕获到异常: array::at: __pos (which is 10) >= _Nm (which is 3)

访问 at(2): 30

Code analysis:

Example 3: Modifying element values

at returns a reference, so it can be used to modify the element's value.

Example

#include

#include

int

main

(

)

{

    std

::

array

<

int

,

3

numbers

=

{

10

,

20

,

30

}

;

    std

::

cout

<<

"Before modification: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers.

at

(

i

)

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Use at() to get a reference and modify the element

    numbers.

at

(

0

)

=

100

;

    numbers.

at

(

1

)

=

200

;

    numbers.

at

(

2

)

=

300

;

    std

::

cout

<<

"After modification: "

;

   

for

(

size_t

i

=

0

;

i

<

numbers.

size

(

)

;

++

i

)

{

        std

::

cout

<<

numbers.

at

(

i

)

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

修改前: 10 20 30

修改后: 100 200 300

Code analysis:


C++ Container Class The C++ container class