C++ 中的元组映射与示例
什么是元组?
C++ 中的元组是一个能够对多个元素进行分组的对象。元素可以是相同的类型,也可以是不同的数据类型。元组元素的初始化顺序可以按照相同的顺序访问。
与元组关联的函数:
1. make_tuple():用于给元组赋值。传递的值应该与元组中声明的值顺序一致。
2. get():用于访问元组值并对其进行修改,它接受索引和元组名称作为参数来访问特定的元组元素。
什么是地图?
C++ 中的映射是可以以映射方式存储元素的关联容器。地图的每个元素都有一个键和对应的映射值。没有两个映射值可以具有相同的键值。地图遵循以下语法,
与地图相关的功能:
- begin():返回映射中第一个元素的迭代器
- end():返回一个迭代器,指向映射中最后一个元素之后的假设元素
- size():返回地图中的元素个数
- max_size():返回map可以容纳的最大元素个数
- empty():返回地图是否为空
- clear():从地图中删除所有元素
元组地图
元组映射是其中键或值都是元组的映射。
句法:
map
Here,
dataType1, dataType2, dataType3 are the data types for the tuple which is a key here
dataType is the data type for value
本文重点介绍如何在 C++ 中创建元组映射。虽然也可以创建一个包含更多或更少元素的元组,但为了简单起见,在本文中,我们使用了只有三个元素的元组。
示例 1:下面是演示元组映射工作的 C++ 程序。
C++
// CPP program to demonstrate
// the working of a map of
// tuples.
#include
using namespace std;
// Function to print map elements
void print(map, int> &mapOfTuple)
{
cout << " Key(Tuple) " <<
"Value(Sum)\n\n";
for (auto pr : mapOfTuple)
// pr points to current pair of mapOfTuple
cout << "[" << get<0>(pr.first) << ", " <<
get<1>(pr.first) << ", " <<
get<2>(pr.first) << "] " <<
pr.second << "\n";
}
// Driver code
int main()
{
// Sending the hash function
// as a third argument
map,
int> mapOfTuple;
// Creating some tuples to be used
// as keys
tuple tuple1(100, 200, 300);
tuple tuple2(400, 500, 600);
tuple tuple3(700, 800, 900);
// Mapping sum of tuple elements as values
mapOfTuple[tuple1] = get<0>(tuple1) +
get<1>(tuple1) +
get<2>(tuple1);
mapOfTuple[tuple2] = get<0>(tuple2) +
get<1>(tuple2) +
get<2>(tuple2);
mapOfTuple[tuple3] = get<0>(tuple3) +
get<1>(tuple3) +
get<2>(tuple3);
// Calling print function
print(mapOfTuple);
return 0;
}
C++
// C++ program to demonstrate
// the working of a map of
// tuples.
#include
using namespace std;
// Function to print map elements
void print(map, string> &mapOfTuple)
{
cout << " Key(Tuple) " <<
"Value(Concatenation)\n\n";
// Iterating over map using range-based loop
for (auto pr : mapOfTuple)
// pr points to current pair of mapOfTuple
cout << "[" << get<0>(pr.first) <<
", " << get<1>(pr.first) <<
", " << get<2>(pr.first) <<
"] " << pr.second << "\n";
}
// Driver code
int main()
{
// Declaring a map whose key is a
// tuple of strings value is of
// also string type
map,
string> mapOfTuple;
// Creating some tuples of string types
// to be used as keys
tuple
tuple1("Geeks", "for", "Geeks");
tuple
tuple2("R", "HTML", "Javascript");
tuple
tuple3("Python", "Swift", "Java");
// Mapping concatenation of tuple elements as values
mapOfTuple[tuple1] = get<0>(tuple1) + " " +
get<1>(tuple1) + " " +
get<2>(tuple1);
mapOfTuple[tuple2] = get<0>(tuple2) + " " +
get<1>(tuple2) + " " +
get<2>(tuple2);
mapOfTuple[tuple3] = get<0>(tuple3) + " " +
get<1>(tuple3) + " " +
get<2>(tuple3);
// Calling print function
print(mapOfTuple);
return 0;
}
输出:
Key(Tuple) Value(Sum)
[100, 200, 300] 600
[400, 500, 600] 1500
[700, 800, 900] 2400
示例 2:下面是演示元组映射工作的 C++ 程序。
C++
// C++ program to demonstrate
// the working of a map of
// tuples.
#include
using namespace std;
// Function to print map elements
void print(map, string> &mapOfTuple)
{
cout << " Key(Tuple) " <<
"Value(Concatenation)\n\n";
// Iterating over map using range-based loop
for (auto pr : mapOfTuple)
// pr points to current pair of mapOfTuple
cout << "[" << get<0>(pr.first) <<
", " << get<1>(pr.first) <<
", " << get<2>(pr.first) <<
"] " << pr.second << "\n";
}
// Driver code
int main()
{
// Declaring a map whose key is a
// tuple of strings value is of
// also string type
map,
string> mapOfTuple;
// Creating some tuples of string types
// to be used as keys
tuple
tuple1("Geeks", "for", "Geeks");
tuple
tuple2("R", "HTML", "Javascript");
tuple
tuple3("Python", "Swift", "Java");
// Mapping concatenation of tuple elements as values
mapOfTuple[tuple1] = get<0>(tuple1) + " " +
get<1>(tuple1) + " " +
get<2>(tuple1);
mapOfTuple[tuple2] = get<0>(tuple2) + " " +
get<1>(tuple2) + " " +
get<2>(tuple2);
mapOfTuple[tuple3] = get<0>(tuple3) + " " +
get<1>(tuple3) + " " +
get<2>(tuple3);
// Calling print function
print(mapOfTuple);
return 0;
}
输出:
Key(Tuple) Value(Concatenation)
[Geeks, for, Geeks] Geeks for Geeks
[Python, Swift, Java] Python Swift Java
[R, HTML, Javascript] R HTML Javascript