📜  C++中元组的多重映射与示例

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

C++中元组的多重映射与示例

什么是多图?

在 C++ 中,multimap 是一个关联容器,用于以映射方式存储元素。在内部,multimap 被实现为红黑树。多重映射的每个元素都被视为一对。第一个值称为键,第二个值称为值。 Multimap 与 map 非常相似,但在 multimap 的情况下,可以有多个相同的键。此外,不能使用方括号 ([]) 来访问键映射的值。与地图一样,多地图的键默认按升序排序。

与多图相关的功能:

  • begin() 返回一个迭代器,指向多重映射中的第一个元素。
  • end() 返回一个迭代器,指向多重映射中最后一个元素之后的理论元素。
  • size() 返回多图中的元素数。
  • max_size() 返回多图可以容纳的最大元素数。
  • empty() 返回多图是否为空。
  • insert(key, Value) 向多图添加一个新元素或对。
  • erase(iterator position) 删除迭代器指向的位置的元素。
  • erase(const x) 从多图中删除键值“x”。
  • clear() 从多图中删除所有元素。

什么是元组?

C++ 中的元组是用于将元素组合在一起的对象。在元组中,元素可以是相同的数据类型或不同的数据类型。元组的元素按照它们被访问的顺序进行初始化。

与元组关联的函数:

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

如何访问元组?

要访问元组的元素,请使用 get<>()函数。

句法:

本文重点介绍如何在 C++ 中创建元组的多重映射。

元组的多重映射

元组的多重映射是一个多重映射,其中键或值本身就是一个元组。如果对应的元组的第一个、第二个和第三个元素相等,则认为两个元组相等。现在,如果需要存储多个元组的副本以及其他元素也以排序或特定顺序存储,在这种情况下,元组的多映射会派上用场。

句法:

示例 1:下面是演示元组多映射工作的 C++ 程序。

C++
// C++ program to demonstrate
// the working of a multimap of
// tuples.
#include 
using namespace std;
  
// Function to print multimap elements
void print(multimap, 
           bool>& myContainer)
{
    cout << "Key(tuple of integers)" << 
            "            " << 
            "Value(boolean)\n\n";
  
    for (auto currentTuple : myContainer) 
    {
        // Each element of the multiset is 
        // a pair on its own Key is of type tuple
        tuple Key = currentTuple.first;
  
        // Value is a boolean value
        bool value = currentTuple.second;
  
        // Print key elements and value
        cout << '[' << get<0>(Key) << " , " << 
                get<1>(Key) << " , " << get<2>(Key) << 
                ']' << "                        " << 
                value << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a multimap
    // Key is of tuple type
    // Value is of bool type
    multimap, 
             bool> myContainer;
  
    // Creating some tuples to be used
    // as keys
    tuple tuple1;
    tuple1 = make_tuple(100, 200, 300);
  
    tuple tuple2;
    tuple2 = make_tuple(200, 300, 400);
  
    tuple tuple3;
    tuple3 = make_tuple(300, 400, 500);
  
    tuple tuple4;
    tuple4 = make_tuple(100, 200, 300);
  
    // Since each element is a tuple on its 
    // own in a multimap. So, we are inserting 
    // a tuple
    // Note that [] operator doesn't working 
    // in case of a multimap
    myContainer.insert(pair, 
                       bool>(tuple1, true));
    myContainer.insert(pair, 
                       bool>(tuple2, false));
    myContainer.insert(pair, 
                       bool>(tuple3, true));
    myContainer.insert(pair, 
                       bool>(tuple4, false));
  
    // Calling print function
    print(myContainer);
  
    return 0;
}


C++
// C++ program to demonstrate
// the working of a multimap of
// tuples.
#include 
using namespace std;
  
// Function to print multimap elements
void print(multimap, 
           bool>& myContainer)
{
    cout << "Key(tuple of integers)" << 
            "            " << 
            "Value(boolean)\n\n";
  
    for (auto currentTuple : myContainer) 
    {
        // Each element of the multiset is 
        // a tuple on its own
        // Key is of type tuple
        tuple Key = 
                           currentTuple.first;
  
        // Value is a boolean value
        bool value = currentTuple.second;
  
        // Print key elements and value
        cout << '[' << get<0>(Key) << " , " << 
                get<1>(Key) << " , " << get<2>(Key) << 
                ']' << "                        " << 
                value << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a multimap
    // Key is of tuple type
    // Value is of bool type
    multimap, 
                           bool> myContainer;
  
    // Creating some tuples to be used
    // as keys
    tuple tuple1;
    tuple1 = make_tuple("Java", 100, 1.123);
  
    tuple tuple2;
    tuple2 = make_tuple("Geek", 200, 2.123);
  
    tuple tuple3;
    tuple3 = make_tuple("HTML", 300, 3.123);
  
    tuple tuple4;
    tuple4 = make_tuple("Java", 100, 1.123);
  
    // Since each element is a tuple on its 
    // own in the multimap. So, we are 
    // inserting a tuple
    // Note that [] operator doesn't working 
    // in case of a multimap
    myContainer.insert(pair, 
                       bool>(tuple1, true));
    myContainer.insert(pair, 
                       bool>(tuple2, false));
    myContainer.insert(pair, 
                       bool>(tuple3, true));
    myContainer.insert(pair, 
                       bool>(tuple4, false));
  
    // Calling print function
    print(myContainer);
  
    return 0;
}


输出

示例 2:下面是演示元组多映射工作的 C++ 程序。

C++

// C++ program to demonstrate
// the working of a multimap of
// tuples.
#include 
using namespace std;
  
// Function to print multimap elements
void print(multimap, 
           bool>& myContainer)
{
    cout << "Key(tuple of integers)" << 
            "            " << 
            "Value(boolean)\n\n";
  
    for (auto currentTuple : myContainer) 
    {
        // Each element of the multiset is 
        // a tuple on its own
        // Key is of type tuple
        tuple Key = 
                           currentTuple.first;
  
        // Value is a boolean value
        bool value = currentTuple.second;
  
        // Print key elements and value
        cout << '[' << get<0>(Key) << " , " << 
                get<1>(Key) << " , " << get<2>(Key) << 
                ']' << "                        " << 
                value << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a multimap
    // Key is of tuple type
    // Value is of bool type
    multimap, 
                           bool> myContainer;
  
    // Creating some tuples to be used
    // as keys
    tuple tuple1;
    tuple1 = make_tuple("Java", 100, 1.123);
  
    tuple tuple2;
    tuple2 = make_tuple("Geek", 200, 2.123);
  
    tuple tuple3;
    tuple3 = make_tuple("HTML", 300, 3.123);
  
    tuple tuple4;
    tuple4 = make_tuple("Java", 100, 1.123);
  
    // Since each element is a tuple on its 
    // own in the multimap. So, we are 
    // inserting a tuple
    // Note that [] operator doesn't working 
    // in case of a multimap
    myContainer.insert(pair, 
                       bool>(tuple1, true));
    myContainer.insert(pair, 
                       bool>(tuple2, false));
    myContainer.insert(pair, 
                       bool>(tuple3, true));
    myContainer.insert(pair, 
                       bool>(tuple4, false));
  
    // Calling print function
    print(myContainer);
  
    return 0;
}
输出