带有示例的 C++ 中的映射数组
什么是数组?
任何编程语言中的数组都是一种数据结构,用于将类似数据类型的元素或数据项存储在连续的内存位置,并且可以使用数组的索引随机访问元素。当我们想要存储大量相似数据类型的元素时,数组是有效的。
什么是地图?
在 C++ 中,映射是一个关联容器,用于以映射方式存储元素。在内部,映射实现为自平衡二叉树。地图的每个元素都被视为一对。第一个值称为键,第二个值称为值。没有两个值可以具有相同的键。
与地图相关的功能:
- begin():返回映射中第一个元素的迭代器
- end():返回一个迭代器,指向映射中最后一个元素之后的理论元素
- size():返回地图中的元素个数
- max_size():返回map可以容纳的最大元素个数
- empty():返回地图是否为空
- insert(key, Value):向地图添加一个新元素
- erase(iterator position):删除迭代器指向的位置的元素
- erase(const x):从地图中删除键值“x”
- clear():从地图中删除所有元素
地图数组
C++ 允许我们创建地图数组。地图数组是一个数组,其中每个元素本身就是一个地图。
句法:
map<
Here,
N: The size of the array of 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 maps in C++
#include
using namespace std;
// Function to print map elements
// specified at the index, "index"
void print(map& myMap,
int index)
{
cout << "The 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(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 maps
// In map Key is of type int
// Value is of type bool
map myContainer[3];
// Mapping values to the 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 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 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 map elements specified
// at the index, "index"
void print(map& myMap,
int index)
{
cout << "The 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)
{
cout << pr.first << " " <<
pr.second << '\n';
}
cout << '\n';
}
// Function to iterate over the map
// corresponding to an index
void print(map* myContainer,
int n)
{
for (int i = 0; i < n; i++)
{
print(myContainer[i], i);
}
}
// Driver code
int main()
{
// Declaring an array of maps
// In map Key is of type string
// Value is of type bool
map myContainer[3];
// Mapping values to the 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 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 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 map elements stored at the index 0:
Key Value
10 1
15 0
20 1
25 0
The map elements stored at the index 1:
Key Value
30 1
35 0
40 1
45 0
The map elements stored at the index 2:
Key Value
50 1
55 0
60 1
65 0
示例 2:下面是实现地图数组的 C++ 程序。
C++
// C++ program to demonstrate the
// working of array of maps in C++
#include
using namespace std;
// Function to print map elements specified
// at the index, "index"
void print(map& myMap,
int index)
{
cout << "The 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)
{
cout << pr.first << " " <<
pr.second << '\n';
}
cout << '\n';
}
// Function to iterate over the map
// corresponding to an index
void print(map* myContainer,
int n)
{
for (int i = 0; i < n; i++)
{
print(myContainer[i], i);
}
}
// Driver code
int main()
{
// Declaring an array of maps
// In map Key is of type string
// Value is of type bool
map myContainer[3];
// Mapping values to the 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 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 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 map elements stored at the index 0:
Key Value
Code 1
HTML 0
Java 1
Solo 0
The map elements stored at the index 1:
Key Value
C++ 1
CSS 0
Lab 0
PHP 1
The map elements stored at the index 2:
Key Value
Cobol 0
Fizzy 1
Pizza 0
Swift 1