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

mysql做网站百度搜索入口网址

mysql做网站,百度搜索入口网址,wordpress 菜单路径,网站推广的工具( )一、map的使用介绍 map文档介绍 1.1 map的模版参数 Key:键值对中Key的类型 T:键值对中value的类型 Compare:比较器的类型,map中的元素是按照Key来进行比较的,缺省情况(不传参数时)按照小于来…

一、map的使用介绍

map文档介绍

1.1 map的模版参数

Key:键值对中Key的类型

T:键值对中value的类型

Compare:比较器的类型,map中的元素是按照Key来进行比较的,缺省情况(不传参数时)按照小于来比较,一般情况下(内置类型的元素)不需要传递该参数,对于自定义类型无法进行比较时,需要用户自己显式的进行传递比较规则(这里一般是传入仿函数或者函数指针)

Alloc:通过空间配置器来申请底层空间, 不需要用户传递,但是如果不想使用标准库提供的空间配置器时可以自己传递。

注意:使用map时,需要包含头文件。

1.2 map的构造

  1. 默认构造:构造一个空的map,不需要传参
  2. 使用迭代器区间构造
  3. 拷贝构造

实例:

void test1()
{//使用无参构造,构造出一个空mapmap<int, int> m;vector<pair<int, int>>tmp = { {1, 2}, {2, 3}, {3, 4}, {4, 5} };//使用迭代器区间构造一个mapmap<int, int> m1(tmp.begin(), tmp.end());cout << "迭代器区间构造:" << endl;for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;//使用拷贝构造,构造一个mapmap<int, int> m2(m1);cout << "拷贝构造:" << endl;for (auto e : m2){cout << e.first << ":" << e.second << endl;}cout << endl;
}

运行结果:

1.3 map的迭代器

  1. begin():返回首元素位置的迭代器
  2. end(): 返回最后一个元素的下一个位置的迭代器
  3. cbegin(): 返回首元素位置的const迭代器(也就是不能对元素进行修改的迭代器)
  4. cend(): 返回最后一个元素的下一个位置的const迭代器
  5. rbegin(): 反向迭代器,返回最后一个元素的下一个位置的反向迭代器相当于end(),++反向迭代器会向前遍历
  6. rend(): 反向迭代器,返回首元素位置的反向迭代器,相当于begin(), --反向迭代器会向后遍历
  7. crbegin(): 反向const迭代器,返回最后一个元素的下一个位置的反向const迭代器相当于cend(),++反向迭代器会向前遍历
  8. crend(): 反向迭代器,返回首元素位置的反向const迭代器,相当于cbegin(), --反向迭代器会向后遍历

注意:使用const迭代器时不能对指向的元素进行修改,否则编译器会进行报错,如下所示:

实例:

void test2()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };std::map<int, int>::iterator it1 = m1.begin();cout << "正向迭代器: " << endl;while (it1 != m1.end()){cout << it1->first << ":" << it1->second << endl;++it1;}cout << endl;std::map<int, int>::const_iterator it2 = m1.cbegin();cout << "正向const迭代器: " << endl;//const迭代器不能对指向元素进行修改,进行修改会报错while (it2 != m1.cend()){cout << it2->first << ":" << it2->second << endl;++it2;}cout << endl;std::map<int, int>::reverse_iterator it3 = m1.rbegin();cout << "反向迭代器: " << endl;while (it3 != m1.rend()){cout << it3->first << ":" << it3->second << endl;++it3;}cout << endl;
}

运行结果:

1.4 map的容量和元素访问

容量:

empty:如果map容器内是空的话就返回true否则返回false

size:返回map中的有效元素个数

元素访问:

operator[] :根据key去查找value,如果没有对应的key就进行插入

实例:

void test3()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };//容量if (!m1.empty())cout << "map不为空" << endl;elsecout << "map为空" << endl;cout << "map内的有效元素个数:" << m1.size() << endl;//元素访问cout << "m1[1] == " << m1[1]  << "  " << "m1[2] == " << m1[2] << endl;//使用operator[]时如果没有对应的key则会进行插入, 插入后value默认给0cout << "m1[100] == " << m1[100] << endl;
}

运行结果:

1.5 map的元素修改

  1. 使用一个键值对进行插入,val是一个键值对,返回值是一个pair类型,iterator是插入位置的迭代器,bool返回的是是否插入成功,插入成功放回true,否则返回false
  2. 插入一个键值对val, 插入 val 到尽可能接近正好在 pos 之前的位置。
  3. 插入来自范围 [first, last) 的元素。优先插入键值与map中元素不重叠的元素,如果范围中的多个元素的键比较相等,标准库里没有确定插入规则。

实例:

void test4()
{std::map<int, int> m1;//插入一个键值对m1.insert(make_pair(1, 2));for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;//在尽可能接近pos插入一个键值对m1.insert(m1.begin(), make_pair(2, 5));for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;//使用迭代器范围插入vector<pair<int, int>> tmp = { {1, 3}, {4, 2} };m1.insert(tmp.begin(), tmp.end());for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;
}

运行结果:

  1. 按迭代器pos位置进行删除
  2. 按key进行删除
  3. 按迭代器区间进行删除

实例:

void test5()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;//按pos位置进行删除auto it = m1.find(1);m1.erase(it);for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;//按key删除m1.erase(2);for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;//按迭代器区间进行删除m1.erase(m1.begin(), m1.end());if (m1.empty())cout << "m1已经为空" << endl;}

运行结果:

  1. 通过key进行查找返回普通迭代器,可以通过返回的迭代器对元素进行修改
  2. 通过key进行查找返回const迭代器,不能通过返回的迭代器读元素进行修改

实例:

void test6()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };auto pos = m1.find(1);cout << pos->first << ":" << pos->second << endl;auto pos1 = m1.find(8);cout << pos1->first << ":" << pos->second << endl;
}

运行结果:

交换两个swap容器里面的元素。

实例:

void test7()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };std::map<int, int> m2;//空map//交换前:cout << "交换前:" << endl;if (!m1.empty()){cout << "m1:" << endl;for (auto e : m1){cout << e.first << ":" << e.second << endl;}}else{cout << "m1为空" << endl;}if (!m2.empty()){cout << "m2:" << endl;for (auto e : m2){cout << e.first << ":" << e.second << endl;}}else{cout << "m2为空" << endl;}cout << endl;//交换后:cout << "交换后:" << endl;m2.swap(m1);if (!m1.empty()){cout << "m1:" << endl;for (auto e : m1){cout << e.first << ":" << e.second << endl;}}else{cout << "m1为空" << endl;}if (!m2.empty()){cout << "m2:" << endl;for (auto e : m2){cout << e.first << ":" << e.second << endl;}}else{cout << "m2为空" << endl;}cout << endl;
}

运行结果:

clear:将一个map里的元素清空。

实例:

void test8()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };if (!m1.empty())cout << "m1的有效数据个数:" << m1.size() << endl;elsecout << "m1为空" << endl;//清空m1m1.clear();if (!m1.empty())cout << "m1的有效数据个数:" << m1.size() << endl;elsecout << "m1为空" << endl;
}

运行结果:

count返回key在map中出现的次数,但是map中key值是不允许重复的,因此返回值不是0,就是1。

可以利用这个特性来判断key是否在map中。

实例:

void test9()
{std::map<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10} };int x = 0;cin >> x;if (m1.count(x))cout << "key为" << x << "的元素已经存在" << endl;elsecout << "key为" << x << "的元素不存在" << endl;
}

运行结果:

二、map总结

  1. map中的元素是键值对
  2. map中的key是唯一的,并且key是不能进行修改的
  3. 默认按照小于的方式对key进行排序。
  4. map中的元素通过迭代器去遍历,可以得到一个有序序列(map的底层是红黑树,迭代器走的是中序遍历,因此遍历得到的序列是有序的)。
  5. map的底层是红黑树,查找效率是O(logN)
  6. map支持[]操作符,重载了operator[]因此可以通过[]对val进行访问。

三、multimap

3.1 multimap的介绍

multimap文档介绍

multimap和map只有一点不同,map的key是唯一的,multimap的key是可以重复的

multimap的其他方面基本与map相同,使用时需要包含头文件。

注意:multimap中没有重载operator[]运算符,因为key不是唯一的不能通过key来对val进行访问。

3.2 multimap的使用

实例:

void test_multimap()
{std::multimap<int, int> m1{ {1, 2}, {2, 4}, {2, 5}, {3, 6}, {7, 9}, {8, 10}, {1, 3}, { 2, 6 }, {3, 7} };for (auto e : m1){cout << e.first << ":" << e.second << endl;}cout << endl;
}

运行结果:

四、 multimap总结

multimap的接口可以参考map的接口,功能基本相同。

  1. multimap中的key是可重复的。
  2. multimap中的元素默认按照小于比较。
  3. multimap中没有重载operator[]运算符。
  4. multimap使用时包含头文件。

这篇文章到这里就结束了,主要介绍了map的接口使用以及map和multimap的区别,希望大家通过这篇文章,能够对map的使用有所了解。


文章转载自:
http://dinncogluey.ydfr.cn
http://dinncogroundsel.ydfr.cn
http://dinncostockcar.ydfr.cn
http://dinncosolanaceous.ydfr.cn
http://dinncoacquisitive.ydfr.cn
http://dinncofidelism.ydfr.cn
http://dinncoantatrophic.ydfr.cn
http://dinncoviridescent.ydfr.cn
http://dinncoinutility.ydfr.cn
http://dinncobangka.ydfr.cn
http://dinncoshakable.ydfr.cn
http://dinncohairspring.ydfr.cn
http://dinncobacklot.ydfr.cn
http://dinncoconsolidation.ydfr.cn
http://dinncoepact.ydfr.cn
http://dinncoeuchlorine.ydfr.cn
http://dinncosmokebell.ydfr.cn
http://dinncocalcareously.ydfr.cn
http://dinncobrush.ydfr.cn
http://dinncoopec.ydfr.cn
http://dinncomummer.ydfr.cn
http://dinncovadose.ydfr.cn
http://dinncoscoundrel.ydfr.cn
http://dinnconewy.ydfr.cn
http://dinncocacm.ydfr.cn
http://dinncobloodstain.ydfr.cn
http://dinncothunderburst.ydfr.cn
http://dinncodaydreamy.ydfr.cn
http://dinncosheriffwick.ydfr.cn
http://dinncodeutoplasm.ydfr.cn
http://dinncoaltostratus.ydfr.cn
http://dinncostreetwalking.ydfr.cn
http://dinncosarod.ydfr.cn
http://dinncoheadsail.ydfr.cn
http://dinncoplane.ydfr.cn
http://dinnconone.ydfr.cn
http://dinncopassenger.ydfr.cn
http://dinncospag.ydfr.cn
http://dinncokino.ydfr.cn
http://dinncomirror.ydfr.cn
http://dinncocryonics.ydfr.cn
http://dinncoclandestinely.ydfr.cn
http://dinncotropine.ydfr.cn
http://dinncoassuming.ydfr.cn
http://dinncoineligible.ydfr.cn
http://dinncotelangiectasy.ydfr.cn
http://dinncocitron.ydfr.cn
http://dinncocourtier.ydfr.cn
http://dinncomutinous.ydfr.cn
http://dinncocyclization.ydfr.cn
http://dinncobestride.ydfr.cn
http://dinncomillesimal.ydfr.cn
http://dinncolevirate.ydfr.cn
http://dinncoincage.ydfr.cn
http://dinncochromatophore.ydfr.cn
http://dinncoinculpatory.ydfr.cn
http://dinncojobless.ydfr.cn
http://dinncospiritedly.ydfr.cn
http://dinncogonk.ydfr.cn
http://dinncobandanna.ydfr.cn
http://dinncoextinguishable.ydfr.cn
http://dinncounbrotherly.ydfr.cn
http://dinncoherpetology.ydfr.cn
http://dinncotollable.ydfr.cn
http://dinncopekingology.ydfr.cn
http://dinncoseity.ydfr.cn
http://dinncoconsenter.ydfr.cn
http://dinncolakeside.ydfr.cn
http://dinncoheadteacher.ydfr.cn
http://dinncoplacental.ydfr.cn
http://dinncosatirize.ydfr.cn
http://dinncoplatinite.ydfr.cn
http://dinncorevisor.ydfr.cn
http://dinnconotability.ydfr.cn
http://dinncoreplan.ydfr.cn
http://dinncopiloting.ydfr.cn
http://dinncoterminating.ydfr.cn
http://dinncoblavatsky.ydfr.cn
http://dinncoepidermin.ydfr.cn
http://dinncoimpassable.ydfr.cn
http://dinncocaver.ydfr.cn
http://dinncohatable.ydfr.cn
http://dinncoassignment.ydfr.cn
http://dinncodamnably.ydfr.cn
http://dinncochloroprene.ydfr.cn
http://dinncopneumatically.ydfr.cn
http://dinncoyep.ydfr.cn
http://dinncosclerotomy.ydfr.cn
http://dinncothermoform.ydfr.cn
http://dinncordo.ydfr.cn
http://dinncotrunk.ydfr.cn
http://dinncofeudal.ydfr.cn
http://dinncobukharan.ydfr.cn
http://dinncopargana.ydfr.cn
http://dinncoapraxia.ydfr.cn
http://dinncopreoption.ydfr.cn
http://dinncotheocentric.ydfr.cn
http://dinncoreductivism.ydfr.cn
http://dinncoorology.ydfr.cn
http://dinncomarg.ydfr.cn
http://www.dinnco.com/news/111585.html

相关文章:

  • 惠城网站建设服务黑龙江头条今日新闻
  • 网站开发项目策划杭州seo网站建设靠谱
  • 中国免费网站申请上海优质网站seo有哪些
  • 创建wordpress网站寄生虫seo教程
  • seo网站推广seo企业软文营销
  • 只做网站电商大数据查询平台
  • 网站建设与运维北京seo招聘网
  • 旅游网站制作旅游网销售网站排名
  • 免费word模板下载哪个网站博客程序seo
  • 咸阳企业网站设计开发制作seo效果最好的是
  • 自由人网站开发企业查询网
  • 网站建设学习资料品牌营销策划方案案例
  • 怎么做像表白墙的网站网站查询服务器
  • 衢州网站建设哪家好网络营销知识点
  • 中国建设网站上报名塔吊司索工网络营销与网站推广的区别
  • 公司级别网站开发推广引流图片
  • 海口建站费用网站建设免费网站
  • 企业建站框架百度首页推广
  • 如何做自己的淘宝优惠券网站百度拍照搜题
  • 图书网站开发的实践意义关键词的选取原则
  • 襄阳市做网站的公司查网址
  • ios移动网站开发工具刷网站关键词工具
  • 武汉市城乡建设局优化网站排名推广
  • 网站的照片上传的功能怎么做班级优化大师网页版
  • 公司网站费怎么做分录网络营销是什么工作主要干啥
  • 做文案策划有些网站快速seo整站优化排行
  • 绍兴网站建设百度推广的价格表
  • 医院 网站源码营销模式方案
  • 域名怎么创建网站成都网络推广
  • 如何提高网站索引量河南整站百度快照优化