当前位置: 首页 > wzjs >正文

门户手机版网站汉中seo培训

门户手机版网站,汉中seo培训,深一网站建设招聘,有哪些网站制作公司1. 定义和初始化 定义&#xff1a;std::map是一个关联容器&#xff0c;键值对会自动根据键的值进行排序&#xff08;默认是升序&#xff09;。 cpp复制 map<char, int> mp; 插入元素&#xff1a;可以通过operator[]或insert方法插入键值对。 cpp复制 mp[a] 1; mp[b] 3…

1. 定义和初始化

  • 定义std::map是一个关联容器,键值对会自动根据键的值进行排序(默认是升序)。

    cpp复制

    map<char, int> mp;
  • 插入元素:可以通过operator[]insert方法插入键值对。

    cpp复制

    mp['a'] = 1;
    mp['b'] = 3;
    mp['c'] = 2;

2. 遍历

  • 使用迭代器遍历std::map的迭代器可以用来遍历容器中的所有键值对。

    cpp复制

    map<char, int>::iterator it = mp.begin();
    map<char, int>::iterator itE = mp.end();
    while (it != itE) {cout << it->first << " ";  // 输出键cout << it->second << endl;  // 输出值it++;
    }

3. 查找

  • 使用find方法查找find方法会返回一个迭代器,指向找到的键值对。如果键不存在,则返回end()

    cpp复制

    map<char, int>::iterator itFind = mp.find('b');
    if (itFind != mp.end()) {cout << itFind->first << " " << itFind->second << endl;
    }

4. 删除

  • 删除指定迭代器的元素:可以通过erase方法删除指定迭代器指向的元素。

    cpp复制

    map<char, int>::iterator itB = mp.find('b');
    mp.erase(itB);
  • 删除指定键的元素:也可以通过键直接删除元素。

    cpp复制

    mp.erase('b');
  • 删除范围内的元素erase方法还可以删除指定范围内的元素。

    cpp复制

    mp.erase(it, itE);

5. 获取大小

  • 使用size方法size方法返回容器中元素的数量。

    cpp复制

    cout << "The size of map is: " << mp.size() << endl;

6. 清空

  • 使用clear方法clear方法会删除容器中的所有元素,但不会释放内存。

    cpp复制

    mp.clear();
    cout << "The size of map after cleaning is: " << mp.size() << endl;

7.插入

      使用insert方法insert插入键值对进入map容器。

8.总结

std::map是一个非常强大的关联容器,适用于需要快速查找、插入和删除键值对的场景。它的主要特点包括:

  • 自动排序:键值对会根据键自动排序。

  • 高效查找:查找操作的时间复杂度为O(logn)。

  • 灵活操作:支持插入、删除、查找等多种操作

Coding

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

int main() {
    map<char, int> mp;
    mp['b'] = 3;
    mp['a'] = 1;
    mp['c'] = 2;
    //map will automatic sort by key value
    map<char,int>::iterator it = mp.begin();
    map<char,int>::iterator itE = mp.end();

    while (it != itE) {
        cout << it->first << " ";  
        cout << it->second << endl;
        it++;
    }
    
    //The map can use so-called find function
    map<char,int>::iterator itFind = mp.find('b');
    cout <<endl << itFind->first << " " << itFind->second << endl;
    
    
    //map can erase the aimed element or a contiuous part of elements
    //erase iterator b
    map<char,int>::iterator itB = mp.find('b');
    mp.erase(itB);
    
    //erase function can erase a contious elements
    //mp.erase(it,itE);
    
    //erase function can remove the elemnt by relative key
    //mp.erase('b');
    
    //size function can get the size of map
    cout << "The size of map is :" << " " << mp.size() << endl;
    
    //clear function can remove all of the map
    mp.clear();
    cout << "The size of map after cleaning is :" << mp.size() << endl;
    
    return 0;
}

http://www.dtcms.com/wzjs/498086.html

相关文章:

  • 物业网站建设方案网络优化推广公司哪家好
  • 网站建设php的心得和体会百度关键词搜索热度查询
  • 国内html5视频网站建设正规代运营公司排名
  • 51简历模板网学校seo推广培训班
  • 现在去石家庄会被隔离吗东莞网站推广及优化
  • 网站建设服务 百度常用的网络推广方式有哪些
  • 网站装修的代码怎么做的济宁网站建设
  • 专门做简历的网站百度商品推广平台
  • 陈坤做直播在哪个网站网站怎么申请怎么注册
  • 自己做网站用什么数据库2023最火的十大新闻
  • 石家庄商城网站制作我想在百度上做广告怎么做
  • 临沂教育平台网站建设电商平台运营方案思路
  • 深圳市移动端网站建设免费seo推广公司
  • 企业网站建设管理及推广查企业信息查询平台
  • 文字设计四川网站seo
  • 南昌企业建站网站推广的常用方法有哪些?
  • 免费自己建立网站网络营销外包推广定制公司
  • 深圳设计公司深圳市广告公司seo怎么做新手入门
  • 网站模板插件十大互联网平台
  • 毕设什么类型网站容易做长春网站搭建
  • java 制作网站开发西安网络推广
  • 成都做一个小企业网站需要多少钱百度怎样免费发布信息
  • 正规品牌网站设计优化排名推广技术网站
  • 快手秒刷自助网站网站设计案例
  • 内乡微网站建设seo快速排名的方法
  • 泰州市建设工程招标网seo网址大全
  • 东莞清溪网站制作经典软文广告
  • 用PS做的个人网站图片优化网站内容
  • 网站后台信息管理怎么做微信朋友圈广告30元 1000次
  • 临沂龙文网站建设企业网站的优化建议