C++ <array> data function
datais a very special function in array, itReturns a pointer to the underlying array., allowing you to use array just like a C array.
dataIs 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 requires raw pointers.
dataprovides a bridge between array and C language code, reflecting array's compatibility.
Word Definitions: datameans "data", indicating obtaining the pointer to the underlying data.
Basic Syntax and Parameters
datais a member function of the container class, and calling it requires no parameters.
Syntax format
T* data(); const T* data() const;
Parameter description
- Parameter: No parameters
dataIt does not accept any parameters.
Function description
- Return Value: Returns a pointer to the underlying array.Pointer. If the container is a const container, 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 in size and never reallocates memory, therefore
data()The returned pointer is always valid.
Example
let us thoroughly master it through a series of examples.dataUsage.
Example 1: Basic usage - obtaining the underlying array pointer
Example
#include <array>
int main() {
// 1. Create an array
std::array<int, 5> numbers = {10, 20, 30, 40, 50};
std::cout << "The size of array is: " << numbers.size() << std::endl;
// 2. Use data to get the underlying array pointer
int* ptr = numbers.data();
std::cout << "Access through 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 通过指针访问: 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, all elements can be accessed using array subscript syntax.
Example 2: Modifying elements via pointer
dataThe returned pointer can be used to modify elements.
Example
#include <array>
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 the pointer through data() and modify elements
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:
- Through
data()The returned pointer can directly modify array's internal data.
Example 3: Interacting with C-style APIs
dataone of its main uses is to interact with C functions that require array pointers.
Example
#include <array>
// A hypothetical C-style function that computes the sum of an array
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 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()Pass the array's internal array pointer to functions that acceptconst int*of the function.- This demonstrates array's ability to interact seamlessly with C code.
other extensions