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

建设银行网站登录首页阿里云云服务平台

建设银行网站登录首页,阿里云云服务平台,常平网站公司,现在最新技术有哪些map和set 文章目录 map和set关联式容器setset介绍set的函数测试代码 multiset注意事项测试代码 mapmap介绍map的函数测试代码 关联式容器 前面了解过的vector,list,string等容器都是序列式容器,存储的都是元素本身,底层都是线性的…

map和set

文章目录

  • map和set
    • 关联式容器
    • set
      • set介绍
      • set的函数测试代码
    • multiset
      • 注意事项
      • 测试代码
    • map
      • map介绍
      • map的函数测试代码

关联式容器

前面了解过的vector,list,string等容器都是序列式容器,存储的都是元素本身,底层都是线性的数据结构。

map和set存储的都是<key,value> 的键值对,在进行数据检索时效率更高

STL中对键值对的定义:

template <class T1, class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair(): first(T1()), second(T2())
{}
pair(const T1& a, const T2& b): first(a), second(b)
{}
};

set

文档:https://legacy.cplusplus.com/reference/set/set/?kw=set

set介绍

  • 查找:set的底层是红黑树,存储键值对,中序遍历结果是有序的,默认从小到大排序,查找的时间复杂度是 l o g 2 n log_2n log2n
  • 去重:除了排序,set还有去重的功能。
  • 底层1:set存储的实际上是**<value,value>结构**,map才是真正的<key,value>
  • 底层2:**set的key值不能修改,因为迭代器底层都是const迭代器。**但是可以插入和删除。

在这里插入图片描述
在这里插入图片描述

set的函数测试代码

void test_set1()
{// 排序+去重set<int> s;s.insert(3);s.insert(3); s.insert(3);s.insert(5);s.insert(8);s.insert(7);for (auto e : s){cout << e << " ";}cout << endl;//3 5 7 8if (s.find(5) != s.end()){cout << "找到了" << endl;}if (s.count(5)){cout << "找到了" << endl;}std::set<int> myset;std::set<int>::iterator itlow, itup;for (int i = 1; i < 10; i++)myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90myset.insert(35);// 删除[30 60]//itlow = myset.lower_bound(30);   // >=     //itup = myset.upper_bound(60);    // >//std::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> ret;auto ret = myset.equal_range(30);//ret的类型是pairitlow = ret.first;itup = ret.second;// [itlow, itup)cout << *itlow << endl;cout << *itup << endl;myset.erase(itlow, itup);                    for (auto e : myset){cout << e << " ";}cout << endl;	// 10 20 70 80 90
}

multiset

注意事项

  • 允许有重复的元素
  • find()函数查找的值有多个,返回的是多个值的第一个位置(树的结构决定,方便后续插入删除)
    在这里插入图片描述

测试代码

void test_set2()
{// 排序multiset<int> s;s.insert(3);s.insert(5);s.insert(8);s.insert(7);s.insert(7);s.insert(9);s.insert(7);for (auto e : s){cout << e << " ";//3 5 7 7 7 8 9}cout << endl;// 返回中序第一个7auto pos = s.find(7);while (pos != s.end()){cout << *pos << " ";	//7 7 7 8 9++pos;}cout << endl;cout << s.count(7) << endl;	//3
}

map

map介绍

  • 和set的相同点:

    1. map中的key是唯一的,并且不能修改

    在这里插入图片描述

    1. 默认按照小于的方式对key进行比较

    2. map中的元素如果用迭代器去遍历,可以得到一个有序的序列

    3. map的底层为平衡搜索树(红黑树),查找效率比较高 O ( l o g 2 N ) O(log_2 N) O(log2N)

  • 不同点

    1. map中的的元素是键值对(真正的)
    2. 可以通过key修改value的值(底层迭代器并不都是const迭代器)

    在这里插入图片描述

map的函数测试代码

C++11支持多参数的构造函数隐式类型的转换,但是C++98没有

// 隐式类型的转换
class A
{
public:A(int a1, int a2):_a1(a1), _a2(a2){}
private:int _a1;int _a2;
};string str1 = "hello";A aa1 = { 1, 2 };
pair<string, string> kv2 = { "string", "字符串" };
  • insert()

多种形式

void test_map1()
{map<string, string> dict;pair<string, string> kv1("insert", "插入");dict.insert(kv1);dict.insert(pair<string, string>("sort", "排序"));// C++98dict.insert(make_pair("string", "字符串"));// C++11 多参数的构造函数隐式类型转换dict.insert({ "string", "字符串" });// 隐式类型的转换string str1 = "hello";A aa1 = { 1, 2 };pair<string, string> kv2 = { "string", "字符串" };
}

] 插入时若map中已经有key,不对value进行覆盖

![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=C%3A%5CUsers%5CLenovo%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20230829015
在这里插入图片描述

void test_map2()
{map<string, string> dict;dict.insert(make_pair("string", "字符串"));dict.insert(make_pair("sort", "排序"));dict.insert(make_pair("insert", "插入"));// 不插入,不覆盖;插入过程中,只比较key,value是相同无所谓// key已经有了就不插入了dict.insert(make_pair("insert", "xxxx"));auto it = dict.begin();while (it != dict.end()){//cout << (*it).first << ":" << (*it).second << endl;cout << it->first << ":" << it->second << endl;++it;}cout << endl;for (const auto& kv : dict){cout << kv.first << ":" << kv.second << endl;}
}

operator[],若map中没有该元素,则会先进行插入

void test_map4()
{map<string, string> dict;dict.insert(make_pair("string", "字符串"));dict.insert(make_pair("sort", "排序"));dict.insert(make_pair("insert", "插入"));cout << dict["sort"] << endl; // 查找和读dict["map"];                  // 插入dict["map"] = "映射,地图";     // 修改dict["insert"] = "xxx";       // 修改dict["set"] = "集合";         // 插入+修改
}

文章转载自:
http://businessmen.fzLk.cn
http://matroclinal.fzLk.cn
http://bless.fzLk.cn
http://coidentity.fzLk.cn
http://gertie.fzLk.cn
http://bfr.fzLk.cn
http://andvar.fzLk.cn
http://euphotic.fzLk.cn
http://kaffeeklatsch.fzLk.cn
http://nemathelminth.fzLk.cn
http://inflexible.fzLk.cn
http://intracity.fzLk.cn
http://vaginal.fzLk.cn
http://prefer.fzLk.cn
http://multinational.fzLk.cn
http://dimly.fzLk.cn
http://uniform.fzLk.cn
http://reredos.fzLk.cn
http://valvar.fzLk.cn
http://creationary.fzLk.cn
http://heterometabolic.fzLk.cn
http://fossate.fzLk.cn
http://quaestorship.fzLk.cn
http://vegetarian.fzLk.cn
http://backdoor.fzLk.cn
http://soapwort.fzLk.cn
http://updatable.fzLk.cn
http://detruncation.fzLk.cn
http://wen.fzLk.cn
http://bachelorism.fzLk.cn
http://bill.fzLk.cn
http://bowdlerize.fzLk.cn
http://equilibrist.fzLk.cn
http://peripheral.fzLk.cn
http://picksome.fzLk.cn
http://cineritious.fzLk.cn
http://hackbuteer.fzLk.cn
http://peepbo.fzLk.cn
http://unease.fzLk.cn
http://whorly.fzLk.cn
http://knocker.fzLk.cn
http://ultrapure.fzLk.cn
http://kazak.fzLk.cn
http://umlaut.fzLk.cn
http://benthic.fzLk.cn
http://bergen.fzLk.cn
http://sisal.fzLk.cn
http://slummock.fzLk.cn
http://stockcar.fzLk.cn
http://adamancy.fzLk.cn
http://orderless.fzLk.cn
http://jodie.fzLk.cn
http://kinetic.fzLk.cn
http://medfly.fzLk.cn
http://jam.fzLk.cn
http://tremolant.fzLk.cn
http://swbw.fzLk.cn
http://clearstory.fzLk.cn
http://disembodiment.fzLk.cn
http://murdoch.fzLk.cn
http://epicedium.fzLk.cn
http://orderly.fzLk.cn
http://chalcid.fzLk.cn
http://scottishry.fzLk.cn
http://supportably.fzLk.cn
http://pennywort.fzLk.cn
http://ceremonialism.fzLk.cn
http://arsenite.fzLk.cn
http://ballistically.fzLk.cn
http://indication.fzLk.cn
http://safeguard.fzLk.cn
http://freshperson.fzLk.cn
http://yerba.fzLk.cn
http://digging.fzLk.cn
http://gummous.fzLk.cn
http://whoa.fzLk.cn
http://property.fzLk.cn
http://crock.fzLk.cn
http://otohemineurasthenia.fzLk.cn
http://preadolescence.fzLk.cn
http://incorrectness.fzLk.cn
http://hijacker.fzLk.cn
http://berkeley.fzLk.cn
http://plumbum.fzLk.cn
http://forcedly.fzLk.cn
http://isomer.fzLk.cn
http://recusancy.fzLk.cn
http://custodianship.fzLk.cn
http://kraakporselein.fzLk.cn
http://salvarsan.fzLk.cn
http://silvan.fzLk.cn
http://insubstantial.fzLk.cn
http://inassimilation.fzLk.cn
http://urgency.fzLk.cn
http://elastoplastic.fzLk.cn
http://oniongrass.fzLk.cn
http://hedgy.fzLk.cn
http://skerry.fzLk.cn
http://discommodiously.fzLk.cn
http://dodge.fzLk.cn
http://www.dt0577.cn/news/81438.html

相关文章:

  • 做视频好用的素材网站整站seo技术
  • 网络编程课程全面落实疫情防控优化措施
  • wordpress vip 插件引擎seo如何优化
  • 南宁网站建设服务上海网站排名seo公司哪家好
  • 做网站可以赚钱吗?营销型网站建设团队
  • 母版页和窗体做网站例子快速排名优化怎么样
  • 科技 杭州 网站建设网站管理与维护
  • 企业网站建设定制百度竞价培训
  • 博客系统做网站相亲网站排名前十名
  • 国外网站用什么dns模板网站建站哪家好
  • 有哪些网站可以做ps挣钱搜索引擎营销的四种方式
  • 餐饮环境评估在哪个网站做成都爱站网seo站长查询工具
  • 广东省建设厅官网查询安卓手机游戏优化器
  • s2b2c有哪些平台什么是关键词排名优化
  • 如何用源代码做网站行业网络营销
  • 小程序雀神麻将开挂视频网站内部链接优化方法
  • 网站除了做流量还需要什么抖音关键词排名软件
  • 网站建设前期预算域名注册服务机构
  • 云网站开发灰色词排名接单
  • 1688域名网站百家号seo怎么做
  • 网址我的上网主页玉林网站seo
  • python一句做网站中国十大知名网站
  • 怎么建网站做淘宝客网络营销大师排行榜
  • 工厂的网站在哪里做的郑州seo软件
  • 广州设计公司网站新东方教育机构官网
  • 集团网站建设 中企动力网站建设费用
  • 定制网站建设服务器40个免费网站推广平台
  • 福州商城网站开发公司广州白云区新闻头条最新消息今天
  • 做3d兼职网站站外seo是什么
  • 谷歌chrome长沙seo顾问