STL mapmultimap容器用法

    科技2024-01-21  96

    一、什么是 map?

    map 容器,又称键值对容器,即该容器的底层是以红黑树变体实现的,是典型的关联式容器。这意味着,map 容器中的元素可以分散存储在内存空间里,而不是必须存储在一整块连续的内存空间中。跟任意其它类型容器一样,它能够存放各种类型的对象。

    二、容器特性

    1.存储结构

    map是由多个节点(二叉树中的红黑树变体)组成的。

    2.键值对

    map是一个键值对序列,即(key,value)——封装在结构体pair中,它提供基于key的快速检索能力,不能在指定位置插入,但支持at(pos)和[]操作。如:map[key]=value; multimap与map的区别:map中的key只能唯一,每个key只能出现一次;而multimap中同一key可以出现多次。

    3.双向迭代器

    不支持随机访问迭代器,只能从容器中第一个元素或最后一个元素开始遍历容器,直到找到该位置。

    三、基本函数实现

    1,构造函数

    map();创建一个空map map(const map&);复制构造函数 map(begin,end);复制[begin,end)区间内的元素,到另一个map中

    2.map插入

    在map中插入元素的**三种方式**: 假设: `map<int, string> mapStu;` 一、通过pair的方式插入对象 mapStu.insert( pair<int,string>(3,"小张") ); 二、通过value_type的方式插入对象 mapStu.insert( map<int,string>::value_type(1,"小李") ); 三、通过索引(key)的方式插入值 mapStu[4] = “小刘"; //使用key的方式插入,当key存在时会直接修改key对应的值,当key不存在时会新建一个然后插入 mapStu.at(3) = “小王";//此种键值插入方式要求键值必须存在,否则会报错,例如mapStu.at(5) = “小王"

    3.map删除

    iterator erase(iterator it);删除键值对中迭代器指向元素 map<int, string>::iterator it = mapStu.begin(); mapStu.erase(it); size_type erase(const key_type& key); 删除指定的元素 mapStu.erase(4); iterator erase(iterator first,iterator last);删除键值对中[first,last)中元素 it = mapStu.begin(); map<int, string>::iterator it_last = mapStu.end(); mapStu.erase(it,it_last); void clear();清空键值对中所有元素 mapStu.clear();

    4.查找函数

    iterator map.find(key); 查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end(); map<int, string>::iterator it = mapStu.begin(); it=mapStu.find(4);

    size_type map.count(keyElem); //返回容器中key为keyElem的对组个数。对map来说,要么是0,要么是1。对multimap来说,值可能大于1。

    int count= mapStu.count(4);

    5.判断函数

    bool empty() const;判断容器中是否有元素,若无元素,则返回 true;反之,返回 false。

    6.大小函数

    int size() const;返回键值对中元素的个数int max_size() const;返回最大可允许的map元素数量值

    四、基本用法,程序举例

    程序一:

    #include <iostream> #include<map> using namespace std; int main() { map<int, string> mapStu; mapStu.insert(pair<int, string>(3, "张3")); mapStu.insert(map<int, string>::value_type(1, "小李")); mapStu[4] = "小四"; mapStu.at(4) = "小王"; mapStu[5] = "小五"; map<int, string>::iterator it; it=mapStu.find(4); int count; count = mapStu.count(4); mapStu.erase(++it); mapStu.erase(4); it = mapStu.begin(); map<int, string>::iterator it_last = mapStu.end(); mapStu.erase(it,--it_last); mapStu.clear(); return 0; }

    程序二:

    #include <iostream> #include<map> using namespace std; int main() { //保存学生学号和姓名 map<int,string> s; for (int i = 0; i < 10; i++) { string ch = "ABCDEFGHIJ"; string name= "maye"; //注意pair的类型参数,需要和map的一致 s.insert(pair<int,string>(i,name+ch[i])); } s.insert(make_pair(111, "C语言PLUS")); s.insert(map<int, string>::value_type(222, "法外狂徒")); s[333] = "顽石"; cout << "学号:" <<" "<< "姓名:" << endl; for (map<int, string>::iterator it = s.begin(); it != s.end(); it++) { cout << it->first << " " << it->second << endl; } //cout << "\nmap size():" << s.size() << endl; //查找指定的key值,返回指向的迭代器,没有找到返回end()迭代器,所以再输出之前需要判断是否找到 map<int, string>::iterator it1 = s.find(6); if (it1 != s.end()) { cout << it1->first << " " << it1->second << endl; } //如果map中有等于4的key,则返回指向4的迭代器,如果没有返回第一个大于4的元素的迭代器,没有找到返回end()迭代器 it1 = s.lower_bound(4); if (it1 != s.end()) { cout << it1->first << " " << it1->second << endl; } //如果map中有大于4的key,返回第一个大于4的元素的迭代器,没有找到返回end()迭代器 it1 = s.upper_bound(4); if (it1 != s.end()) { cout << it1->first << " " << it1->second << endl; } cout << "------------------我是 C语言Plus 华丽分割线" << endl; //定义对组,接受equal_range()的返回值 pair<map<int, string>::iterator, map<int, string>::iterator> pa; //如果没有找到会返回end() pa = s.equal_range(7); if (it1 != s.end()) { cout << pa.first->first << " " << pa.first->second << endl; cout << pa.second->first << " " << pa.second->second << endl; } s.clear();//清空 cout << s.size() << endl; return 0; }

    文章参考或转自:https://mp.weixin.qq.com/s/gqeYUhjYorKei13t3Q4Hsg

    Processed: 0.014, SQL: 8