std::vector::data() 是 C++ 中的 STL,它返回一个指向内存数组的直接指针,该内存数组由向量内部用于存储其拥有的元素。
vector_name.data()
参数:该函数不接受任何参数。
返回值:该函数返回一个指向数组中第一个元素的指针,该指针在向量内部使用。
// C++ program to demonstrate the
// vector::date() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialising vector
vector<int> vec = { 10, 20, 30, 40, 50 };
// memory pointer pointing to the
// first element
int* pos = vec.data();
// prints the vector
cout << "The vector elements are: ";
for (int i = 0; i < vec.size(); ++i)
cout << *pos++ << " ";
return 0;
}