美文网首页C/C++编程技巧
C++:map的使用方法

C++:map的使用方法

作者: AI秘籍 | 来源:发表于2020-02-19 11:40 被阅读0次

0.什么是map?

map是一类关联式容器,提供一对一的数据处理能力.
map第一个可以称为关键字key,每个关键字只能在map中出现一次,第二个称为该关键字的值value.
map内部自建一颗红黑树,这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的.

1.map的功能?

自动建立Key - value的映射;key和value可以是任意你需要的类型;
根据key值快速查找记录,查找的复杂度是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次;
快速插入Key -Value 记录;
快速删除记录
根据Key 修改value记录;
遍历所有记录;

2.map的使用方法?

头文件 #include <map>
map定义 std:map<int,string> personnel;
这样就定义了一个用int作为key,并拥有相关联的string类型的value.

3.map基本操作函数?

C++ maps是一种关联式容器,包含"键-值"对

 begin()         返回指向map头部的迭代器
 clear()         删除所有元素
 count()         返回指定元素出现的次数
 empty()         如果map为空则返回true
 end()           返回指向map末尾的迭代器
 equal_range()   返回特殊条目的迭代器对
 erase()         删除一个元素
 find()          查找一个元素
 get_allocator() 返回map的配置器
 insert()        插入元素
 key_comp()      返回比较元素key的函数
 lower_bound()   返回键值>=给定元素的第一个位置
 max_size()      返回可以容纳的最大元素个数
 rbegin()        返回一个指向map尾部的逆向迭代器
 rend()          返回一个指向map头部的逆向迭代器
 size()          返回map中元素的个数
 swap()          交换两个map
 upper_bound()   返回键值>给定元素的第一个位置
 value_comp()    回比较元素value的函数

4.map示例

/*****************************************
* map 演示
*******************************************/
 
#include <iostream>
 
#include <string>
#include <map>
#include <vector>
using namespace std;
 
map<string, string> map_name_major;
 
void map_show()
{
    map<string, string>::iterator iter;
    for (iter = map_name_major.begin(); iter != map_name_major.end(); iter++)
    {
        cout << "key: " << iter->first << "\tvalue: " << iter->second << endl;
    }
}
 
void map_insert()
{
    pair<map<string, string>::iterator, bool> insert_ret_val;
 
    // map_name_major.insert({"xiaoli", "computer"}); //-std=c++11
    insert_ret_val = map_name_major.insert(make_pair("xiaohua", "handwriting"));
    if (insert_ret_val.second == true) {
        cout << "insert successfully." << endl;
    } else {
        cout << "insert failed." << endl;
    }
 
    insert_ret_val = map_name_major.insert(pair<string, string> ("xiaohong", "math"));
    if (insert_ret_val.second == true) {
        cout << "insert successfully." << endl;
    } else {
        cout << "insert failed." << endl;
    }
 
    insert_ret_val = map_name_major.insert(map<string, string>::value_type("xiaolan", "electric"));
    if (insert_ret_val.second == true) {
        cout << "insert successfully." << endl;
    } else {
        cout << "insert failed." << endl;
    }
 
    map_name_major["xiaohuang"] = "computer";
}
 
void map_modify()
{
    string key = "xiaohong";
    string value = "computer";
 
    cout << "map modify begin" << endl;
    /* 数组方式赋值操作时,如果key已存在会直接覆盖 */
    cout << "key: "  << key << endl;
    cout << "value: " << map_name_major[key] << " -> ";
    map_name_major[key] = value;
    cout << map_name_major[key] << endl;
    cout << "map modify end" << endl;
}
 
void map_find(string key)
{
    map<string, string>::iterator iter;
    iter = map_name_major.find(key);
    if (iter != map_name_major.end())
    {
        cout << "Hit " << iter->first << ": " << iter->second << endl;
    }
    else
    {
        cout << key << ": Not in map" << endl;
    }
}
 
void map_remove(string key)
{
    cout << "will remove: " << key << endl;
    // case 1
    // map_name_major.erase(key);
 
    // case 2
    map<string, string>::iterator iter;
    iter = map_name_major.find(key);
    if (iter != map_name_major.end())
    {
        map_name_major.erase(iter);
    }
}
 
int main() {
    map_insert();
    map_show();
    map_modify();
    map_show();
    map_find("xiaolan");
    map_remove("xiaohong");
    map_show();
    return 0;
}

参考

1.https://blog.csdn.net/halazi100/article/details/88311597

相关文章

  • C++:map的使用方法

    0.什么是map? map是一类关联式容器,提供一对一的数据处理能力.map第一个可以称为关键字key,每个关键字...

  • go语言学习总结

    1、go语言的map和c++中的map有什么区别? go语言中的map是hash_table,和c++中uno...

  • map

    在其他语言诸如C++/Java中, map一般都以库的方式提供 如C++中的 std::map<> ,Java中的...

  • 实现特定场景下高性能的HashMap

    C++标准库的某些场景下的效率问题 在下面的场景中,C++标准库的unordered_map、map、multis...

  • C++ unordered_map

    hash_map ≈ unordered_map 最初的 C++ 标准库中没有类似 hash_map 的实现,但不...

  • map hash_map(挖坑)

    学习内容来自C++ STL中哈希表 hash_map 未学C++之哈希表的使用 map 使用count,返回的是被...

  • C++大厂面试真题

    C++标准库的map和set有什么区别,如何实现的? map和set都是C++的关联容器,其底层实现都是红黑树。 ...

  • 【JS】map遍历数组

    这里的map不是地图的意思,而是“映射”。 map的使用方法和forEach类似。 和forEach不同的是,ma...

  • C++ STL:unordered_map

    背景: hash_map 不是C++ 11 的标准 在vc中编译时: #includeusin...

  • map in C++

网友评论

    本文标题:C++:map的使用方法

    本文链接:https://www.haomeiwen.com/subject/wzaofhtx.html