在 C++ 中,映射(maps)是关联容器,用于存储数据对。
这些数据对被称为键值对,每个键都是唯一的,而关联的值不一定唯一。

映射中的元素是按照其键进行内部排序的。
为了在 C++ 中使用映射,我们必须在程序中包含 map
头文件
#include <map>
创建映射
我们可以使用以下语法声明一个映射
std::map<key_type, value_type> map_name = {{key1, value1},{key2, value2}, ...};
这里,
std::map
- 声明一个类型为map
的 STL 容器<key_type>
- 将存储在映射中的键的数据类型<value_type>
- 将存储在映射中的值的数据类型map_name
- 映射的唯一名称key1, key2, ...
- 要存储在映射中的键value1, value2, ...
- 要存储在映射中的值
让我们看一个例子,
// create a map with integer keys and string values
std::map<int, string> student = {{1,"Jacqueline"}, {2,"Blake"}, {3,"Denise"}};
注意:我们也可以不使用赋值运算符 =
来初始化映射元素。例如,
std::map<int, string> student {{1,"Jacqueline"}, {2,"Blake"}, {3,"Denise"}};
映射方法
在 C++ 中,map
类提供了各种方法来对映射执行不同的操作。
操作 | 描述 |
---|---|
insert() |
将一个元素(键值对)添加到映射中 |
erase() |
从映射中移除一个元素或一系列元素 |
clear() |
移除映射中的所有元素 |
find() |
在映射中搜索给定的键 |
size() |
返回映射中的元素数量 |
empty() |
如果映射为空,则返回 true |
在映射中添加值
我们可以使用 []
运算符将键值对添加到映射中。例如,
// create a map with integer keys and string values
std::map<int, string> student;
// add element with key 1 and value "Jacqueline"
student[1] = "Jacqueline";
// add element with key 2 and value "Blake"
student[2] = "Blake";
我们还可以使用 insert()
函数与 make_pair()
函数一起将元素插入映射中。例如,
student.insert(std::make_pair(3, "Denise"));
我们可以将上述两种方法推广为以下语法
// using the [] operator
map_name[key] = value;
// using the insert() and make_pair() functions
map_name.insert(std::make_pair(key, value));
示例 1:C++ 映射
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> student;
// use [] operator to add elements
student[1] = "Jacqueline";
student[2] = "Blake";
// use insert() method to add elements
student.insert(make_pair(3, "Denise"));
student.insert(make_pair(4, "Blake"));
// add elements with duplicate keys
student[5] = "Timothy";
student[5] = "Aaron";
for (int i = 1; i <= student.size(); ++i) {
cout << "Student[" << i << "]: " << student[i] << endl;
}
return 0;
}
输出
Student[1]: Jacqueline Student[2]: Blake Student[3]: Denise Student[4]: Blake Student[5]: Aaron
在此程序中,我们创建了一个名为 student 的映射,它存储 int
键和 string
值。
请注意,我们的程序中有两个重复的值("Blake"
),这是有效的。
但是,我们的程序中也有两个重复的键(5),这是无效的。
在这里,由于 "Timothy"
和 "Aaron"
共享相同的键,因此只允许存储其中一个值。
首先,"Timothy"
与 student 中的键 5 配对。然后,当 "Aaron"
与 5 配对时,"Timothy"
被覆盖。
注意:当我们直接使用 cout
打印映射元素时,我们只打印相应的映射值,而不打印其键。
// Output: Jacqueline
cout << student[1];
访问键和值
我们可以借助映射 迭代器来访问映射的键和值。例如,
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int, string> student;
student[1] = "Jacqueline";
student[2] = "Blake";
student[3] = "Denise";
student[4] = "Aaron";
// declare map iterator
map<int, string>::iterator iter;
// use iterator to display map elements
for (iter = student.begin(); iter != student.end(); ++iter) {
cout << iter->first << " - " << iter->second << endl;
}
return 0;
}
输出
1 - Jacqueline 2 - Blake 3 - Denise 4 - Aaron
在上面的示例中,我们使用自定义迭代器 iter 来访问 student 映射的键和值。键由 first
对象给出,值由 second
对象给出。
请注意,我们使用了带有 iter 迭代器的 for
循环来显示 student 的所有元素。
for (iter = student.begin(); iter != student.end(); ++iter) {
cout << iter->first << " - " << iter->second << endl;
}
这里,
begin()
- 返回一个指向映射第一个元素的迭代器end()
- 返回一个指向映射最后一个元素之后理论位置的迭代器
我们使用了 iter 的 ->
运算符来访问 first
和 second
对象。
或者,我们也可以解引用 iter 并使用 .
运算符来访问 first
和 second
对象。
// Output: 1
cout << (*iter).first << " - ";
// Output: Jacqueline
cout << (*iter).second;
注意:*
运算符用于解引用迭代器以直接访问映射元素。要了解更多信息,请访问 C++ 指针。
C++ 映射的 find() 函数
我们可以使用 find()
函数搜索映射中的键。其语法是
map_name.find(key);
例如,
map<int, string> student;
map<int, string>::iterator iter;
student[1] = "Jacqueline";
student[2] = "Blake";
// find the key 2 in the student map
// store the return value in iter
iter = student.find(2);
在上面的示例中,我们使用 find()
函数搜索 student 中包含键 2 的元素。
现在,find()
函数返回
- 如果元素存在,则返回指向该元素的迭代器
- 如果元素不存在,则返回指向映射末尾的迭代器,即
student.end()
。
注意:要了解更多信息,请访问 C++ 函数。
从 C++ 映射中删除元素
我们可以使用 erase()
和 clear()
函数来删除映射元素。
首先,我们来讨论 clear()
函数。
C++ 映射的 clear() 函数
clear()
函数删除映射的所有元素。例如,
map<int, string> student;
student[1] = "Jacqueline";
student[2] = "Blake";
cout << student.size(); // Output: 2
student.clear();
cout << student.size(); // Output: 0
C++ 映射的 erase() 函数
我们可以使用 erase()
函数来删除单个元素,方法是借助迭代器或键。
假设我们有一个名为 student_rank 的映射,如下所示
map <string, int> student_rank {{"Josh", 2}, {"Rachel", 4}, {"Betty", 6}};
现在,我们可以通过以下方式删除此映射的元素
1. 使用键删除
// delete element with key "Rachel" from the map
student_rank.erase("Rachel");
2. 使用迭代器删除
// initialize an iterator pointing to the beginning of the map
map <string, int>::iterator itr = student_rank.begin();
// increment the iterator to point to the second element
itr++;
// delete the second element
student_rank.erase(itr);
示例 2:使用 erase() 移除单个元素
#include <iostream>
#include <map>
using namespace std;
int main() {
// create a map named student
map <int, string> student {{1, "Denise"}, {2, "Blake"}, {3, "Courtney"}, {4, "John"}, {5, "Jennifer"}};
// create map iterator
map <int, string>::iterator itr;
// display initial map values
cout << "Initial Values:" << endl;
for(itr = student.begin(); itr != student.end(); ++itr) {
cout << itr->second << ", ";
}
cout << endl;
// use itr iterator to point to the first map element
itr = student.begin();
// remove the first element
student.erase(itr);
// remove the element having key 4
student.erase(4);
// display final map values
cout << "\nFinal Values:" << endl;
for(itr = student.begin(); itr != student.end(); ++itr) {
cout << itr->second << ", ";
}
return 0;
}
输出
Initial Values: Denise, Blake, Courtney, John, Jennifer, Final Values: Blake, Courtney, Jennifer,
在上面的程序中,我们使用 erase()
函数删除了单个映射元素。
首先,我们通过将 itr 迭代器传递给 erase()
来删除了第一个映射元素。
itr = student.begin();
student.erase(itr);
注意:我们还使用了 itr 迭代器来遍历映射。
然后,我们将键为 4 的映射元素通过将键传递给 erase()
来删除。
student.erase(4);

删除一系列元素
erase()
函数还可以用于从映射中移除一系列元素。为此,我们需要将两个迭代器传递给 erase()
函数
my_map.erase(itr_first, itr_last);
这里,
- itr_first - 要移除的第一个元素的迭代器
- itr_last - 要移除的最后一个元素之后的元素的迭代器
在这里,itr_first 是包含的,意味着它会被移除,而 itr_last 是不包含的,意味着它不会被移除。显然,它们之间的所有元素也会被移除。
请注意,映射中的元素是按照其键进行内部排序的。
示例 3:使用 erase() 移除一系列元素
#include <iostream>
#include <map>
using namespace std;
int main() {
// create a map named student
map <int, string> student {{1, "Denise"}, {2, "Blake"}, {3, "Courtney"}, {4, "John"}, {5, "Jennifer"}};
// create a map iterator
map <int, string>::iterator iter;
// display initial map values
cout << "Initial Values:" << endl;
for(iter = student.begin(); iter != student.end(); ++iter) {
cout << iter->second << ", ";
}
cout << endl;
// remove a range of elements
student.erase(student.find(2),student.find(5));
// display final map values
cout << "\nFinal Values:" << endl;
for(iter = student.begin(); iter != student.end(); ++iter) {
cout << iter->second << ", ";
}
return 0;
}
输出
Initial Values: Denise, Blake, Courtney, John, Jennifer, Final Values: Denise, Jennifer,
在上面的程序中,我们移除了键 2 和 5 之间的映射元素。
但是,请注意,键为 5 的元素没有被移除,而键为 2 的元素被移除了。
这是因为 erase()
会移除给定范围的起始元素,但不会移除结束元素。

另请阅读