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
廣告