當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Array data()用法及代碼示例

The array是存儲在連續內存位置的相同數據類型元素的集合。

C++ 標準庫包含許多支持數組函數的庫。其中之一是數組 data() 方法。

C++ 中的數組 data() 返回一個指向對象第一個元素的指針。

用法

array_name.data();

參數

該函數不接受任何參數。

返回類型

指向數組第一個元素的指針。

示例

Program To Illustrate The Use Of Array Data() Method −

#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

示例

Program To Illustrate The Use Of Array Data() Method

#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

相關用法


注:本文由純淨天空篩選整理自Sudhir sharma大神的英文原創作品 Array data() in C++ STL with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。