C ++ STL中的数组data()及其示例
数组是存储在连续内存位置中的相同数据类型元素的集合。
C++标准库包含许多支持数组功能的库。其中之一是数组data()方法。
data()c++中的数组返回一个指向对象第一个元素的指针。
语法
array_name.data();
参数
该函数不接受任何参数。
返回类型
指向数组第一个元素的指针。
示例
程序来说明数组Data()方法的使用-
#include <bits/stdc++.h>
using namespace std;
int main(){
array<float, 4> percentage = { 45.2, 89.6, 99.1, 76.1 };
cout << "The array elements are: ";
for (auto it = percentage.begin(); it != percentage.end(); it++)
cout << *it << " ";
auto it = percentage.data();
cout << "\nThe first element is:" << *it;
return 0;
}输出结果
The array elements are: 45.2 89.6 99.1 76.1 The first element is:45.2
示例
演示如何使用数组Data()方法的程序
#include <bits/stdc++.h>
using namespace std;
int main(){
array<float, 4> percentage = { 45.2, 89.6, 99.1, 76.1 };
cout << "The array elements are: ";
for (auto it = percentage.begin(); it != percentage.end(); it++)
cout << *it << " ";
auto it = percentage.data();
it++;
cout << "\nThe second element is: " << *it;
it++;
cout << "\nThe third element is: " << *it;
return 0;
}输出结果
The array elements are: 45.2 89.6 99.1 76.1 The second element is: 89.6 The third element is: 99.1