📜  C++ 中的无序映射向量与示例

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

C++ 中的无序映射向量与示例

向量

Vector :它与动态数组相同,能够在插入删除元素时自动调整自身大小,其存储由容器自动处理。向量元素被放置在连续存储中,以便可以使用迭代器访问和遍历它们。

与向量一起使用的一些函数:

  • 开始() 返回指向向量中第一个元素的迭代器
  • end() 它返回一个迭代器,指向向量中最后一个元素之后的理论元素
  • size() 它返回向量中的元素数。

无序地图:

Unordered_map是一个关联容器,它存储由键值和映射值组合形成的元素。键值用于唯一标识元素,映射的值是与键关联的内容。键和值都可以是预定义或用户定义的任何类型。无序映射中的元素没有按任何特定顺序排列。在内部,使用Hash Table实现无序映射。

与无序映射一起使用的一些函数:

  • at(): C++ unordered_map 中的此函数返回对以元素为键 k 的值的引用。
  • begin():返回一个迭代器,指向unordered_map容器中容器的第一个元素
  • end():返回一个迭代器,指向 unordered_map 容器中容器中最后一个元素之后的位置

本文重点介绍如何在 C++ 中使用无序映射向量。在设计复杂的数据结构时,无序映射向量非常有用。

下面是使用无序映射向量的实现:

示例 1:

C++
// C++ program to illustrate the working
// of vector of unordered maps
#include 
using namespace std;
  
// Function to print vector elements
void print(vector >& vect)
{
  
    cout << "vector : \n";
    for (int i = 0; i < (int)vect.size(); i++) {
  
        // Each element of the vector is a unordered map
        unordered_map unorderedMap = vect[i];
  
        cout << "unordered map : ";
        cout << "[  ";
  
        // Print unordered map elements
        for (auto it = unorderedMap.begin();
             it != unorderedMap.end(); it++) {
            cout << it->first << ':' << it->second << "  ";
        }
  
        cout << "]\n";
    }
}
  
// Driver Code
int main()
{
  
    // Declaring a vector of unordered maps
    vector > vect;
  
    // Declaring a unordered map
    unordered_map unorderedMap1;
  
    // Hashing values
    unorderedMap1[2] = 1;
    unorderedMap1[4] = 7;
    unorderedMap1[6] = 10;
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap1);
  
    // Declaring another unordered map
    unordered_map unorderedMap2;
  
    // Hashing values
    unorderedMap2[14] = 11;
    unorderedMap2[15] = 21;
    unorderedMap2[6] = 34;
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap2);
  
    // Declaring another unordered map
    unordered_map unorderedMap3;
  
    // Hashing values
    unorderedMap3[7] = 277;
    unorderedMap3[18] = 188;
    unorderedMap3[9] = 399;
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap3);
  
    // Declaring another unordered map
    unordered_map unorderedMap4;
  
    // Hashing values
    unorderedMap4[121] = 88;
    unorderedMap4[97] = 99;
    unorderedMap4[197] = 199;
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap4);
  
    print(vect);
    return 0;
}


C++
// C++ program to illustrate the working
// of vector of unordered maps
#include 
using namespace std;
  
// Function to print vector elements
void print(vector >& vect)
{
  
    cout << "vector : \n";
    for (int i = 0; i < (int)vect.size(); i++) {
        // Each element of the vector is a unordered map
        unordered_map unorderedMap = vect[i];
  
        cout << "unordered map : ";
        cout << "[  ";
  
        // Print unordered map elements
        for (auto it = unorderedMap.begin();
             it != unorderedMap.end(); it++) {
            cout << it->first << ':' << it->second << "  ";
        }
  
        cout << "]\n";
    }
}
  
// Driver Code
int main()
{
  
    // Declaring a vector of unordered maps
    vector > vect;
  
    // Declaring a unordered map
    unordered_map unorderedMap1;
  
    // Hashing values
    unorderedMap1[11] = "Geeks";
    unorderedMap1[23] = "for";
    unorderedMap1[32] = "Geeks";
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap1);
  
    // Declaring another unordered map
    unordered_map unorderedMap2;
  
    // Hashing values
    unorderedMap2[12] = "Python";
    unorderedMap2[32] = "Java";
    unorderedMap2[73] = "C++";
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap2);
  
    // Declaring another unordered map
    unordered_map unorderedMap3;
  
    // Hashing values
    unorderedMap3[11] = "PHP";
    unorderedMap3[2] = "C#";
    unorderedMap3[35] = "Assembly";
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap3);
  
    // Declaring another unordered map
    unordered_map unorderedMap4;
  
    // Hashing values
    unorderedMap4[14] = "C";
    unorderedMap4[27] = "Javascript";
    unorderedMap4[54] = "Swift";
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap4);
  
    print(vect);
    return 0;
}


C++
// C++ program to illustrate the working
// of vector of unordered maps
#include 
using namespace std;
  
// Function to print vector elements
void print(vector >& vect)
{
  
    cout << "vector : \n";
    for (int i = 0; i < (int)vect.size(); i++) {
        // Each element of the vector is a unordered map
        unordered_map unorderedMap = vect[i];
  
        cout << "unordered map : ";
        cout << "[  ";
  
        // Print unordered map elements
        for (auto it = unorderedMap.begin();
             it != unorderedMap.end(); it++) {
            cout << it->first << ':' << it->second << "  ";
        }
  
        cout << "]\n";
    }
}
  
// Driver Code
int main()
{
  
    // Declaring a vector of unordered maps
    vector > vect;
  
    // Declaring a unordered map
    unordered_map unorderedMap1;
  
    // Hashing values
    unorderedMap1[2] = 'G';
    unorderedMap1[7] = 'e';
    unorderedMap1[12] = 'e';
    unorderedMap1[14] = 'k';
    unorderedMap1[21] = 's';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap1);
  
    // Declaring another unordered map
    unordered_map unorderedMap2;
  
    // Hashing values
    unorderedMap2[13] = 'J';
    unorderedMap2[15] = 'a';
    unorderedMap2[24] = 'v';
    unorderedMap2[27] = 'a';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap2);
  
    // Declaring another unordered map
    unordered_map unorderedMap3;
  
    // Hashing values
    unorderedMap3[33] = 'P';
    unorderedMap3[37] = 'y';
    unorderedMap3[41] = 't';
    unorderedMap3[19] = 'h';
    unorderedMap3[43] = 'o';
    unorderedMap3[53] = 'o';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap3);
  
    // Declaring another unordered map
    unordered_map unorderedMap4;
  
    // Hashing values
    unorderedMap4[15] = 's';
    unorderedMap4[53] = 'w';
    unorderedMap4[16] = 'i';
    unorderedMap4[23] = 'f';
    unorderedMap4[27] = 't';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap4);
  
    print(vect);
    return 0;
}


输出
vector : 
unordered map : [  6:10  2:1  4:7  ]
unordered map : [  6:34  14:11  15:21  ]
unordered map : [  9:399  7:277  18:188  ]
unordered map : [  197:199  121:88  97:99  ]

示例 2:

C++

// C++ program to illustrate the working
// of vector of unordered maps
#include 
using namespace std;
  
// Function to print vector elements
void print(vector >& vect)
{
  
    cout << "vector : \n";
    for (int i = 0; i < (int)vect.size(); i++) {
        // Each element of the vector is a unordered map
        unordered_map unorderedMap = vect[i];
  
        cout << "unordered map : ";
        cout << "[  ";
  
        // Print unordered map elements
        for (auto it = unorderedMap.begin();
             it != unorderedMap.end(); it++) {
            cout << it->first << ':' << it->second << "  ";
        }
  
        cout << "]\n";
    }
}
  
// Driver Code
int main()
{
  
    // Declaring a vector of unordered maps
    vector > vect;
  
    // Declaring a unordered map
    unordered_map unorderedMap1;
  
    // Hashing values
    unorderedMap1[11] = "Geeks";
    unorderedMap1[23] = "for";
    unorderedMap1[32] = "Geeks";
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap1);
  
    // Declaring another unordered map
    unordered_map unorderedMap2;
  
    // Hashing values
    unorderedMap2[12] = "Python";
    unorderedMap2[32] = "Java";
    unorderedMap2[73] = "C++";
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap2);
  
    // Declaring another unordered map
    unordered_map unorderedMap3;
  
    // Hashing values
    unorderedMap3[11] = "PHP";
    unorderedMap3[2] = "C#";
    unorderedMap3[35] = "Assembly";
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap3);
  
    // Declaring another unordered map
    unordered_map unorderedMap4;
  
    // Hashing values
    unorderedMap4[14] = "C";
    unorderedMap4[27] = "Javascript";
    unorderedMap4[54] = "Swift";
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap4);
  
    print(vect);
    return 0;
}
输出
vector : 
unordered map : [  32:Geeks  11:Geeks  23:for  ]
unordered map : [  73:C++  12:Python  32:Java  ]
unordered map : [  35:Assembly  11:PHP  2:C#  ]
unordered map : [  54:Swift  14:C  27:Javascript  ]

示例 3:

C++

// C++ program to illustrate the working
// of vector of unordered maps
#include 
using namespace std;
  
// Function to print vector elements
void print(vector >& vect)
{
  
    cout << "vector : \n";
    for (int i = 0; i < (int)vect.size(); i++) {
        // Each element of the vector is a unordered map
        unordered_map unorderedMap = vect[i];
  
        cout << "unordered map : ";
        cout << "[  ";
  
        // Print unordered map elements
        for (auto it = unorderedMap.begin();
             it != unorderedMap.end(); it++) {
            cout << it->first << ':' << it->second << "  ";
        }
  
        cout << "]\n";
    }
}
  
// Driver Code
int main()
{
  
    // Declaring a vector of unordered maps
    vector > vect;
  
    // Declaring a unordered map
    unordered_map unorderedMap1;
  
    // Hashing values
    unorderedMap1[2] = 'G';
    unorderedMap1[7] = 'e';
    unorderedMap1[12] = 'e';
    unorderedMap1[14] = 'k';
    unorderedMap1[21] = 's';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap1);
  
    // Declaring another unordered map
    unordered_map unorderedMap2;
  
    // Hashing values
    unorderedMap2[13] = 'J';
    unorderedMap2[15] = 'a';
    unorderedMap2[24] = 'v';
    unorderedMap2[27] = 'a';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap2);
  
    // Declaring another unordered map
    unordered_map unorderedMap3;
  
    // Hashing values
    unorderedMap3[33] = 'P';
    unorderedMap3[37] = 'y';
    unorderedMap3[41] = 't';
    unorderedMap3[19] = 'h';
    unorderedMap3[43] = 'o';
    unorderedMap3[53] = 'o';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap3);
  
    // Declaring another unordered map
    unordered_map unorderedMap4;
  
    // Hashing values
    unorderedMap4[15] = 's';
    unorderedMap4[53] = 'w';
    unorderedMap4[16] = 'i';
    unorderedMap4[23] = 'f';
    unorderedMap4[27] = 't';
  
    // Push back the unordered map in the vector
    vect.push_back(unorderedMap4);
  
    print(vect);
    return 0;
}
输出
vector : 
unordered map : [  12:e  2:G  21:s  14:k  7:e  ]
unordered map : [  24:v  27:a  13:J  15:a  ]
unordered map : [  53:o  43:o  41:t  19:h  33:P  37:y  ]
unordered map : [  27:t  23:f  16:i  15:s  53:w  ]