C++ data() function
data is a very special function in array; itreturns a pointer to the underlying array, allowing you to use array like a C array.
data is a member function of the container class, used toreturns a pointer to the container's internal data storage. This enables array to interoperate with C-style APIs or other code that needs raw pointers.
data provides a bridge between array and C code, reflecting array's compatibility.
Word meaning: data means 'data', i.e., to get a pointer to the underlying data.
Basic syntax and parameters
data is a member function of the container class; calling it requires no parameters.
Syntax format
T* data();
const T* data() const;
Parameter Description
- Parameters: No parameters
datatakes no arguments.
Function description
- Return Value: Returns a pointer to the underlying arraypointer. If the container is const, a const pointer is returned.
- effect: The returned pointer points to the first element of the container, equivalent to
&operator[](0). - Note: array is fixed-size and never reallocates memory, so
data()the returned pointer is always valid.
Example
Let us thoroughly master data through a series of examples from simple to complex.
Example 1: Basic usage - getting the underlying array pointer
Example
#include
#include
int
main
(
)
{
// 1. Create an array
std
::
array
<
int
,
5
numbers
=
{
10
,
20
,
30
,
40
,
50
};
std
::
cout
<<
"array size is: "
<<
numbers.
size
(
)
<<
std
::
endl
;
// 2. Use data to get the underlying array pointer
int
ptr
=
numbers.
data
(
)
;
std
::
cout
<<
"Access via pointer: "
<<
std
::
endl
;
for
(
size_t
i
=
0
;
i
<
numbers.
size
(
)
;
++
i
)
{
std
::
cout
<<
"ptr["
<<
i
<<
"] = "
<<
ptr
[
i
]
<<
std
::
endl
;
}
return
0
;
}
Expected output:
array的大小是: 5
Access via pointer:
ptr[0] = 10
ptr[1] = 20
ptr[2] = 30
ptr[3] = 40
ptr[4] = 50
Code analysis:
numbers.data()returns a pointer to the first element.- Through the returned pointer, you can use array subscript syntax to access all elements.
Example 2: Modifying elements via pointer
data The returned pointer can be used to modify elements.
Example
#include
#include
int
main
(
)
{
std
::
array
<
int
,
3
numbers
=
{
10
,
20
,
30
};
std
::
cout
<<
"Before modification: "
;
for
(
int
n
:
numbers
)
{
std
::
cout
<<
n
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Get a pointer via data() and modify the element
int
ptr
=
numbers.
data
(
)
;
ptr
[
0
]
=
100
;
ptr
[
1
]
=
200
;
ptr
[
2
]
=
300
;
std
::
cout
<<
"After modification: "
;
for
(
int
n
:
numbers
)
{
std
::
cout
<<
n
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
修改前: 10 20 30
修改后: 100 200 300
Code analysis:
- Pass
data()The returned pointer can directly modify array's internal data.
Example 3: Interacting with C-style APIs
data One of the main uses of is to interact with C functions that need an array pointer.
Example
#include
#include
// A hypothetical C-style function that computes the array sum
int
sumArray
(
const
int
arr,
size_t
size
)
{
int
sum
=
0
;
for
(
size_t
i
=
0
;
i
<
size
;
++
i
)
{
sum
=
arr
[
i
]
;
}
return
sum
;
}
int
main
(
)
{
std
::
array
<
int
,
5
numbers
=
{
1
,
2
,
3
,
4
,
5
};
// Use data() to pass the array to the C-style function
int
total
=
sumArray
(
numbers.
data
(
)
, numbers.
size
(
)
)
;
std
::
cout
<<
"Array elements: "
;
for
(
int
n
:
numbers
)
{
std
::
cout
<<
n
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
std
::
cout
<<
"Sum: "
<<
total
<<
std
::
endl
;
return
0
;
}
Expected output:
数组元素: 1 2 3 4 5
总和: 15
Code analysis:
numbers.data()passes array's internal array pointer to a function that acceptsconst int*.- This demonstrates array's ability to seamlessly interact with C code.
The C++ container class