map:映射,存储键-值对,并按键升序排序。
1.头文件:
| #include <iostream> |
| #include <map> |
2.创建map:
map<string, int> myMap;
3.添加键值对:
insert(pair<Key, Value>(key, value)):向映射中插入键-值对
| |
| myMap.insert(make_pair("c1",v1) ); |
| myMap.insert(make_pair("c2", v2)); |
| myMap.insert(make_pair("c3", v3)); |
| |
| myMap.insert({"c4",v4}); |
4.查找键值对:
find(key)
:查找映射中是否存在特定键,返回迭代器。
count(key)
:统计特定键在映射中的出现次数(对于 std::map
,要么是0,要么是1)。
| |
| int times = myMap.count("c2"); |
| if (times == 1) { |
| cout << "存在班级c2" << endl; |
| } else { |
| cout << "不存在班级c2" << endl; |
| } |
| |
| |
| map<string, vector<string>>::iterator it; |
| it = myMap.find("c3"); |
| |
| if(it != myMap.end()) { |
| cout << "班级c2的学生: "; |
| for (string stu : it->second) { |
| cout << stu << " "; |
| } |
| } |
5.遍历键-值对:
| void printMap1(map<string, vector<string>>& myMap) { |
| map<string, vector<string>>::iterator it; |
| for (it = myMap.begin(); it != myMap.end();it++) { |
| cout << "班级: " << it->first << " 学生:"; |
| for (const string& stuName : it->second) |
| { |
| cout << stuName << ", "; |
| } |
| cout << endl; |
| } |
| } |
5.综合示例:班级学生
| |
| |
| |
| |
| |
| using namespace std; |
| |
| |
| void printMap(map<string, vector<string>>& myMap) { |
| for (const auto& pair : myMap) { |
| cout << "班级: " << pair.first << " 学生:"; |
| for (const auto& stuName : pair.second) |
| { |
| cout << stuName << ", "; |
| } |
| cout << endl; |
| |
| } |
| |
| } |
| |
| void printMap1(map<string, vector<string>>& myMap) { |
| map<string, vector<string>>::iterator it; |
| for (it = myMap.begin(); it != myMap.end();it++) { |
| cout << "班级: " << it->first << " 学生:"; |
| for (const string& stuName : it->second) |
| { |
| cout << stuName << ", "; |
| } |
| cout << endl; |
| |
| } |
| |
| } |
| |
| int main() { |
| |
| |
| |
| map<string, vector<string>> myMap; |
| |
| |
| vector<string> v1 = {"张三","李四","王五"}; |
| vector<string> v2 = {"李寻欢","二郎神","游所为"}; |
| vector<string> v3 = {"郭靖","杨过","黄蓉"}; |
| vector<string> v4 = {"jack","Tom","Lina"}; |
| |
| |
| |
| |
| myMap.insert(make_pair("c1",v1) ); |
| myMap.insert(make_pair("c2", v2)); |
| myMap.insert(make_pair("c3", v3)); |
| myMap.insert({"c4",v4}); |
| |
| |
| |
| printMap(myMap); |
| |
| |
| |
| myMap.erase("c4"); |
| |
| printMap1(myMap); |
| |
| |
| int times = myMap.count("c2"); |
| if (times == 1) { |
| cout << "存在班级c2" << endl; |
| } else { |
| cout << "不存在班级c2" << endl; |
| } |
| |
| |
| map<string, vector<string>>::iterator it; |
| it = myMap.find("c3"); |
| |
| if(it != myMap.end()) { |
| cout << "班级c2的学生: "; |
| for (string stu : it->second) { |
| cout << stu << " "; |
| } |
| } |
| |
| return 0; |
| } |
![STL之map有序哈希表使用方法 图片[1]-STL之map有序哈希表使用方法-不念博客](https://img.mryunwei.com/uploads/2023/12/20231214010232380.jpg)
综合示例2:统计单词频率
| |
| |
| |
| |
| using namespace std; |
| |
| |
| |
| |
| |
| |
| int main() { |
| string text = "this is a simple example of using std map this is example"; |
| map<string, int> wordFrequency; |
| |
| |
| |
| istringstream iss(text); |
| string word; |
| |
| while (iss >> word) { |
| |
| wordFrequency[word]++; |
| |
| |
| } |
| |
| |
| |
| for (const auto& pair : wordFrequency) { |
| cout << pair.first << ": " << pair.second << endl; |
| } |
| |
| return 0; |
| } |
![STL之map有序哈希表使用方法 图片[2]-STL之map有序哈希表使用方法-不念博客](https://img.mryunwei.com/uploads/2023/12/20231214090233457.jpg)