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

环评在那个网站做今日头条热搜

环评在那个网站做,今日头条热搜,投资做任务赚钱的网站,软件销售具体怎么做的1. 序列式容器和关联式容器 string、vector、list、deque、array、forward_list等STL容器统称为序列式容器,因为逻辑结构为线性序列的数据结构,两个位置存储的值之间一般没有紧密的关联关系,比如交换一下,他依旧是序列式容器。顺…

1. 序列式容器和关联式容器

string、vector、list、deque、array、forward_list等STL容器统称为序列式容器,因为逻辑结构为线性序列的数据结构,两个位置存储的值之间一般没有紧密的关联关系,比如交换一下,他依旧是序列式容器。顺序容器中的元素是按他们在容器中的存储位置来顺序保存和访问的。

关联容器也是用来存储数据的,与序列式容器不同的是,关联式容器逻辑结构通常是非线性结构,两个位置有紧密的关联关系,交换一下,他的存储结构就被破坏了。顺序容器中的元素是按关键字来保存和访问的。关联式容器有map/set系列和unordered_map/unordered_set系列。

map和set底层是红黑树,红黑树是一颗平衡二叉搜索树。set是我们上一篇讲的key的搜索场景的结构,map是key/value搜索场景的结构。

2. set类的介绍

set类的声明如下代码所示

template<class T,class Compare = less<T>,class Alloc = allocator<T>>class set;
  • T是set底层关键字的类型。
  • set默认要求T支持小于比较,如果不支持或者想按自己的需求走可以自行实现仿函数来传给第二个模板参数。
  • set底层存储数据的内存是从空间配置器申请的,如果需要可以自己实现内存池,传给第三个参数。

 一般情况下我们都不需要传后两个模板参数。

set底层使用红黑树实现,增删查的效率是O(logN) ,迭代器遍历是走的搜索树的中序,所以是有序的

(1). 构造和迭代器

set的构造我们关注以下几个接口即可。

set的迭代器支持正向和反向迭代遍历,遍历默认按升序顺序,因为底层是二叉搜索树,迭代器遍历走的中序;支持迭代器就意味着支持范围for,set的iterator和const_iterator都不支持迭代器修改数据,修改关键字数据,破坏了底层搜索树的结构

构造函数的声明

//无参默认构造
explicit set (const key_compare& comp =key_compare(), const allocator_type& alloc = allocator_type() );//迭代器区间构造
template <class InputIterator>
set(InputIterator first , InputIterator last , const key_compare& comp = key_compare() , const allocator_type& = allocator_type());//拷贝构造
set (const set& x);//列表构造
set (initializer_list<value_type> il , const key_compare& comp = key_compare() , const allocator_type& alloc = allocator_type());

C++提供了关键字explicit,用来阻止不应该允许的经过转换构造函数进行的隐式类型转换的发生,声明为explicit的构造函数不能在隐式转换中使用。即只能被明确调用

set的迭代器是一个双向迭代器 bidirectional iterator

//正向迭代器
iterator begin();
iterator end();//反向迭代器
reverse_iterator rbegin();
reverse_iterator rend();
(2). 增删查
// 单个数据插⼊,如果已经存在则插⼊失败 
pair<iterator,bool> insert (const value_type& val);
// 列表插⼊,已经在容器中存在的值不会插⼊ 
void insert (initializer_list<value_type> il);
// 迭代器区间插⼊,已经在容器中存在的值不会插⼊ 
template <class InputIterator>
void insert (InputIterator first, InputIterator last);// 查找val,返回val所在的迭代器,没有找到返回end() 
iterator find (const value_type& val);
// 查找val,返回Val的个数 
size_type count (const value_type& val) const;// 删除⼀个迭代器位置的值 
iterator erase (const_iterator position);
// 删除val,val不存在返回0,存在返回1 
size_type erase (const value_type& val);
// 删除⼀段迭代器区间的值 
iterator erase (const_iterator first, const_iterator last);// 返回⼤于等val位置的迭代器 
iterator lower_bound (const value_type& val) const;
// 返回⼤于val位置的迭代器 
iterator upper_bound (const value_type& val) const;

insert插入和迭代器遍历使用代码如下

#include<iostream>
#include<set>
using namespace std;int main()
{set<int> sl;set<int, greater<int>> sg;sl.insert(5);sl.insert(3);sl.insert(8);sl.insert(3);sl.insert(5);sg.insert(5);sg.insert(3);sg.insert(8);set<int>::iterator itl = sl.begin();set<int>::iterator itg = sg.begin();while (itl != sl.end()){cout << *itl << "  ";itl++;}cout << endl;while (itg != sg.end()){cout << *itg << "  ";itg++;}sl.insert({ 2,8,3,9 });cout << endl;for (auto e : sl){cout << e << "  ";}cout << endl;set<string> strs = { "s","ii","affg" };for (auto e : strs){cout << e << "  ";}return 0;
}

输出结果为

 find查找(库里的和set自身的)和erase删除使用代码

#include<iostream>
#include<set>
using namespace std;int main()
{set<int> s = { 4,3,8,3,9,6,10 };for (auto e : s){cout << e << "  ";}cout << endl;//删除最小值s.erase(s.begin());for (auto e : s){cout << e << "  ";}cout << endl;//直接删除一个数xint x;cin >> x;int n = s.erase(x);if (n == 0){cout << x << "这个数不存在!" << endl;}for (auto e : s){cout << e << "  ";}cout << endl;//直接查找在利用迭代器删除xcin >> x;auto pos = s.find(x);if (pos != s.end()){s.erase(pos);}else{cout << x << "不存在!" << endl;}for (auto e : s){cout << e << "  ";}cout << endl;//算法库的查找O(N)auto pos1 = find(s.begin(), s.end(), x);//set自身实现的查找O(logN)auto pos2 = s.find(x);//利用count间接实现快速查找cin >> x;if (s.count(x)){cout << x << "在!" << endl;}else{cout << x << "不存在!" << endl;}return 0;}

输出结果为(6,1,2是我们选择删除的数x)

 通过lower_bound查找大于等于x1的迭代器,在通过upper_bound查找小于x2位置的迭代器

来删除这一段区间,代码如下

#include<iostream>
#include<set>
using namespace std;
int main()
{std::set<int> myset;for (int i = 1; i < 10; i++)myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90for (auto e : myset){cout << e << " ";}cout << endl;// 实现查找到的[itlow,itup)包含[30, 60]区间 // 返回 >= 30 auto itlow = myset.lower_bound(30);// 返回 > 60 auto itup = myset.upper_bound(60);// 删除这段区间的值 myset.erase(itlow, itup);for (auto e : myset){cout << e << " ";}cout << endl;return 0;
}

输出结果如下

3. multiset与set的差异

multiset和set的使用基本完全类似,主要区别点在于multiset支持值冗余,那么insert/find/count/erase都围绕着支持值冗余有所差异

  1. 相较于set,multiset是排序,但是不去重
  2. 相比set不同的是,x可能会存在多个,find查找中序遍历的第一个
  3. 与set不同的是,multiset的count会返回x的实际个数
  4. 相比set不同的是,multiseterase给值时会删除所有的为x的节点

 4. map类的介绍

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

template < class Key, // map::key_typeclass T, // map::mapped_typeclass Compare = less<Key>, // map::key_compareclass Alloc = allocator<pair<const Key,T> > > class map;

5. pair类型

map底层的红黑树节点中的数据,使用pair<Key,T>存储键值对数据。

通过 first与second来输出存储的键值

#include<iostream>
#include<set>
using namespace std;
int main()
{pair<int, int> p{ 1,56 };cout << p.first << endl;cout << p.second << endl;return 0;
}

6. map的构造

map的支持正向和反向迭代遍历,遍历默认按key的升序顺序,因为底层是⼆叉搜索树,迭代器遍历走的中序;支持迭代器就意味着支持范围for,map支持修改value数据,不支持修改key数据,修改关键字数据,破坏了底层搜索树的结构。

//⽆参默认构造 
explicit map (const key_compare& comp = key_compare(),const allocator_type& alloc = allocator_type());
//迭代器区间构造 
template <class InputIterator>map (InputIterator first, InputIterator last,const key_compare& comp = key_compare(),const allocator_type& = allocator_type());
// 拷⻉构造 
map (const map& x);
// initializer 列表构造 
map (initializer_list<value_type> il,const key_compare& comp = key_compare(),const allocator_type& alloc = allocator_type());

迭代器依然是一个双向迭代器与set类似

7. map的增删查

map增接口,插入的pair键值对数据,跟set所有不同,但是查和删的接口只用关键字key跟set是完全类似的,不过find返回iterator,不仅仅可以确认key在不在,还找到key映射的value,同时通过迭代还可以修改value。

// 单个数据插⼊,如果已经key存在则插⼊失败,key存在相等value不相等也会插⼊失败 
pair<iterator,bool> insert (const value_type& val);
// 列表插⼊,已经在容器中存在的值不会插⼊ 
void insert (initializer_list<value_type> il);
// 迭代器区间插⼊,已经在容器中存在的值不会插⼊ 
template <class InputIterator>
void insert (InputIterator first, InputIterator last);// 查找k,返回k所在的迭代器,没有找到返回end() 
iterator find (const key_type& k);
// 查找k,返回k的个数 
size_type count (const key_type& k) const;// 删除⼀个迭代器位置的值 
iterator erase (const_iterator position);
// 删除k,k存在返回0,存在返回1 
size_type erase (const key_type& k);
// 删除⼀段迭代器区间的值 
iterator erase (const_iterator first, const_iterator last);// 返回⼤于等k位置的迭代器 
iterator lower_bound (const key_type& k);
// 返回⼤于k位置的迭代器 
const_iterator lower_bound (const key_type& k) const;

基本使用

#include<iostream>
#include<set>
#include<map>
using namespace std;
int main()
{map<string, string> dict = { {"dog","狗"}, {"cat","猫"},{"pig","猪"} };//map<string, string>::iterator it = dict.begin();auto it = dict.begin();while (it != dict.end()){//cout << (*it).first <<":"<<(*it).second << endl;// map的迭代基本都使⽤operator->,这⾥省略了⼀个-> // 第⼀个->是迭代器运算符重载,返回pair*,第⼆个箭头是结构指针解引⽤取pair数据//cout << it.operator->()->first << ":" << it.operator->()-> second << endl;cout << it->first << ":" << it->second << endl;++it;}cout << endl;// insert插⼊pair对象的4种⽅式,对⽐之下,最后⼀种最⽅便 pair<string, string> kv1("first", "第一个");dict.insert(kv1);dict.insert(pair<string, string>("second", "第二个"));dict.insert(make_pair("sort", "排序"));dict.insert({ "auto", "自动的" });// "left"已经存在,插⼊失败 dict.insert({ "left", "左边" });// 范围for遍历 for (const auto& e : dict){cout << e.first << ":" << e.second << endl;}cout << endl;string str;while (cin >> str){auto ret = dict.find(str);if (ret != dict.end()){cout << "->" << ret->second << endl;}else{cout << "无此单词,请重新输入" << endl;}}return 0;
}

8. map的数据修改

map支持修改mapped_type数据,不支持修改key数据,因为这样会修改关键字数据,从而破坏了底层搜索树的结构。

map第⼀个⽀持修改的⽅式时通过迭代器,迭代器遍历时或者find返回key所在的iterator修改,map 还有⼀个⾮常重要的修改接口operator[],但是operator[]不仅仅支持修改,还⽀持插⼊数据和查找数据,所以他是⼀个多功能复合接口

需要注意从内部实现⻆度,map这⾥把我们传统说的value值,给的是T类型,typedef为 mapped_type。而value_type是红⿊树结点中存储的pair键值对值。⽇常使⽤我们还是习惯将这⾥的 T映射值叫做value。

insert插入一个pair<key , T>对象

1. 如果key已经在map中,插入失败,则返回一个pair<iterator,bool>对象,返回pair对象first是新插入key所在节点的迭代器,second是false。

2. 如果key不在map中,插入成功,也返回一个pair<iterator,bool>对象,返回pair对象first是新插入key所在节点的迭代器,second是true。

也就是说无论插入成功与否,返回pair<iterator,bool>对象的first都会指向key所在的迭代器

那么就意味着insert插入失败时充当了查找的功能,正是因为这一点,insert可以用来实现operator[]。

需要注意的是这里有两个pair,一个是map底层红黑树节点中存储的pair<key,T>,另一个是insert的返回值pair<iterator,bool>

#include<iostream>
#include<algorithm>
#include<iostream>
#include<map>using namespace std;int main()
{string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "葡萄","李","梨" };map<string, int> CountMap1;map<string, int> CountMap2;for (const auto& str : arr){auto val = CountMap1.find(str);//看是否在map中有str水果if (val == CountMap1.end()){CountMap1.insert({ str,1 });//不在就插入水果,将次数设置为1}else{val->second++;//在就将查到的节点中的水果对应val值++}}for (const auto& e : CountMap1){cout << e.first << ":" << e.second << endl;}cout << endl;for (const auto& str : arr){auto val = str;//key第一次出现,就插入这个值并将其val++CountMap2[val]++;}for (const auto& e : CountMap2){cout << e.first << ":" << e.second << endl;}cout << endl;return 0;
}

输出结果如下

9. multimap和map的差异

multimap和map的使用基本完全类似,主要区别点在于multimap支持关键值key冗余,insert/find/count/erase也都围绕着支持关键字key冗余有所差异,这里跟set和multiset区别完全一样,比如find返回中序的第一个。其次就是multimap不支持[],因为key支持冗余,[]就只能支持插入,不能支持修改了


这篇就到这里啦ヾ( ̄▽ ̄)Bye~Bye~


文章转载自:
http://heterotroph.nrpp.cn
http://discontentment.nrpp.cn
http://snagged.nrpp.cn
http://infractor.nrpp.cn
http://nonreduction.nrpp.cn
http://minesweeping.nrpp.cn
http://earthshine.nrpp.cn
http://fratching.nrpp.cn
http://portwide.nrpp.cn
http://arthrosis.nrpp.cn
http://menacingly.nrpp.cn
http://rockless.nrpp.cn
http://businesslike.nrpp.cn
http://roomy.nrpp.cn
http://requin.nrpp.cn
http://digitalose.nrpp.cn
http://herdic.nrpp.cn
http://anaphylactoid.nrpp.cn
http://battlewagon.nrpp.cn
http://barefisted.nrpp.cn
http://hidalgo.nrpp.cn
http://pentagynous.nrpp.cn
http://epithelia.nrpp.cn
http://unhealthily.nrpp.cn
http://divaricate.nrpp.cn
http://filmgoer.nrpp.cn
http://peplos.nrpp.cn
http://undyed.nrpp.cn
http://bootlegger.nrpp.cn
http://tropicalize.nrpp.cn
http://dihedron.nrpp.cn
http://molehill.nrpp.cn
http://ergophile.nrpp.cn
http://underslept.nrpp.cn
http://enfeoff.nrpp.cn
http://sambhar.nrpp.cn
http://archespore.nrpp.cn
http://supermultiplet.nrpp.cn
http://thanatism.nrpp.cn
http://relational.nrpp.cn
http://horse.nrpp.cn
http://leadenhall.nrpp.cn
http://calabazilla.nrpp.cn
http://zincode.nrpp.cn
http://sneery.nrpp.cn
http://playmobile.nrpp.cn
http://chalone.nrpp.cn
http://ferdus.nrpp.cn
http://stigmatization.nrpp.cn
http://lederhosen.nrpp.cn
http://saltatorial.nrpp.cn
http://tallyshop.nrpp.cn
http://amphitrite.nrpp.cn
http://nonsulfide.nrpp.cn
http://transudatory.nrpp.cn
http://whisk.nrpp.cn
http://jowl.nrpp.cn
http://aquila.nrpp.cn
http://consonance.nrpp.cn
http://allotheism.nrpp.cn
http://triphibious.nrpp.cn
http://prisoner.nrpp.cn
http://thermion.nrpp.cn
http://jungfrau.nrpp.cn
http://clonicity.nrpp.cn
http://episcopalism.nrpp.cn
http://insanely.nrpp.cn
http://passage.nrpp.cn
http://rattoon.nrpp.cn
http://translate.nrpp.cn
http://wollongong.nrpp.cn
http://rbds.nrpp.cn
http://convector.nrpp.cn
http://handyman.nrpp.cn
http://misbound.nrpp.cn
http://magnicide.nrpp.cn
http://jingled.nrpp.cn
http://jatha.nrpp.cn
http://radioamplifier.nrpp.cn
http://shikaree.nrpp.cn
http://default.nrpp.cn
http://zebra.nrpp.cn
http://lawmaker.nrpp.cn
http://prevail.nrpp.cn
http://subterranean.nrpp.cn
http://trunnion.nrpp.cn
http://radicalization.nrpp.cn
http://railroading.nrpp.cn
http://monographic.nrpp.cn
http://unconformity.nrpp.cn
http://linkboy.nrpp.cn
http://technic.nrpp.cn
http://octennial.nrpp.cn
http://trimaran.nrpp.cn
http://terebrate.nrpp.cn
http://eeo.nrpp.cn
http://actuator.nrpp.cn
http://cosmogenic.nrpp.cn
http://squattocracy.nrpp.cn
http://lollygag.nrpp.cn
http://www.dt0577.cn/news/73677.html

相关文章:

  • dw用表格做网站如何在手机上建立自己的网站
  • 网站建设维护招聘要求百度网站名称和网址
  • 百度h5转换器seo整站优化什么价格
  • 贵州网站开发流程网络推广工作好做不
  • php wordpress xmlrpc常用的seo查询工具有哪些
  • 网站建设的内容有哪些广告联盟app下载官网
  • 市场营销研究生好考吗搜索引擎优化的核心是
  • 做关于家乡的网站有名的seo外包公司
  • 织梦网站如何更新系统百度搜索资源管理平台
  • 西凤酒网站建设的目标青岛专业网站制作
  • 国内使用vue做的网站代写文章的平台有哪些
  • 免费微信网站怎么做建站系统主要包括
  • 网站建设的目的和意义免费做网站的网站
  • wordpress菜单显示在哪快速优化seo软件推广方法
  • 中文企业网站html模板百度竞价品牌广告
  • wordpress 搬家 换域名潍坊seo推广
  • 做网站排名大概要多少短视频营销的优势
  • 服装网站建设与规划杭州龙席网络seo
  • 怎么管理网站的内容seo公司网站
  • icp备案流程优化防控举措
  • 个人网站栏目免费推广网站入口
  • 建行商城网站网络推广营销
  • 邵阳做网站的有哪些appstore关键词优化
  • 泊头市做网站湘潭seo快速排名
  • 桂林哪里做网站学网络营销
  • 51做网站广州惠州seo招聘
  • 智慧治水网站系统建设无排名优化
  • 网站设计步骤图片外贸网站推广平台
  • 做mp3链接的网站搜索引擎优化规则
  • 深圳广告公司画册设计seo方法