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

The C++ container class

The C++ container class

The C++11 standard introduced header, which provides a fixed-size array container with better type safety and memory management characteristics compared to C-language arrays.

std::array is a template class in the C++ standard library, defined in the header.std::array template class provides a fixed-size array whose size is determined at compile time and cannot be changed dynamically.

Syntax

std::array The basic syntax is as follows:

#include 

std::array array_name;

Declaration and initialization

requires the size to be determined at compile time and cannot be changed dynamically. Example of use:

#include 
#include 

int main() {
    std::array arr = {1, 2, 3, 4, 5}; // 声明一个定长为5的int数组
    return 0;
}

Feature

Example

Below is an example using std::array with its output.

Example

#include

#include

int

main

(

)

{

   

// Create a std::array containing 5 integers

    std

::

array

<
int
,
5
myArray
=
{
1
,
2
,
3
,
4
,
5
}

;

   

// Traverse the array using a range-based for loop

   

for

(

const

auto

&

value

:

myArray

)

{

        std

::

cout

<<

value

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Access array elements using indices

    std

::

cout

<<

"Element at index 2: "

<<

myArray.

at

(

2

)

<<

std

::

endl

;

   

// Get the array size

    std

::

cout

<<

"Array size: "

<<

myArray.

size

(

)

<<

std

::

endl

;

   

// Modify array elements

    myArray

[

3

]

=

10

;

   

// Traverse the array again to show the modified elements

   

for

(

const

auto

&

value

:

myArray

)

{

        std

::

cout

<<

value

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Output result:

1 2 3 4 5 
Element at index 2: 3
Array size: 5
1 2 3 10 5 

Common member functions

The following are of include the following commonly used member functions:

function Description
at(size_t pos) returns the element at the specified position, with bounds checking
operator[] returns the element at the specified position, without bounds checking
front() Returns the first element of the array
back() Returns the last element of the array
data() Returns a pointer to the array data
size() Returns the array size (fixed)
fill(const T& value) Sets all array elements to a specified value
swap(array& other) Swaps the contents of two arrays
begin() / end() Returns the array's begin/end iterators

Example

1. Basic operations

Example

#include

#include

int

main

(

)

{

    std

::

array

<
int
,
5
arr
=
{
10
,
20
,
30
,
40
,
50
}

;

    std

::

cout

<<

"Array elements: "

;

   

for

(

int

i

=

0

;

i

<

arr.

size

(

)

;

++

i

)

{

        std

::

cout

<<

arr

[

i

]

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// Get the first and last elements

    std

::

cout

<<

"First element: "

<<

arr.

front

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Last element: "

<<

arr.

back

(

)

<<

std

::

endl

;

   

return

0

;

}

Using at with bounds checking

Example

#include

#include

int

main

(

)

{

    std

::

array

<
int
,
3
arr
=
{
1
,
2
,
3
}

;

   

try

{

        std

::

cout

<<

arr.

at

(

2

)

<<

std

::

endl

;

 

// Normal output

        std

::

cout

<<

arr.

at

(

5

)

<<

std

::

endl

;

 

// Out of range, throws exception

   

}

catch

(

const

std

::

out_of_range

&

e

)

{

        std

::

cout

<<

"Exception: "

<<

e.

what

(

)

<<

std

::

endl

;

   

}

   

return

0

;

}

3. Using fill to populate elements

Example

#include

#include

int

main

(

)

{

    std

::

array

<
int
,
5
arr

;

    arr.

fill

(

100

)

;

 

// Set all elements to 100

    std

::

cout

<<

"Filled array: "

;

   

for

(

const

auto

&

elem

:

arr

)

{

        std

::

cout

<<

elem

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

4. Array swapping

Example

#include

#include

int

main

(

)

{

    std

::

array

<
int
,
3
arr1
=
{
1
,
2
,
3
}

;

    std

::

array

<
int
,
3
arr2
=
{
4
,
5
,
6
}

;

    arr1.

swap

(

arr2

)

;

    std

::

cout

<<

"Array 1: "

;

   

for

(

const

auto

&

elem

:

arr1

)

{

        std

::

cout

<<

elem

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

    std

::

cout

<<

"Array 2: "

;

   

for

(

const

auto

&

elem

:

arr2

)

{

        std

::

cout

<<

elem

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}


Comparison with other array types

Feature std::array C-style array std::vector
Size Fixed at compile time Fixed at compile time Dynamically variable
bounds checking at() Provides bounds checking None at() Provides bounds checking
Memory management Allocated on the stack Allocated on the stack Allocated on the heap
Performance High efficiency High efficiency Lower (dynamic allocation)
an interface Supports STL standard interface Does not support STL standard interface Supports STL standard interface

std::array is a very useful container in the C++ standard library; it provides a fixed-size array with the advantages of type safety and memory management. Through the examples above, we can see how to create, access, and modify std::array elements. For scenarios that need a fixed-size array,std::array is a very good choice.