STL之map有序哈希表使用方法

map:映射,存储键-值对,并按键升序排序。

1.头文件:

#include <iostream>
#include <map>

2.创建map:

map<string, int> myMap; // 创建一个从字符串到整数的映射

3.添加键值对:

insert(pair<Key, Value>(key, value)):向映射中插入键-值对

//使用make_pair将参数变成键值对类型
 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)。
// 5.查找键值对
    int times = myMap.count("c2");
    if (times == 1) {
        cout << "存在班级c2" << endl;
    } else {
        cout << "不存在班级c2" << endl;
    }

    //找出班级c3的所有学生
    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.综合示例:班级学生

#include <iostream>
#include <set>
#include <string>
#include <map>
#include <vector>
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存储班级-学生信息,班级是key,学生信息是value,一个字符串数组
    // 1.创建map
    map<string, vector<string>>  myMap;

    // 2.准备数据
    vector<string> v1 = {"张三","李四","王五"};
    vector<string> v2 = {"李寻欢","二郎神","游所为"};
    vector<string> v3 = {"郭靖","杨过","黄蓉"};
    vector<string> v4 = {"jack","Tom","Lina"};


    // 3.给map添加元素的两种办法: 
    //在 map 中,键值对的插入语法应该使用 make_pair 或者花括号 {}
    myMap.insert(make_pair("c1",v1) );
    myMap.insert(make_pair("c2", v2));
    myMap.insert(make_pair("c3", v3));
    myMap.insert({"c4",v4});


    //输出每个班级的人
    printMap(myMap);


    // 4.删除指定键值对
    myMap.erase("c4");

    printMap1(myMap);

    // 5.查找键值对
    int times = myMap.count("c2");
    if (times == 1) {
        cout << "存在班级c2" << endl;
    } else {
        cout << "不存在班级c2" << endl;
    }

    //找出班级c3的所有学生
    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;
}
图片[1]-STL之map有序哈希表使用方法-不念博客

综合示例2:统计单词频率

#include <iostream>
#include <map>
#include <string>
#include <sstream>
using namespace std;

/*
@description: 本文件介绍有序map容器用法02
@author zhengshihong
*/

int main() {
    string text = "this is a simple example of using std map this is example";
    map<string, int> wordFrequency;

    // 将文本拆分为单词并统计频率
    // istringstream 是 C++ 中的输入流,它可以从字符串中读取数据,就像 cin 从标准输入中读取数据一样。
    istringstream iss(text);//创建了一个 istringstream 对象 iss,并用给定的文本 text 来初始化它
    string word;//定义了一个字符串变量 word,用于存储从 iss 读取的每个单词。
    //它不断从 iss 中读取单词,并将每个单词存储在 word 变量中
    while (iss >> word) {
        // 将单词插入映射,如果已存在则增加频率
        wordFrequency[word]++;
        //使用 word 作为键,将单词插入到名为 wordFrequency 的映射(map 或 unordered_map)中。
        //如果已存在key等于单词,则频率加1,不存在则增加等于单词的key,并初始化频率为1
    }


    // 输出每个单词的频率
    for (const auto& pair : wordFrequency) {
        cout << pair.first << ": " << pair.second << endl;
    }

    return 0;
}
图片[2]-STL之map有序哈希表使用方法-不念博客
© 版权声明
THE END
喜欢就支持一下吧
点赞94赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称

    暂无评论内容