本文翻译自: C++ Map
在C++中,map是关联容器,存储数据对。这些数据对被认为是键值对,拥有唯一的键,但对应的值不必是唯一的:
下图是一个名为stduent的map:
map的元素在内部是通过键来排序的。
1.我们可以用如下的句法来声明一个map:
std::map<key_type, value_type> map_name = {{key1, value1},{key2, 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"}};
2.C++中的map类提供了多种方法用于执行不同操作:
3.往map中添加值:
我们可以使用操作符 [ ] 添加键值对,比如:
// 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( )函数在map中插入值。比如:
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));
4.案例1:
#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
5.访问键和值:
我们可以在map迭代器的帮助下访问map的键和值,比如:
#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 map中的键和值,first对象提供键,second对象提供值。
在遍历的for循环语句中:
for (iter = student.begin(); iter != student.end(); ++iter) {
cout << iter->first << " - " << iter->second << endl;
}
begin() —— 返回指向map的第一个元素的迭代器
end() —— 返回指向map的最后一个元素之后理论的元素的迭代器
对于first和second对象,我们还可以这样访问:
// Output: 1
cout << (*iter).first << " - ";
// Output: Jacqueline
cout << (*iter).second;
6.map的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);
返回结果:
(1) 如果元素存在,返回一个迭代器指向这个元素
(2) 如果元素不存在,返回一个迭代器指向map的末端,例如,student.end()
7.删除map中的元素
我们可以使用erase( )和clear( )来删除元素
(1) clear( )函数
该函数删除map中所有的元素,就像这样:
map<int, string> student;
student[1] = "Jacqueline";
student[2] = "Blake";
cout << student.size(); // Output: 2
student.clear();
cout << student.size(); // Output: 0
(2) map的erase( )函数
利用键或者迭代器,该函数能够删除map中的单个元素。对于这样一个map:
map <string, int> student_rank {{"Josh", 2}, {"Rachel", 4}, {"Betty", 6}};
使用键删除:
// delete element with key "Rachel" from the map
student_rank.erase("Rachel");
使用迭代器删除:
// 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);
8.删除一定范围内的元素
这个要用erase( )函数来完成,为此我们传入了两个迭代器:
my_map.erase(itr_first, itr_last);
itr_first —— 指向第一个被删除元素的迭代器
itr_last —— 指向被删除元素的下一个元素的迭代器
下面举个例子:
#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,
具体过程如图: