C ++ STL中的数组get()函数?

在本节中,我们将看到get()C ++ STL中的数组功能。此函数用于获取数组容器的第ith个元素。语法如下-

语法

get<i> array_name

此功能有两个必填参数。是索引参数。它用于指向数组的第i个位置。第二个参数是array_name。这是第ith个元素的实际数组。此函数返回第ith个元素。

让我们看一个例子来了解这个想法。

示例

#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