C/C++编程技巧

C++:map的使用方法

2020-02-19  本文已影响0人  AI秘籍

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

上一篇下一篇

猜你喜欢

热点阅读