带有示例的 C++ 中的无序映射数组
什么是数组?
任何编程语言中的数组都是一种数据结构,用于将类似数据类型的元素或数据项存储在连续的内存位置,并且可以使用数组的索引随机访问元素。当我们想要存储大量具有相似数据类型的元素时,数组是有效的。
什么是无序地图?
Unordered_map 是一个关联容器,它存储由键值和映射值组合形成的元素。键值用于唯一标识元素,映射的值是与键关联的内容。键和值都可以是预定义或用户定义的任何类型。无序映射中的元素没有按任何特定顺序排列。在内部,无序映射是使用哈希表实现的。
与无序映射一起使用的函数:
- at() : C++ unordered_map 中的此函数返回对以元素为键 k 的值的引用。
- begin() :返回一个迭代器,指向 unordered_map 容器中容器的第一个元素
- end() :返回一个迭代器,指向 unordered_map 容器中容器中最后一个元素之后的位置
本文重点介绍如何在 C++ 中使用无序映射数组。在设计复杂的数据结构时,一组无序映射非常有用。
无序映射数组
C++ 允许我们创建一个无序映射数组。无序映射数组是一个数组,其中每个元素本身就是一个映射。
句法:
unordered_map<
Here,
N: The size of the array of unordered maps
dataType1: The dataType for the key
dataType2: The dataType for the value
示例 1:下面是实现该方法的 C++ 程序:
C++
// C++ program to demonstrate the
// working of array of unordered maps in C++
#include
using namespace std;
// Function to print unordered map elements
// specified at the index, "index"
void print(unordered_map& myMap,
int index)
{
cout << "The unordered map elements stored " <<
"at the index " << index << ": \n";
cout << "Key Value\n";
// Each element of the map is a pair on
// its own
for (auto pr : myMap)
{
// Each element of the map is a pair
// on its own
cout << pr.first << " " <<
pr.second << '\n';
}
cout << '\n';
}
// Function to iterate over all the array
void print(unordered_map* myContainer, int n)
{
// Iterating over myContainer elements
// Each element is a map on its own
for (int i = 0; i < n; i++)
{
print(myContainer[i], i);
}
}
// Driver code
int main()
{
// Declaring an array of unordered maps
// In unordered map Key is of type int
// Value is of type bool
unordered_map myContainer[3];
// Mapping values to the unordered map
// stored at the index 0
myContainer[0][10] = true;
myContainer[0][15] = false;
myContainer[0][20] = true;
myContainer[0][25] = false;
// Mapping values to the unordered map
// stored at the index 1
myContainer[1][30] = true;
myContainer[1][35] = false;
myContainer[1][40] = true;
myContainer[1][45] = false;
// Mapping values to the unordered map
// stored at the index 2
myContainer[2][50] = true;
myContainer[2][55] = false;
myContainer[2][60] = true;
myContainer[2][65] = false;
// Calling print function to iterate
// over myContainer elements
print(myContainer, 3);
return 0;
}
C++
// C++ program to demonstrate the
// working of array of maps in C++
#include
using namespace std;
// Function to print unordered map elements
// specified at the index, "index"
void print(unordered_map& myMap, int index)
{
cout << "The unordered map elements stored " <<
"at the index " << index << ": \n";
cout << "Key Value\n";
// Each element of the unordered map is
// a pair on its own
for (auto pr : myMap)
{
cout << pr.first << " " <<
pr.second << '\n';
}
cout << '\n';
}
// Function to iterate over the unordered
// map corresponding to an index
void print(unordered_map* myContainer, int n)
{
for (int i = 0; i < n; i++)
{
print(myContainer[i], i);
}
}
// Driver code
int main()
{
// Declaring an array of unordered maps
// In unordered map Key is of type string
// Value is of type bool
unordered_map myContainer[3];
// Mapping values to the unordered map
// stored at the index 0
myContainer[0]["Code"] = true;
myContainer[0]["HTML"] = false;
myContainer[0]["Java"] = true;
myContainer[0]["Solo"] = false;
// Mapping values to the unordered map
// stored at the index 1
myContainer[1]["PHP"] = true;
myContainer[1]["CSS"] = false;
myContainer[1]["C++"] = true;
myContainer[1]["Lab"] = false;
// Mapping values to the unordered map
// stored at the index 2
myContainer[2]["Swift"] = true;
myContainer[2]["Cobol"] = false;
myContainer[2]["Fizzy"] = true;
myContainer[2]["Pizza"] = false;
// Calling print function to print
// myContainer elements
print(myContainer, 3);
return 0;
}
The unordered map elements stored at the index 0:
Key Value
25 0
20 1
10 1
15 0
The unordered map elements stored at the index 1:
Key Value
45 0
40 1
30 1
35 0
The unordered map elements stored at the index 2:
Key Value
65 0
60 1
50 1
55 0
示例 2:下面是实现该方法的 C++ 程序:
C++
// C++ program to demonstrate the
// working of array of maps in C++
#include
using namespace std;
// Function to print unordered map elements
// specified at the index, "index"
void print(unordered_map& myMap, int index)
{
cout << "The unordered map elements stored " <<
"at the index " << index << ": \n";
cout << "Key Value\n";
// Each element of the unordered map is
// a pair on its own
for (auto pr : myMap)
{
cout << pr.first << " " <<
pr.second << '\n';
}
cout << '\n';
}
// Function to iterate over the unordered
// map corresponding to an index
void print(unordered_map* myContainer, int n)
{
for (int i = 0; i < n; i++)
{
print(myContainer[i], i);
}
}
// Driver code
int main()
{
// Declaring an array of unordered maps
// In unordered map Key is of type string
// Value is of type bool
unordered_map myContainer[3];
// Mapping values to the unordered map
// stored at the index 0
myContainer[0]["Code"] = true;
myContainer[0]["HTML"] = false;
myContainer[0]["Java"] = true;
myContainer[0]["Solo"] = false;
// Mapping values to the unordered map
// stored at the index 1
myContainer[1]["PHP"] = true;
myContainer[1]["CSS"] = false;
myContainer[1]["C++"] = true;
myContainer[1]["Lab"] = false;
// Mapping values to the unordered map
// stored at the index 2
myContainer[2]["Swift"] = true;
myContainer[2]["Cobol"] = false;
myContainer[2]["Fizzy"] = true;
myContainer[2]["Pizza"] = false;
// Calling print function to print
// myContainer elements
print(myContainer, 3);
return 0;
}
The unordered map elements stored at the index 0:
Key Value
Solo 0
Java 1
Code 1
HTML 0
The unordered map elements stored at the index 1:
Key Value
Lab 0
C++ 1
PHP 1
CSS 0
The unordered map elements stored at the index 2:
Key Value
Fizzy 1
Pizza 0
Swift 1
Cobol 0