带有示例的 C++ 中的多组元组
什么是元组?
C++ 中的元组是将一组元素绑定在一起的对象。元素可以是相似的,也可以是不同的数据类型。元组的元素按照它们被访问的顺序进行初始化。
句法:
tuple
Here,
dataType1, dataType2, dataType3 . . . . are similar or dissimilar data types
与元组关联的函数:
1 、make_tuple(): make_tuple()用于给元组赋值。传递的值应该与元组中声明的值顺序一致。
句法:
myTuple = make_tuple(value1, value2, value3,….)
2. get(): get() 用于访问元组值并对其进行修改,它接受索引和元组名称作为参数来访问特定的元组元素。
句法:
get
Here,
index is the index of the element we want to access in myTuple. Indexing in a tuple begins with zero.
什么是多集?
多重集是一种类似于集合的关联容器,但在多重集的情况下,所有元素都必须成对不同。简单来说,可以有多个元素具有相同的值。
句法:
multiset
Here,
dataType is a data type.
与多集相关的函数:
- begin():返回一个迭代器,指向多重集中的第一个元素。
- end() :返回一个迭代器,指向多重集中最后一个元素之后的理论元素。
- size() :返回多集中元素的数量。
- max_size() :返回多重集可以容纳的最大元素数。
- empty() :返回多重集是否为空。
当算法需要复杂的数据结构时,多组元组可能非常有用。本文重点介绍如何在 C++ 中创建多组元组。请注意,为简单起见,考虑了三个元素的元组,但元组也可以包含更多或更少的元素。
多组元组
元组的多重集是一个多重集,其中每个元素都是一个元组。
句法:
multiset
Here,
dataType1, dataType2, dataType3 are similar or dissimilar data types.
示例 1:下面是实现多组元组的 C++ 程序:
C++
// C++ program to illustrate the
// implementation of multiset of
// tuples
#include
using namespace std;
// Function to print multiset contents
void print(multiset >& multisetOfTuples)
{
// Iterating over multisetOfTuples elements
for (auto x : multisetOfTuples)
{
// Each element is a tuple itself
tuple tp = x;
// Printing tuple elements
cout << get<0>(tp) <<
' ' << get<1>(tp) <<
' ' << get<2>(tp) << '\n';
}
}
// Driver code
int main()
{
// Declaring a multiset of tuples
multiset > multisetOfTuples;
// Initializing tuples
tuple tuple1;
tuple1 = make_tuple(4, 2, 3);
tuple tuple2;
tuple2 = make_tuple(2, 3, 5);
tuple tuple3;
tuple3 = make_tuple(2, 3, 5);
tuple tuple4;
tuple4 = make_tuple(2, 1, 4);
tuple tuple5;
tuple5 = make_tuple(4, 2, 3);
// Inserting into multiset
multisetOfTuples.insert(tuple1);
multisetOfTuples.insert(tuple2);
multisetOfTuples.insert(tuple3);
multisetOfTuples.insert(tuple4);
multisetOfTuples.insert(tuple5);
// Calling print function
print(multisetOfTuples);
return 0;
}
C++
// C++ program to demonstrate the
// implementation of multiset 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
{
// Arranging multiset elements in
// non-ascending order
bool operator()(const tuple& x,
const tuple& y)
{
// If first elements of corresponding
// tuples are equal
if (get<0>(x) == get<0>(y))
{
// If second elements of corresponding
// tuples are equal
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 multiset elements
void print(multiset, cmp>& multisetOfTuples)
{
for (auto x : multisetOfTuples)
{
// Each element of the multiset is
// tuple itself
tuple tp = x;
// Printing tuple elements
cout << get<0>(tp) <<
' ' << get<1>(tp) <<
' ' << get<2>(tp) << '\n';
}
}
// Driver code
int main()
{
// Declaring a multiset of tuples
multiset, cmp> multisetOfTuples;
// 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, 5);
tuple tuple4;
tuple4 = make_tuple(2, 1, 4);
tuple tuple5;
tuple5 = make_tuple(5, 8, 4);
// Inserting into multiset
multisetOfTuples.insert(tuple1);
multisetOfTuples.insert(tuple2);
multisetOfTuples.insert(tuple3);
multisetOfTuples.insert(tuple4);
multisetOfTuples.insert(tuple5);
// Calling print function
print(multisetOfTuples);
return 0;
}
输出:
2 1 4
2 3 5
2 3 5
4 2 3
4 2 3
解释:
在上述输出中,多重集中共有五个元组,其中 (2, 3, 5) 和 (4, 2, 3) 出现两次。默认情况下,元组在多重集中以非降序排列,并遵循以下逻辑:
- 在多重集中,如果两个元组的第一个值相等,则比较元组的第二个值,如果也相等,则比较第三个值。
- 但是总是可以将比较器传递给集合。
示例 2:下面是 C++ 程序,用于演示将比较器传递给多集,该多集以非升序排列元组元素的多集。
C++
// C++ program to demonstrate the
// implementation of multiset 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
{
// Arranging multiset elements in
// non-ascending order
bool operator()(const tuple& x,
const tuple& y)
{
// If first elements of corresponding
// tuples are equal
if (get<0>(x) == get<0>(y))
{
// If second elements of corresponding
// tuples are equal
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 multiset elements
void print(multiset, cmp>& multisetOfTuples)
{
for (auto x : multisetOfTuples)
{
// Each element of the multiset is
// tuple itself
tuple tp = x;
// Printing tuple elements
cout << get<0>(tp) <<
' ' << get<1>(tp) <<
' ' << get<2>(tp) << '\n';
}
}
// Driver code
int main()
{
// Declaring a multiset of tuples
multiset, cmp> multisetOfTuples;
// 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, 5);
tuple tuple4;
tuple4 = make_tuple(2, 1, 4);
tuple tuple5;
tuple5 = make_tuple(5, 8, 4);
// Inserting into multiset
multisetOfTuples.insert(tuple1);
multisetOfTuples.insert(tuple2);
multisetOfTuples.insert(tuple3);
multisetOfTuples.insert(tuple4);
multisetOfTuples.insert(tuple5);
// Calling print function
print(multisetOfTuples);
return 0;
}
输出:
5 8 4
2 3 5
2 3 5
2 1 4
1 2 3
解释:
在上面的输出中,multiset 中的元素按照非升序排列。具有值 (2, 3, 5) 的元组在多重集中有两个副本。