📜  C++ 中的元组集和示例

📅  最后修改于: 2022-05-13 01:55:19.126000             🧑  作者: Mango

C++ 中的元组集和示例

什么是元组?
元组是一个可以容纳多个元素的对象。元素可以是不同的数据类型。元组的元素按照访问顺序初始化为参数。

元组操作:
1. get(): get() 用于访问元组值并对其进行修改,它接受索引和元组名称作为参数来访问特定的元组元素。
2. make_tuple(): make_tuple()用于给元组赋值。传递的值应该与元组中声明的值顺序一致。

什么是一套?
集合是一种关联容器,其中每个元素都必须是唯一的,因为元素的值标识了它。元素的值一旦添加到集合中就不能修改,尽管可以删除和添加该元素的修改值。

与 Set 相关的一些基本功能:  
1. begin() 返回指向集合中第一个元素的迭代器。
2. end() 返回一个迭代器,指向集合中最后一个元素之后的理论元素。
3. size() 返回集合中元素的数量。
4. max_size()返回集合可以容纳的最大元素数。
5. empty() 返回集合是否为空。

在实现复杂的数据结构时,一组元组可能非常有用。下面是实现上述方法的 C++ 程序——

C++
// C++ program to demonstrate the 
// implementation of set of
// tuples
#include 
using namespace std;
  
// Function to print set contents
void print(set >& setOfTuples)
{
  for (auto x : setOfTuples) 
  {
    tuple tp = x;
    cout << get<0>(tp) << 
            ' ' << get<1>(tp) << 
            ' ' << get<2>(tp) << '\n';
  }
}
  
// Driver code
int main()
{
  // Declaring a set of tuples
  set > setOfTuples;
  
  // Initializing tuples
  tuple tuple1;
  tuple1 = make_tuple(1, 2, 3);
  
  tuple tuple2;
  tuple2 = make_tuple(2, 3, 5);
  
  tuple tuple3;
  tuple3 = make_tuple(2, 3, 4);
  
  tuple tuple4;
  tuple4 = make_tuple(2, 1, 4);
  
  tuple tuple5;
  tuple5 = make_tuple(5, 8, 14);
  
  // Inserting into set
  setOfTuples.insert(tuple1);
  setOfTuples.insert(tuple2);
  setOfTuples.insert(tuple3);
  setOfTuples.insert(tuple4);
  setOfTuples.insert(tuple5);
  
  // Calling print function
  print(setOfTuples);
  
  return 0;
}


C++
// C++ program to demonstrate the 
// implementation of set of tuples 
// by using custom comparator
#include 
using namespace std;
  
// Comparator for arranging elements 
// in non-ascending order We can 
// always modify the comparator as 
// per the requirement
struct cmp 
{
  bool operator()(const tuple& x,
                  const tuple& y)
  {
  
    if (get<0>(x) == get<0>(y))
    {
      if (get<1>(x) == get<1>(y))
        return get<2>(x) > get<2>(y);
      return get<1>(x) > get<1>(y);
    }
  
    return get<0>(x) > get<0>(y);
  }
};
  
// Function to print set elements
void print(set, cmp>& setOfTuples)
{
  for (auto x : setOfTuples) 
  {
    tuple tp = x;
    cout << get<0>(tp) << 
            ' ' << get<1>(tp) << 
            ' ' << get<2>(tp) << '\n';
  }
}
  
// Driver code
int main()
{
  // Declaring a set of tuples
  set, cmp> setOfTuples;
  
  // Initializing tuples
  tuple tuple1;
  tuple1 = make_tuple(1, 2, 3);
  
  tuple tuple2;
  tuple2 = make_tuple(2, 3, 5);
  
  tuple tuple3;
  tuple3 = make_tuple(2, 3, 4);
  
  tuple tuple4;
  tuple4 = make_tuple(2, 1, 4);
  
  tuple tuple5;
  tuple5 = make_tuple(5, 8, 14);
  
  // Inserting into set
  setOfTuples.insert(tuple1);
  setOfTuples.insert(tuple2);
  setOfTuples.insert(tuple3);
  setOfTuples.insert(tuple4);
  setOfTuples.insert(tuple5);
  
  // Calling print function
  print(setOfTuples);
  
  return 0;
}


输出
1 2 3
2 1 4
2 3 4
2 3 5
5 8 14

默认情况下,元组在集合中以非降序排列,并遵循以下逻辑:
在集合中,如果两个元组的第一个值相等,则比较元组的第二个值,如果也相等,则比较第三个值。但是总是可以将比较器传递给集合。

下面是实现上述方法的 C++ 程序——

C++

// C++ program to demonstrate the 
// implementation of set of tuples 
// by using custom comparator
#include 
using namespace std;
  
// Comparator for arranging elements 
// in non-ascending order We can 
// always modify the comparator as 
// per the requirement
struct cmp 
{
  bool operator()(const tuple& x,
                  const tuple& y)
  {
  
    if (get<0>(x) == get<0>(y))
    {
      if (get<1>(x) == get<1>(y))
        return get<2>(x) > get<2>(y);
      return get<1>(x) > get<1>(y);
    }
  
    return get<0>(x) > get<0>(y);
  }
};
  
// Function to print set elements
void print(set, cmp>& setOfTuples)
{
  for (auto x : setOfTuples) 
  {
    tuple tp = x;
    cout << get<0>(tp) << 
            ' ' << get<1>(tp) << 
            ' ' << get<2>(tp) << '\n';
  }
}
  
// Driver code
int main()
{
  // Declaring a set of tuples
  set, cmp> setOfTuples;
  
  // Initializing tuples
  tuple tuple1;
  tuple1 = make_tuple(1, 2, 3);
  
  tuple tuple2;
  tuple2 = make_tuple(2, 3, 5);
  
  tuple tuple3;
  tuple3 = make_tuple(2, 3, 4);
  
  tuple tuple4;
  tuple4 = make_tuple(2, 1, 4);
  
  tuple tuple5;
  tuple5 = make_tuple(5, 8, 14);
  
  // Inserting into set
  setOfTuples.insert(tuple1);
  setOfTuples.insert(tuple2);
  setOfTuples.insert(tuple3);
  setOfTuples.insert(tuple4);
  setOfTuples.insert(tuple5);
  
  // Calling print function
  print(setOfTuples);
  
  return 0;
}
输出
5 8 14
2 3 5
2 3 4
2 1 4
1 2 3