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
热门推荐
10 女生高级生日祝福语简短
11 侄女婚礼语音祝福语简短
12 对司机的祝福语简短
13 对睡觉的祝福语简短
14 送礼祝福语男性生日简短
15 满月红包英文祝福语简短
16 客户入住新房祝福语简短
17 牛年佛家祝福语大全简短
18 祝福语商业文案范例简短