C++数组库 tuple_element() 函数



描述

C++函数std::tuple_element(std::array)使用类似元组的接口提供编译时索引访问数组元素的类型。

声明

以下是来自std::array头文件的std::tuple_element(std::array)函数声明。

template< std::size_t I, class T, std::size_t N >
struct tuple_element<I, array<T, N> >;

参数

  • T − 获取元组大小的类型。

  • I − 元素的索引。

  • N − 数组的大小。

示例

以下示例演示了std::tuple_element(std::array)函数的使用。

#include <iostream>
#include <array>

using namespace std;

int main(void) {

   array <int, 5> arr = {1, 2, 3, 4, 5};

   /* iterator pointing at the start of the array */
   auto itr = arr.begin();

   /* traverse complete container */
   while (itr != arr.end()) {
      cout << *itr << " ";
      ++itr;   /* increment iterator */
   }

   cout << endl;

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果:

1 2 3 4 5
array.htm
广告
© . All rights reserved.