อาร์เรย์ คือชุดขององค์ประกอบประเภทข้อมูลเดียวกันที่เก็บไว้ในตำแหน่งหน่วยความจำต่อเนื่อง
ไลบรารีมาตรฐาน C++ ประกอบด้วยไลบรารีจำนวนมากที่สนับสนุนการทำงานของอาร์เรย์ หนึ่งในนั้นคือเมธอดอาร์เรย์ data()
ข้อมูลอาร์เรย์ () ใน c ++ จะส่งกลับตัวชี้ที่ชี้ไปที่องค์ประกอบแรกของวัตถุ
ไวยากรณ์
array_name.data();
พารามิเตอร์
ไม่มีพารามิเตอร์ที่ยอมรับโดยฟังก์ชัน
ประเภทการส่งคืน
ตัวชี้ไปยังองค์ประกอบแรกของอาร์เรย์
ตัวอย่าง
โปรแกรมเพื่อแสดงการใช้ Array 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
ตัวอย่าง
โปรแกรมเพื่อแสดงการใช้ Array 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