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.