帶示例的 C++ STL 中的 array data()


陣列是相同資料型別的元素的集合,這些元素儲存在連續的記憶體位置中。

C++ 標準庫包含許多支援陣列執行的庫。其中之一是 array data() 方法。

array 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

更新時間:2019 年 10 月 24 日

384 次瀏覽

開始你的職業生涯

完成該課程並獲得認證

開始學習
廣告
© . All rights reserved.