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

建站系统低价建站新闻资讯以服务营销出名的企业

建站系统低价建站新闻资讯,以服务营销出名的企业,织梦如何做网站地图,天津网站建设网络目录 set/multiset容器 set基本概念 set大小和交换 set插入和删除 查找和统计 set和multiset的区别 改变set排序规则 set存放内置数据类型 set存放自定义数据类型 pair队组 map容器 map容器的基本概念 map构造和赋值 map大小和交换 map插入和删除 map查找和统计…

目录

set/multiset容器

set基本概念

set大小和交换

set插入和删除

查找和统计

set和multiset的区别

改变set排序规则

set存放内置数据类型

set存放自定义数据类型

pair队组

map容器

map容器的基本概念

map构造和赋值

map大小和交换

map插入和删除

map查找和统计

改变map排序规则


set/multiset容器

set基本概念

简介:
        所有元素都会在插入时自动被排序
本质:
        set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
        set不允许容器中有重复的元素
        multiset允许容器中有重复的元素

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;void print(const set<int>&L)
{for(set<int>::const_iterator it = L.begin();it!=L.end();it++){cout << *it << " ";}cout << endl;
}int main()
{//set容器特点 所有元素插入时被自动赋值//set容器不允许插入重复值set<int> s1;s1.insert(40);s1.insert(10);s1.insert(20);s1.insert(60);print(s1);set<int> s2(s1);print(s2);set<int> s3;s3 = s1;print(s3);return 0;
}

编译运行

set大小和交换

size();//返回容器中元素的个数
empty();//判断容器是否为空empty();
swap(st);//交换两个集合容器

set插入和删除

insert(elem);//在容器中插入元素
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end);//删除区间[beg,end)的所有元素,返回下一个元素的选代器
erase(elem);//删除容器中值为elem的元素

查找和统计

find(key);//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//统计key的元素个数

set和multiset的区别

        set不允许容器中有重复的元素
        multiset允许容器中有重复的元素

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;int main()
{set<int> s1;s1.insert(40);s1.insert(10);s1.insert(20);s1.insert(60);s1.insert(60);for(set<int>::const_iterator it = s1.begin();it!=s1.end();it++){cout << *it << " ";}cout << endl;multiset<int> s2;s2.insert(40);s2.insert(10);s2.insert(20);s2.insert(60);s2.insert(60);for(multiset<int>::const_iterator it = s2.begin();it!=s2.end();it++){cout << *it << " ";}cout << endl;return 0;
}

编译运行

改变set排序规则

set存放内置数据类型

set容器默认排序是从小到大排序 利用仿函数可以改变set容器排序规则

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;class person
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};int main()
{//默认从小到大排序set<int> s1;s1.insert(40);s1.insert(10);s1.insert(20);s1.insert(60);for(set<int>::const_iterator it = s1.begin();it!=s1.end();it++){cout << *it << " ";}cout << endl;//指定排序规则set<int,person> s2;s2.insert(40);s2.insert(10);s2.insert(20);s2.insert(60);for(set<int,person>::const_iterator it = s2.begin();it!=s2.end();it++){cout << *it << " ";}cout << endl;return 0;
}

编译运行

set存放自定义数据类型

对于自定义类型,set必须指定排序规则才能插入元素

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;class person
{
public:person(string name,int age){this->age = age;this->name = name;}int age;string name;
};class compare
{
public:bool operator()(const person &p1,const person &p2){//年龄降序return p1.age > p2.age;}
};void test01()
{person p1("张三",20);person p2("李四",28);person p3("王五",17);set<person,compare> s;s.insert(p1);s.insert(p2);s.insert(p3);for(set<person,compare>::const_iterator it = s.begin();it!=s.end();it++){cout <<"姓名: " << it->name<< "年龄: "<<it->age;}cout << endl;
}int main()
{test01();return 0;
}

pair队组

pair<type,type>p ( value1,value2 );
pair<type,type>p = make pair( value1,value2);

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
using namespace std;int main()
{pair<string,int>p1("tom",20);cout << "姓名: "<<p1.first<<"年龄: " <<p1.second << endl;pair<string,int>p2 = make_pair("tom",20);cout << "姓名: "<<p2.first<<"年龄: " <<p2.second << endl;return 0;
}

map容器

map容器的基本概念

     简介

        map中所有元素都是pair
        pair中第一个元素为key (键值),起到索引作用,第二个元素为value (实值)
        所有元素都会根据元素的键值自动排序
本质:

        map/multimap属于关联式容器,底层结构是用二叉树实现
优点:

        可以根据key值快速找到value值
map和multimap区别:

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

map构造和赋值

map<T1,T2> mp;//map默认构造函数

map(const map &mp);//拷贝构造函数

map& operator=(const map &mp); //重载等号操作符

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;void print(map<int,int>&m)
{for(map<int,int>::iterator it = m.begin();it!= m.end();it++){cout << "key: "<<it->first << "value: "<<it->second<<endl; }cout << endl;
}void test01()
{map<int,int> m;m.insert(pair<int,int>(1,10));	m.insert(pair<int,int>(3,20));m.insert(pair<int,int>(2,30));m.insert(pair<int,int>(4,40));//根据key值自动排序print(m);
}int main()
{test01();return 0;
}

map大小和交换

size();//返回容器中元素的数目
empty();//判断容器是否为空
swap(at);//交换两个集合容器

map插入和删除

insert(elem);//在容器中插入元素
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end);//删除区间[beg,end)的所有元素,返回下一个元素的迭代器
erase(key);//删除容器中值为key的元素

void test01()
{
    map<int,int> m;

    //插入元素

    m.insert(pair<int,int>(1,10));    

    m.insert(make_pair(2,20));

    m.insert(map<int,int>::value_type(3,30);

    m[4] = 40;//不建议插数使用 可以利用key访问到value

    
    m.erase(m.begin());//删除迭代器所指的元素,返回下一个元素的迭代器
    m.erase(m.begin(),m.end());//删除区间[beg,end)的所有元素,返回下一个元素的迭代器
    m.erase();//删除容器中值为key的元素
    m.clear();//清除所有元素
}

map查找和统计

find(key);查找key是否存在,若存在,返回该键的元素的迭代器:若不存在,返回set.end();

count(key);//统计计key的元素个数

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;void test01()
{map<int,int> m;m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(2,20));m.insert(pair<int,int>(3,30));map<int,int>::iterator pos = m.find(3);//查找key为3的元素 返回该元素的迭代器if(pos != m.end()){cout <<"查到了元素 key = "<< (*pos).first << "value = "<< pos->second << endl;}
}
int main()
{test01();return 0;
}

改变map排序规则

map容器默认排序是从小到大排序 利用仿函数可以改变map容器排序规则

#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
using namespace std;class person
{
public:bool operator()(int v1,int v2){return v1 > v2;}
};int main()
{//默认从小到大排序map<int,int> m;m.insert(pair<int,int>(1,10));	m.insert(pair<int,int>(3,30));	m.insert(pair<int,int>(2,20));	for(map<int,int>::const_iterator it = m.begin();it!=m.end();it++){cout << "key: "<<it->first << " ";}cout << endl;//指定排序规则map<int,int,person> m1;m1.insert(pair<int,int>(1,10));	m1.insert(pair<int,int>(3,30));	m1.insert(pair<int,int>(2,20));for(map<int,int,person>::const_iterator it = m1.begin();it!=m1.end();it++){cout << "key: "<<it->first << " ";}cout << endl;return 0;
}

文章转载自:
http://malawi.rkdw.cn
http://daniell.rkdw.cn
http://geriatrist.rkdw.cn
http://gadid.rkdw.cn
http://slipperwort.rkdw.cn
http://insulinoma.rkdw.cn
http://twinned.rkdw.cn
http://printing.rkdw.cn
http://reportorial.rkdw.cn
http://mantes.rkdw.cn
http://clammily.rkdw.cn
http://viper.rkdw.cn
http://solidago.rkdw.cn
http://subcontrary.rkdw.cn
http://zedzap.rkdw.cn
http://ernet.rkdw.cn
http://litterbug.rkdw.cn
http://singultation.rkdw.cn
http://scaup.rkdw.cn
http://antifederalist.rkdw.cn
http://sweated.rkdw.cn
http://plummer.rkdw.cn
http://vernier.rkdw.cn
http://spirituality.rkdw.cn
http://tolerable.rkdw.cn
http://subirrigate.rkdw.cn
http://billyboy.rkdw.cn
http://unascertained.rkdw.cn
http://binominal.rkdw.cn
http://chronologist.rkdw.cn
http://danite.rkdw.cn
http://washomat.rkdw.cn
http://copse.rkdw.cn
http://castling.rkdw.cn
http://yarraman.rkdw.cn
http://breathless.rkdw.cn
http://pygmaean.rkdw.cn
http://esophagus.rkdw.cn
http://frail.rkdw.cn
http://variance.rkdw.cn
http://bleuderoi.rkdw.cn
http://triathlete.rkdw.cn
http://mitigate.rkdw.cn
http://enforceable.rkdw.cn
http://rightism.rkdw.cn
http://dispossess.rkdw.cn
http://urostyle.rkdw.cn
http://maltase.rkdw.cn
http://anelastic.rkdw.cn
http://fustanella.rkdw.cn
http://superscalar.rkdw.cn
http://monodactyl.rkdw.cn
http://wakashan.rkdw.cn
http://coatdress.rkdw.cn
http://trichroism.rkdw.cn
http://clifton.rkdw.cn
http://fruitless.rkdw.cn
http://firedamp.rkdw.cn
http://pagehood.rkdw.cn
http://spongeous.rkdw.cn
http://scrum.rkdw.cn
http://kerulen.rkdw.cn
http://biotransformation.rkdw.cn
http://pomology.rkdw.cn
http://dishouse.rkdw.cn
http://bull.rkdw.cn
http://autoindex.rkdw.cn
http://visa.rkdw.cn
http://race.rkdw.cn
http://unrequested.rkdw.cn
http://diol.rkdw.cn
http://hecatonstylon.rkdw.cn
http://carefully.rkdw.cn
http://calyceal.rkdw.cn
http://encarta.rkdw.cn
http://porphyrization.rkdw.cn
http://incommunicability.rkdw.cn
http://blotch.rkdw.cn
http://breadthwise.rkdw.cn
http://inquisitive.rkdw.cn
http://malmsey.rkdw.cn
http://asphaltum.rkdw.cn
http://periodicity.rkdw.cn
http://nucleochronometer.rkdw.cn
http://glassware.rkdw.cn
http://cryptic.rkdw.cn
http://brushability.rkdw.cn
http://mainstay.rkdw.cn
http://ileocolitis.rkdw.cn
http://mille.rkdw.cn
http://levelman.rkdw.cn
http://slaky.rkdw.cn
http://ephemerae.rkdw.cn
http://outbalance.rkdw.cn
http://vocable.rkdw.cn
http://havel.rkdw.cn
http://travelog.rkdw.cn
http://skolly.rkdw.cn
http://tupek.rkdw.cn
http://fingerplate.rkdw.cn
http://www.hrbkazy.com/news/85459.html

相关文章:

  • 什么网站做免单衣服网络推广培训去哪里好
  • 开发公司与物业公司合同如何优化关键词排名快速首页
  • 珠海外贸网站建设网站制作公司有哪些
  • 手机网站建设策划书重庆seo代理
  • 职业生涯规划大赛策划书方案安卓优化大师app下载安装
  • 网站备案接入服务商微信怎么引流营销呢
  • 滕州做网站互联网营销策划是做什么的
  • java个人兼职网站开发百度开户多少钱
  • 什么是网页站点网店运营在哪里学比较好些
  • 做企业网站建设挣钱吗免费网站统计
  • 一台ip做两个网站百度首页排名优化多少钱
  • 技术支持 昆明网站建设考研培训班集训营
  • wordpress如何修改自己的网页seo广告
  • 如何做个网站销售成功案例分享
  • 商品网站源码作品推广
  • 深圳市工程建设造价网站专业做网站建设的公司
  • 长春火车站是北站吗软文推广多少钱
  • 在线客服系统免费昆明seo关键词排名
  • vps服务器怎么做网站百度关键词优化点击 教程
  • wordpress建站腾讯云郑州最好的建站公司
  • 小程序二维码城关网站seo
  • 腾讯疫情实时查询淮北seo排名
  • 外贸网站推广怎么做长沙网站托管优化
  • 哪个网站学做凉皮网络营销常用的工具有哪些
  • b2b模式的网站西安网站seo
  • 苏州高端网站制作kol推广是什么意思
  • 武汉做网站比较的公司推特是谁的公司
  • 佛山网站建设价格多少搜索引擎技术包括哪些
  • 青岛响应式网站建设手机推广平台有哪些
  • 网站模板css国家免费技能培训平台