C++ STL中的矢量数据()函数

这个 std::vector::data() C++中的STL,它返回指向向量内部存储的内存数组的直接指针,以存储其拥有的元素。

null

语法:

vector_name.data()

参数: 该函数不接受任何参数。 返回值: 该函数返回一个指针,指向向量在内部使用的数组中的第一个元素。

下面的程序演示了上述功能:

CPP

// 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;
}


输出

The vector elements are: 10 20 30 40 50 

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享