C++ data() function
data is a very special function in vector; itreturns a pointer to the underlying arraylets you use the vector 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 allows the vector to interoperate with C-style APIs or other code that needs raw pointers.
data provides the bridge between vector and C code, reflecting vector's powerful 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: If the container is empty, the returned pointer is undefined (but usually not a null pointer).
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 a vector
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
};
std
::
cout
<<
"vector 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:
vector的大小是: 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
::
vector
<
int
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 the vector'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
#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
::
vector
<
int
numbers
=
{
1
,
2
,
3
,
4
,
5
};
// Use data() to pass the vector to a 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 the vector's internal array pointer to a function that acceptsconst int*.- this demonstrates the vector's ability to interact seamlessly with C code.
Example 4: Use memset to manipulate memory
can be used via data() to directly manipulate the vector's underlying memory.
Example
#include
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
10
,
20
,
30
,
40
,
50
};
std
::
cout
<<
"Before zeroing: "
;
for
(
int
n
:
numbers
)
{
std
::
cout
<<
n
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
// Use memset to set all bytes to 0
// Note: this is only valid for integer types; complex types may cause problems
std
::
memset
(
numbers.
data
(
)
,
0
, numbers.
size
(
)
sizeof
(
int
)
)
;
std
::
cout
<<
"After zeroing: "
;
for
(
int
n
:
numbers
)
{
std
::
cout
<<
n
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Expected output:
清零前: 10 20 30 40 50
清零后: 0 0 0 0 0
Code analysis:
data()allows direct manipulation of the vector's underlying memory.- Use
memsetcan quickly set all elements to zero. - Note: this method is not suitable for object types that contain constructors.
Example 5: Safe use of the returned pointer
when the vector is reallocated, the previously obtained pointer becomes invalid.
Example
#include
#include
int
main
(
)
{
std
::
vector
<
int
numbers
=
{
1
,
2
,
3
};
// Get the data pointer
int
ptr
=
numbers.
data
(
)
;
std
::
cout
<<
"Value pointed to by the original pointer: "
<<
ptr
<<
std
::
endl
;
// Adding elements may cause memory reallocation
numbers.
push_back
(
4
)
;
numbers.
push_back
(
5
)
;
numbers.
push_back
(
6
)
;
numbers.
push_back
(
7
)
;
numbers.
push_back
(
8
)
;
numbers.
push_back
(
9
)
;
numbers.
push_back
(
10
)
;
// Warning: ptr may already be invalid!
// In modern implementations, reallocation usually does not happen, but you should not rely on this
std
::
cout
<<
"After adding elements, the value pointed to by data(): "
<<
(
numbers.
data
(
)
)
<<
std
::
endl
;
// Safe practice: call data() to get a new pointer each time
ptr
=
numbers.
data
(
)
;
std
::
cout
<<
"After re-obtaining the pointer: "
<<
ptr
<<
std
::
endl
;
return
0
;
}
Code analysis:
- When the vector's capacity is insufficient,
push_backcauses memory reallocation. - After reallocation, the original
data()pointer returned by becomes invalid (becomes a dangling pointer). - Safe practice: call again each time it is used,
data()or usereserve()to pre-allocate enough space.
The C++ container class