Array get() 函式在 C++ STL 中的作用是什麼?


在本部分,我們來看看 C++ STL 中 array 的 get() 函式。該函式用於獲取陣列容器中的第 i 個元素。以下為其語法 −

語法

get<i> array_name

此函式採用兩個必需引數。第一個引數是索引引數。它用於指向陣列的第 i 個位置。第二個引數是 array_name。這是從中獲取第 i 個元素的實際陣列。此函式返回第 i 個元素。

我們來看一個示例來了解該函式的用途。

示例

 線上示例

#include<iostream>
#include<array>
using namespace std;
main() {
   array<int, 10> arr = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99};
   cout << "1st element: " << get<0>(arr) << endl;
   cout << "6th element: " << get<5>(arr) << endl;
   cout << "8th element: " << get<7>(arr) << endl;
   cout << "10th element: " << get<9>(arr) << endl;
}

輸出

1st element: 0
6th element: 55
8th element: 77
10th element: 99

更新於: 2019 年 7 月 30 日

261 次瀏覽

啟動您的 職業生涯

完成課程,拿到認證

立即開始
廣告
© . All rights reserved.