tuple Class

包装元素固定长度的序列。

template<class T1, class T2, ..., class TN>
class tuple {
public:
    tuple();
    explicit tuple(P1, P2, ..., PN);              // 0 < N
    tuple(const tuple&);
    template <class U1, class U2, ..., class UN>
        tuple(const tuple<U1, U2, ..., UN>&);
    template <class U1, class U2>
        tuple(const pair<U1, U2>&);               // N == 2
    void swap(tuple& right);
    tuple& operator=(const tuple&);
    template <class U1, class U2, ..., class UN>
        tuple& operator=(const tuple<U1, U2, ..., UN>&);
    template <class U1, class U2>
        tuple& operator=(const pair<U1, U2>&);    // N == 2
    };

参数

  • TN
    第n个元组元素的类型。

备注

模板选件类描述了类型 T1,T2N个对象的对象,…,TN,分别,其中之 0 <= N ;AMP_lt;= Nmax。 元组实例 tuple<T1, T2, ..., TN;AMP_gt; 区域是其模板参数的数目 N。 模板参数 Ti 的索引和该类型所对应的存储的值是 i - 1。 因此,而我们从1计算类型到此文档的N,从0到N - 1.相应的索引值范围。

示例

// tuple.cpp
// compile with: /EHsc

#include <vector>
#include <iomanip>
#include <iostream>
#include <tuple>
#include <string>

using namespace std;

typedef tuple <int, double, string> ids;

void print_ids(const ids& i)
{
   cout << "( "
        << get<0>(i) << ", " 
        << get<1>(i) << ", " 
        << get<2>(i) << " )." << endl;
}

int main( )
{
   // Using the constructor to declare and initialize a tuple
   ids p1(10, 1.1e-2, "one");

   // Compare using the helper function to declare and initialize a tuple
   ids p2;
   p2 = make_tuple(10, 2.22e-1, "two");

   // Making a copy of a tuple
   ids p3(p1);

   cout.precision(3);
   cout << "The tuple p1 is: ( ";
   print_ids(p1);
   cout << "The tuple p2 is: ( ";
   print_ids(p2);
   cout << "The tuple p3 is: ( ";
   print_ids(p3);

   vector<ids> v;

   v.push_back(p1);
   v.push_back(p2);
   v.push_back(make_tuple(3, 3.3e-2, "three"));

   cout << "The tuples in the vector are" << endl;
   for(vector<ids>::const_iterator i = v.begin(); i != v.end(); ++i)
   {
      print_ids(*i);
   }
}
  
  
  
  
  
  

要求

标头: <tuple>

命名空间: std

请参见

参考

<tuple>

make_tuple Function