【STL】 map

【STL】从源码看map map 与set相同,map同样是以红黑树RB_Tree为底层机制的关联式容器。map的每一个元素都拥有两个值,一个键值(key)和一个实值(value)。它的内部实现是用一个pair来保存这个两个值。所以,map的每一个元素又是一个pair。下面是STL源码中stl_pair.h对pair的定义。 template struct pair { typedef T1 first_type 阅读详情

1、说明:系统根据C++ Reference学习下STL--> Map

2、Map:Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. 就是说一个key(关键)值映射一个mapped(映射)值,并且按照欧一定的顺序排列。

(1) key value标记一个一个元素,mapped value通常用来key value对应的内容

(2) Key value和Mapped Value的类型可以不同,一般由

typedef pair<const Key, T> value_type来绑定。

3、Map的基本成员函数

(1) map::at  --> Returns a reference to the mapped value of the element identified with key k

    map<string,int> mymap = {
        {"alpha", 0},
        {"beta", 0},
        {"gamma", 0}};


    mymap.at("alpha") = 10;
    mymap.at("beta") = 20;
    mymap.at("gamma") = 30;

    for(auto &x: mymap){
        cout << x.first << ": " << x.second << endl;
    }

(2)map::begin  --> Returns an iterator referring to the first element in the map container.

    map<char, int> mymap;
    map<char, int>::iterator it;

    mymap['b'] = 100;
    mymap['a'] = 200;
    mymap['c'] = 300;

    for(map<char, int>::iterator it = mymap.begin(); it != mymap.end(); it ++)
    {
        cout << it->first << " => " << it->second << endl;
    }

(3) map::cbegin  -->  Returns a  const_iterator  pointing to the first element in the container.

    map<char, int> mymap;

    mymap['b'] = 100;
    mymap['a'] = 200;
    mymap['c'] = 300;

    cout << "mymap contains:\n";
    for(auto it = mymap.cbegin(); it != mymap.cend(); it ++)
    {
        cout << "[" << (*it).first << ": " << (*it).second << "]\n";
    }

     (4)map::clear  --> Removes all elements from the map container (which are destroyed), leaving the container with a size of 0.

       (5)map::count  --> Searches the container for elements with a key equivalent to k and returns the number of matches.  Map中的所有element都是唯一的。

    map<char, int> mymap;
    char c;

    mymap['a'] = 101;
    mymap['b'] = 202;
    mymap['f'] = 303;

    for(c = 'a'; c < 'h'; c ++)
    {
        cout << c;
        if(mymap.count(c) > 0)
            cout << " is an element of mymap.\n";
        else cout << " is not an element of mymap.\n";
    }
      (6)map::crbegin  --> A const_reverse_iterator to the reverse beginning of the sequence.

和cbegin的调用是一致的。

     (7)map::emplace  --> Inserts a new element in the map if its key is unique. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pair type).  如果键值是唯一的,则插入成功。如果插入成功,则会依照原有的顺序插入到map中

    map<char, int> mymap;
    mymap.emplace('x', 100);
    mymap.emplace('y', 200);
    mymap.emplace('z', 300);

    cout << "mymap contains:\n";
    for(auto &x: mymap){
        cout << "[" << x.first << ", " << x.second << "]\n";
    }

         (8)map::emplace_hint   --> Inserts a new element in the map if its key is unique, with a hint on the insertion position. This new element is constructed in place using args as the arguments for the construction of a value_type (which is an object of a pairtype).

    map<char, int> mymap;
    auto it = mymap.end();

    it = mymap.emplace_hint(it, 'b', 10);
    mymap.emplace_hint(it, 'a', 12);
    mymap.emplace_hint(mymap.end(), 'c', 14);

    cout << "mymap contains: ";
    for(auto &x: mymap)
        cout << "[" << x.first << ", " << x.second << endl;
       (9)map::empty  -->  Returns whether the  map  container is empty (i.e. whether its  size  is  0 ).


       (10)map::equal_range  --> Returns the bounds of a range that includes all the elements in the container which have a key equivalent to k.

    map<char, int> mymap;
    mymap['a'] = 10;
    mymap['b'] = 20;
    mymap['c'] = 30;

    pair<map<char, int>::iterator, map<char, int>::iterator> ret;
    ret = mymap.equal_range('a');

    cout << "lower bound points to: ";
    cout << ret.first->first << " => " << ret.first->second << endl;
    cout << "upper bound points to: ";
    cout << ret.second->first << " => " << ret.second->second << endl;
       (11)map::erase  --> Removes from the map container either a single element or a range of elements ([first,last)).

    map<char, int> mymap;
    map<char, int>::iterator it;

    mymap['a'] = 10;
    mymap['b'] = 20;
    mymap['c'] = 30;
    mymap['d'] = 40;
    mymap['e'] = 50;
    mymap['f'] = 60;

    it = mymap.find('b');
    mymap.erase(it);        //erasing by iterator, erase b

    mymap.erase('c') ;       //erasing by key, erase c

    it = mymap.find('e');
    mymap.erase(it, mymap.end()) ;  //erasing by range, erase e, f

    //输出a d
    for(it = mymap.begin(); it != mymap.end(); ++ it)
        cout << it->first << " => " << it->second << endl;
     (12)map::find  --> Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end.

    std::map<char,int> mymap;
    std::map<char,int>::iterator it;

    mymap['a']=50;
    mymap['b']=100;
    mymap['c']=150;
    mymap['d']=200;

    it=mymap.find('b');
    mymap.erase (it);
    mymap.erase (mymap.find('d'));

    // print content:
    std::cout << "elements in mymap:" << '\n';
    std::cout << "a => " << mymap.find('a')->second << '\n';
    std::cout << "c => " << mymap.find('c')->second << '\n';

      (13)map::get_allocate  --> Returns a copy of the allocator object associated with the map.

    int psize;
    map<char, int> mymap;
    pair<const char, int> *p;

    //allocate an array of 5 elements using mymap's allocater.
    p = mymap.get_allocator().allocate(5);

    //assign some values to array
    psize = sizeof(map <char, int>::value_type) * 5;
    cout << "the allocated array has a size of " << psize << " bytes\n";
    mymap.get_allocator().deallocate(p, 5);

      (14)map::insert  --> Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.  插入时会检查是否已经存在和当前键值一样的元素。

    map<char, int> mymap;

    //first insert function version(single parameter)
    mymap.insert(pair<char, int>('a', 200));
    mymap.insert(pair<char, int>('z', 200));

    pair<map<char, int>::iterator, bool> ret;

    ret = mymap.insert(pair<char, int>('z', 500));
    if(ret.second == false){
        cout << "element '" << ret.first->first << "' already exists";
        cout << " with a value of " << ret.first->second << endl;
    }

    //second insert function version(with hint position)
    map<char, int>::iterator it = mymap.begin();
    mymap.insert(it, pair<char, int>('b', 300));
    mymap.insert(it, pair<char, int>('c', 400));

    //third insert function version(range insertion);
    map<char, int> anothermap;
    anothermap.insert(mymap.begin(), mymap.find('c'));

    //showing contents
    cout << "mymap contains:\n";
    for(it = mymap.begin(); it != mymap.end(); ++it)
        cout << it->first << " => " << it->second << endl;

    cout << "anothermap contains:\n";
    for(it = anothermap.begin(); it != anothermap.end(); ++ it)
        cout << it->first << " => " << it->second << endl;

    (15)map::key_comp  --> Returns a copy of the comparison object used by the container to compare keys.

    map<char, int> mymap;

    map<char, int>::key_compare mycomp = mymap.key_comp();
    mymap['b'] = 400;
    mymap['e'] = 500;
    mymap['f'] = 300;
    mymap['a'] = 100;

    cout << "mymap contains:\n";
    //key value of last element
    char highest = mymap.rbegin()->first;

    map<char, int>::iterator it = mymap.begin();
    do{
        cout << it->first << " => " << it->second << endl;
    }while(mycomp((*it ++).first, highest));

    cout << endl;


     (16)map::lower_bound  -> Returns an iterator pointing to the first element in the container whose key is not considered to go before k (i.e., either it is equivalent or goes after).  he mapcontains an element with a key equivalent to k: In this case, lower_bound returns an iterator pointing to that element, whereas upper_bound returns an iterator pointing to the next element.

    map<char, int> mymap;
    map<char, int>::iterator itlow, itup;

    mymap['a'] = 20;
    mymap['b'] = 40;
    mymap['c'] = 60;
    mymap['d'] = 80;
    mymap['e'] = 100;

    //itlow points to b
    itlow = mymap.lower_bound('a');
    //itup points to e(not 'd')
    itup = mymap.upper_bound('d');

    //erase [itlow, itup)
    mymap.erase(itlow, itup);

    for(map<char, int>::iterator it = mymap.begin(); it != mymap.end(); ++ it)
    {
        cout << it->first << " => " << it->second << endl;
    }
     (17)map::max_size  --> Returns the maximum number of elements that the map container can hold.
    map<int, int> mymap;

    cout << mymap.max_size() << endl;
    if(mymap.max_size() > 1000){
        for(int i = 0; i < 1000; i ++)
            mymap[i] = 0;
        cout << "The map contains 1000 elements" << endl;;
    }
    else cout << "The map could not hold 1000 elements." << endl;
   

   (18)map::operator=  --> Assigns new contents to the container, replacing its current content.

    map<char, int> first;
    map<char, int> second;

    first['x'] = 8;
    first['y'] = 16;
    first['z'] = 32;

    second = first;
    second.insert(pair<char, int>('a', 100));
    first = second;

    cout << "Size of first: " << first.size() << endl;
    cout << "Size of second: " << second.size() << endl;

    first = map<char, int>();
    cout << "Size of first: " << first.size() << endl;
    cout << "Size of second: " << second.size() << endl;


    (19)map::operator[]  -->  A reference to the mapped value of the element with a key value equivalent to  k .

    map<char, string> mymap;

    mymap['a'] = "an element";
    mymap['b'] = "another elment";
    mymap['c'] = mymap['b'];

    cout << "mymap['a'] is " << mymap['a'] << endl;
    cout << "mymap['b'] is " << mymap['b'] << endl;
    cout << "mymap['c'] is " << mymap['c'] << endl;

    cout << "mymap now contains " << mymap.size() << endl;

    (20)map::rbegin  -->  Returns a  reverse iterator  pointing to the last element in the container (i.e., its  reverse beginning ).

    map<char, int> mymap;

    mymap['x'] = 100;
    mymap['y'] = 200;
    mymap['z'] = 300;

    map<char, int>::reverse_iterator rit;
    for(rit=mymap.rbegin(); rit != mymap.rend(); ++rit){
        cout << rit->first << " => " << rit->second << endl;
    }


    (21)map::size  ->  Returns the number of elements in the  map  container.

  

    (22)map::value_comp  --> Returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second.


    (23)map::swap  --> Exchanges the content of the container by the content of x, which is another map of the same type. Sizes may differ.  需要是同种类型的

    map<char, int> foo, bar;

    foo['x'] = 100;
    foo['y'] = 200;

    bar['a'] = 11;
    bar['b'] = 22;
    bar['c'] = 33;

    foo.swap(bar);

    cout << "foo contains: \n";
    for(map<char, int>::iterator it = foo.begin(); it != foo.end(); ++it)
        cout << it->first << " => " << it->second << endl;

    cout << "bar contains: \n";
    for(map<char, int>::iterator it = bar.begin(); it != bar.end(); ++it)
        cout << it->first << " => " << it->second << endl;













STL map的介绍与使用 STL map的介绍与使用 1、map简介 map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。 对于迭代器来说,可以修改实值,而不能修改key。 2、map的功能 自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查... 阅读详情

相关推荐

Cisco Packet Tracer 7.3免登录技巧:3种方法快速跳过注册界面(附详细截图)

本文针对Cisco Packet Tracer 7.3启动时遇到的登录界面问题,提供了三种合法高效的解决方案。详细介绍了如何利用官方访客模式快速启动、注册思科账号实现一劳永逸登录,以及通过调整本地安装配置优化启动流程,帮助网络学习者与备考者绕过繁琐步骤,快速进入实验环境。

weixin_29305337的博客 763

C++STL之map详解

本文详细介绍map的各种用法和注意事项。

行码棋 7万+

具身智能落地实战:制造业、物流与电力的工业级方案设计

具身智能是一种融合感知、推理与物理执行的智能范式,其核心在于构建具备因果建模能力、多模态闭环反馈和实时动作规划的行动主体。区别于通用AI或传统自动化,它依托多模态大模型、高精度力觉/触觉传感及Sim2Real仿真技术,实现对真实物理环境的深度理解与自适应交互。技术价值体现在可解释性决策、毫秒级跨模态协同与零停机容错能力,已规模化应用于制造业质检、物流破损鉴定、电力绝缘子巡检等强约束工业场景。本文聚焦‘行业应用方案解决方案’这一高频搜索需求,系统拆解从工位筛选、分层架构设计到72小时现场交付的全链路工程方法论

weixin_30952479的博客 309

c++ STL中的map

1、map容器 map是C++ STL的一个关联容器,它提供一对一的数据处理能力。其中,各个键值对的键和值可以是任意数据类型,包括 C++ 基本数据类型(int、double 等)、使用结构体或类自定义的类型。 第一个可以称为关键字(key); 第二个可能称为该关键字的值(value); 该容器存储的都是 pair<const K, T> 类型(其中 K 和 T 分别表示键和值的数据类型)的键值对元素。 使用 map 容器存储的各个键值对,键的值既不能重复也不能被修改。换句话说,map 容

$好记性还是要多记录$ 2647

如何在高版本的ubuntu系统里安装gcc4.1.2

1、下载安装包,一共五个http://archive.ubuntu.com/ubuntu/pool/universe/g/gcc-4.1/,根据计算机系统位数   进行下载,64位下载amd64结尾的。开头分别是cpp-4.1_4.1.2、g++-4.1_4.1.2、gcc-4.1-base_4.1.2、gcc-     4.1_4.1.2、libstdc++6-4.1 2、使用sudo d

lsr_fighting的博客 1939

通用定时器脉冲计数实验(STM32CubeMx配置)

通用定时器脉冲计数实验

a1169511636的博客 5516

浅谈C++|STL之map篇

map不允许容器中有重复key值元素multimap允许容器中有重复key值元素。

m0_73731708的博客 1441

31、多项式的根:求解方法与技术详解

本博客详细探讨了多项式方程根的求解方法与技术,涵盖了从基础理论到高级数值逼近方法的内容。文章介绍了多项式根的存在性条件、解析解法以及常用的数值方法如牛顿法和二分法,并深入分析了割线法、布伦特法等高效数值逼近技术。此外,还讨论了优化策略、根的隔离与验证技术以及实际应用案例,为读者提供了全面的理论知识和实践指导。

w1x2y3的博客 408

C++stl map 超详细解说!!!

1.使用时要包含头文件 #include<map>,不要忘记了 2.格式:map<key, value> key:键值 value:实值 map中的元素是一些关键字-值(key-value)对 map容器通常被称为关联数组,但其下标不必为整数 key与value相当于绑定在一起,每一个不同的key对应于一个value,因此map容器中元素总是成对出现,而key起到索引的作用,可以凭借它很方便的找到value的值 所有元素都会根据元素的键值(key)自动排序 举...

qq_52313416的博客 1837

linux平台Modbus TCP测试

【代码】linux平台Modbus TCP测试。

无线互联&网络介绍 821

YOLOv8改进-利用图像去雾网络UnfogNet提升检测精度的探索【附核心模块代码】

YOLOv8-UnfogNet联合模型的提出为目标检测和图像去雾领域提供了一个新的思路。通过将图像去雾网络和目标检测网络结合,不仅可以提升在恶劣天气下的目标检测精度,还能够为实际应用中的自动驾驶、监控安防等领域带来更强的鲁棒性和适应性。随着去雾技术和目标检测算法的不断发展,我们相信,这种联合方法将在多个领域中获得更加广泛的应用,并为进一步提升图像质量和检测精度做出重要贡献。

一键难忘的博客 3206

STL之map

map的声明如下,Key就是map底层关键字的类型,T是map底层value的类型,map默认要求Key支持小于比较,如果不支持或者需要的话可以自行实现仿函数传给第二个模版参数,map底层存储数据的内存是从空间配置器申请的。一般情况下,我们都不需要传后两个模版参数。map底层是用红黑树实现,增删查改效率是O(logN) ,迭代器遍历是走的中序,所以是按key有序顺序遍历的。map底层的红黑树节点中的数据,使用pair<Key, T>存储键值对数据。T1 first;T2 second;

2501_92032664的博客 1351

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

STL—map

文章目录一、什么是map二、map的操作1.map的定义2.map内元素的访问(1)通过下标访问(2)通过迭代器访问3.map中的函数(1)find()(2)erase()删除单个元素删除一个区间内的所有元素(4)size()(4)clear() 一、什么是map map是映射,我们在定义数组的时候int a[100];其实是一个int --> int的映射,比如a[3] = 5的含义就是把3映射到5,一个double类型的数组就是一个int --> double的映射,这里我们就能知道我们.

佛系更新 2348

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【STL】map

在 C++ STL 标准库中,map 是一个关联容器,用于存储键值对。每个元素都有一个唯一的键,并且可以通过键快速检索对应的值。map 中的元素是按照键的特定顺序排列的(默认情况下是升序)。

jmlangel_的博客 1135

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

【STL】map函数

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有...

逆风笑的博客 9635

STL——map

摘要:map是STL中的关联容器,基于红黑树实现,提供O(logN)的增删查改效率,按键有序存储。其模板参数包括键类型、值类型、比较函数(默认less升序)和内存分配器。常用操作包括insert插入pair键值对、operator[]访问/修改值、find/count查找元素。map支持迭代器遍历,迭代器为双向迭代器。特性包括键唯一、自动排序,与multimap(允许重复键)、unordered_map(哈希实现)形成对比。核心接口包括插入、删除、查找和空间管理,operator[]兼具插入和修改功能。

LVZHUO_2022的博客 1271

STL中map用法详解

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有

IT老兵 12万+

STL容器之map类

STL容器之map类

Xpccccc的博客 1423

STL中 Map 的基本用法

一、大致了解 map是STL中的一个关联容器,和python中的数据类型字典一样,map 类型变量中的元素也是由键-值对组成,没有重复的键。其底层实现是红黑树(非严格意义上的平衡二叉树) 二、 基本用法 基本用法包括:声明一个map类型的变量、向声明的map变量中插入元素、查找map变量中的元素、删除map变量中的元素、清空map变量中的元素等。 2.1 声明一个map类型的变量 先加入头文件 #include<map> #include<map> ...

菜鸟学编程 7035

【STL】C++ STL之map详解

【C++】手把手教你看懂的 STL map 详解(超详细解析,小白一看就懂!!map是关联容器,它按照特定的次序(按照key来比较)存储 由键值 key 和值 value 组合而成的元素。在map中,键值 key 通常用于排序和唯一的标识元素,而 value 中存储的值与键值 key 产生关联。键值 key 和值 value的类型可能不同,并且在map的内部,key与value通过成员类型value_type绑定在一起,为其取别名称为pair::typedef pair value_type;

weixin_44595311的博客 972

STL中map介绍

本文主要介绍 C++ 编程语言的 STL(Standard Template Library) 中 map 的相关知识,同时通过示例代码介绍 map的使用方法。

web13618542420的博客 1553

STL:MAP容器

文章目录1.map简介2.map特性3.map基本函数1.头文件及其容器构造容器创建2. 容量计算函数3.插入函数4.查找函数5.删除与清空函数6.总结4. 1.map简介 作为关联式容器的一种,map 容器存储的都是 pair 对象,也就是用 pair 类模板创建的键值对。其中,各个键值对的键和值可以是任意数据类型,包括 C++ 基本数据类型(int、double 等)、使用结构体或类自定义的类型。 通常情况下,map 容器中存储的各个键值对都选用 string 字符串作为键的类型。 2.map..

xingzi201的博客 4482

STL MAP用法详解

说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂。本人水平有限,不当之处,望大家辅正。 一.Map概述 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下ma

nwf5d的专栏 1万+
上一篇: UVA 11991 Easy Problem from Rujia Liu?
下一篇: CF 407A Triangle
小do在努力
博客等级 码龄16年 58粉丝 161原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值