日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)

發(fā)布時(shí)間:2025/3/16 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL 整理(map、set、vector、list、stack、queue、deque、priority_queue) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

向量(vector)?<vector>

連續(xù)存儲(chǔ)的元素<vector>

Vector<int>c;

c.back()????傳回最后一個(gè)數(shù)據(jù),不檢查這個(gè)數(shù)據(jù)是否存在。

c.clear()???? 移除容器中所有數(shù)據(jù)。

c.empty()???判斷容器是否為空。

c.front()???? 傳回地一個(gè)數(shù)據(jù)。

c.pop_back() 刪除最后一個(gè)數(shù)據(jù)。

c.push_back(elem)? 在尾部加入一個(gè)數(shù)據(jù)。

c[i]?等同于 c.at(i);

列表(list)?<list>

由節(jié)點(diǎn)組成的雙向鏈表,每個(gè)結(jié)點(diǎn)包含著一個(gè)元素<list>

list<int>?list1(1,2,3)

front()返回第一個(gè)元素的引用? int?nRet =list1.front()????// nRet = 1

back()返回最后一元素的引用? int?nRet =list1.back()?????// nRet = 3

push_back()增加一元素到鏈表尾?list1.push_back(4)???????//list1(1,2,3,4)

push_front()增加一元素到鏈表頭 ?list1.push_front(4)??????//list1(4,1,2,3)

pop_back()刪除鏈表尾的一個(gè)元素?list1.pop_back()??????????//list1(1,2)

pop_front()刪除鏈表頭的一元素? list1.pop_front() ?????????//list1(2,3)

clear()刪除所有元素?? list1.clear();???// list1空了,list1.size()=0

sort()對(duì)鏈表排序,默認(rèn)升序(可自定義回調(diào)函數(shù))?

list對(duì)象L1(4,3,5,1,4)? L1.sort();????????????????

?//L1(1,3,4,4,5) L1.sort(greater<int>());?

//L1(5,4,4,3,1)

insert()在指定位置插入一個(gè)或多個(gè)元素

list1.insert(++list1.begin(),9);??// list1(1,9,2,3)

list1.insert(list1.begin(),2,9);??// list1(9,9,1,2,3);

list1.insert(list1.begin(),list2.begin(),--list2.end());//list1(4,5,1,2,3);

swap()交換兩個(gè)鏈表(兩個(gè)重載)

list1.swap(list2);???//list1(4,5,6)?list2(1,2,3)

unique()刪除相鄰重復(fù)元素

L1(1,1,4,3,5,1)

L1.unique();// L1(1,4,3,5,1)

merge()合并兩個(gè)有序鏈表并使之有序

//?升序list1.merge(list2);??????????//list1(1,2,3,4,5,6) list2現(xiàn)為空

//?降序L1(3,2,1), L2(6,5,4)L1.merge(L2,?greater<int>()); //list1(6,5,4,3,2,1) list2現(xiàn)為空

reverse()反轉(zhuǎn)鏈表:list1.reverse();?????//list1(3,2,1)

remove()刪除鏈表中匹配值的元素(匹配元素全部刪除)list對(duì)象L1(4,3,5,1,4)L1.remove(4);???????????????//L1(3,5,1);

empty()判斷是否鏈表為空bool bRet =L1.empty(); //若L1為空,bRet = true,否則bRet = false。

rbegin()返回鏈表最后一元素的后向指針(reverse_iteratoror const)list<int>::reverse_iterator?it?=?list1.rbegin();??//*it = 3

rend()返回鏈表第一元素的下一位置的后向指針list<int>::reverse_iteratorit =?list1.rend(); // *(--riter) = 1

?

集合(set)?<set>

由節(jié)點(diǎn)組成的紅黑樹,每個(gè)節(jié)點(diǎn)都包含著一個(gè)元素,節(jié)點(diǎn)之間以某種作用于元素對(duì)的謂詞排列,沒有兩個(gè)不同的元素能夠擁有相同的次序?<set>

set<type>: 以less<>為排序法則的set

set<type,op>: 以op為排序法則的set

struct?op{

????bool?operator()(const?rec&a,const?rec&b){

????????return?a.x<b.x||a.x==b.x&&a.y<b.y;

????}

};

1.1 set::begin

功能:返回第一個(gè)元素的定位器(iterator)的地址。

set <char>::iterator cp;

ctr.insert('a');

ctr.insert('b');

cp=ctr.begin(); //定位到ctr 的開始位置

1.2 set::clear

功能:將一個(gè)set 容器的全部元素刪除。

1.3 set::count

功能:返回對(duì)應(yīng)某個(gè)關(guān)鍵字的元素的個(gè)數(shù)。好像都是1吧

1.4 set::empty

功能:測(cè)試一個(gè)set 容器是否為空。

1.5 set::end

功能:返回最后一個(gè)元素后面的定位器(iterator)的地址。

1.7 set::erase

功能:將一個(gè)或一定范圍的元素刪除。

1.8 set::find

功能:求出與給定的關(guān)鍵字相等的元素的定位器。

set <string> ctr;

??? ctr.insert("abc");

??? ctr.insert("abcd");

??? ctr.insert("abcf");

??? set <string>::iterator cp;

??? cp=ctr.find("abc"); //查找key=1 的元素

??? if(cp!=ctr.end())

??????? cout<<*cp <<endl;//顯示abc

??? cp=ctr.find("adf"); //查找key=2 的元素

??? if(cp!=ctr.end())

??????? cout<<*cp <<endl;//不顯示

??? cp=ctr.find("gfv"); //查找key=3 的元素

??? if(cp!=ctr.end())

??????? cout<<*cp <<endl;// 不顯示

1.10 set::insert

功能:將一個(gè)元素或者一定數(shù)量的元素插入到set 的特定位置中。

1.25 set::upper_bound

功能:求出指向第一個(gè)關(guān)鍵字的值是大于一個(gè)給定值的元素的定位器。

cp=ctr.upper_bound(2);//輸出比2大的最小元素

多重集合(multiset)<?set>

允許存在兩個(gè)次序相等的元素的集合?<set>

multiset<type>: 以less<>為排序法則的multiset

multiset<type, op>: 以op為排序法則的multise

struct?op{

bool?operator()(const?rec&a,const?rec&b){

????????return?a.x<b.x||a.x==b.x&&a.y<b.y;

????}

};

multiset<int>h;

__typeof(h.begin()) c=h.begin();//c指向h序列中第一個(gè)元素的地址,第一個(gè)元素是最小的元素

printf("%d ",*c);//將地址c存的數(shù)據(jù)輸出

h.erase(c);//從h序列中將c指向的元素刪除

__typeof()是個(gè)好東西~

棧(stack)??<stack>

后進(jìn)先出的值的排列?<stack>

定義一個(gè)stack的變量stack<int> s;

入棧,如例:s.push(x);

出棧,如例:s.pop();注意,出棧操作只是刪除棧頂元素,并不返回該元素。

訪問棧頂,如例:s.top()

判斷棧空,如例:s.empty(),當(dāng)棧空時(shí),返回true。

訪問棧中的元素個(gè)數(shù),如例:s.size()

隊(duì)列(queue)?<queue>

先進(jìn)先出的執(zhí)的排列?<queue>

定義一個(gè)queue的變量???? queue<Type> M
查看是否為空范例??????? M.empty()???是的話返回1,不是返回0;
從已有元素后面增加元素M.push()
輸出現(xiàn)有元素的個(gè)數(shù)????? ??M.size()
顯示第一個(gè)元素????????? ????M.front()
顯示最后一個(gè)元素??????? ???M.back()
清除第一個(gè)元素????????? ????M.pop()

優(yōu)先隊(duì)列(priority_queue)?<queue>

元素的次序是由作用于所存儲(chǔ)的值對(duì)上的某種謂詞決定的的一種隊(duì)列?<queue>

1、默認(rèn)從大到小

priority_queue<int>?qi;

2、從小到大輸出可以傳入一個(gè)比較函數(shù),使用functional.h函數(shù)對(duì)象作為比較函數(shù),great<int>(小到大) less<int>(大到小)

priority_queue<int,?vector<int>,?greater<int>?>qi2; 第二個(gè)參數(shù)為容器類型。第三個(gè)參數(shù)為比較函數(shù)。

3、自定義:

struct cmp? // 最小優(yōu)先隊(duì)列

{

??? bool operator()(const long long i,constlong long j)

??? {

??????? return i>j;

??? }

};

priority_queue<int,vector<longlong>,cmp> Q;

?

?

struct node // 最小優(yōu)先隊(duì)列

{

??? int id,len;

??? bool operator < (const node &b)const// 只能重載小于號(hào)

??? {

??????? return len>b.len;

??? }

};

priority_queue<node>Q;

?

Q.empty() // 判斷隊(duì)列是否為空返回ture表示空返回false表示空 bool

Q.top() // 返回頂端元素的值元素還在隊(duì)列里

Q.pop() // 刪除頂端元素 void

Q.push(V) // 把long long型的數(shù)V加入到隊(duì)列里它會(huì)制動(dòng)條件V的位置void

Q.size() // 返回隊(duì)列里元素個(gè)數(shù) unsigned int

雙端隊(duì)列(deque)?<deque>

連續(xù)存儲(chǔ)的指向不同元素的指針?biāo)M成的數(shù)組<deque>

deque<int>c

c.pop_back()????? 刪除最后一個(gè)數(shù)據(jù)。

c.pop_front()????? 刪除頭部數(shù)據(jù)。

c.push_back(elem) 在尾部加入一個(gè)數(shù)據(jù)。

c.push_front(elem) 在頭部插入一個(gè)數(shù)據(jù)。

c.clear()????? ????移除容器中所有數(shù)據(jù)。

c.front()????????? 傳回地一個(gè)數(shù)據(jù)。

c.back()????????? 傳回最后一個(gè)數(shù)據(jù),不檢查這個(gè)數(shù)據(jù)是否存在。

c.size()?????????? 返回容器中實(shí)際數(shù)據(jù)的個(gè)數(shù)。

c.empty()???????? 判斷容器是否為空。

c[i]?等同于 c.at(i);

?

?

映射(map)?由{鍵,值}對(duì)組成的集合?<map>

以某種作用于鍵對(duì)上的謂詞排列?<map>

三種插入數(shù)據(jù)的方法(第一種和第二種是一樣的,當(dāng)map中有這個(gè)關(guān)鍵字時(shí),insert操作是插入不了數(shù)據(jù)的,用數(shù)組方式會(huì)覆蓋以前該關(guān)鍵字對(duì)應(yīng)的值)

1. map<int,string>mapStudent;

?? mapStudent.insert(pair<int,string>(1,"student_one"));

mapStudent.insert(pair<int,string>(2,"student_two"));

2. map<int,string>mapStudent;

?? mapStudent.insert(map<int,string>::value_type(1,"student_one"));

?? mapStudent.insert(map<int,string>::value_type(2,"student_two"));

3. map<int,string>mapStudent;

mapStudent[1]="student_one";

mapStudent[2]="student_two";

可以用pair來獲得是否插入成功

map<int,string>mapStudent;

pair<map<int,string>::iterator,bool> insert_pair;

insert_pair=mapStudent.insert(pair<int,string>(1,"student_one"));

if(insert_pair.second==true)????

???? cout<<"insert Successfully"<<endl;

怎么知道當(dāng)前已經(jīng)插入了多少數(shù)據(jù)呢,可以用size函數(shù),用法如下:int nSize=mapStudent.size();

清空map中的數(shù)據(jù)可以用clear()函數(shù),判定map中是否有數(shù)據(jù)可以用empty()函數(shù)

要判定一個(gè)數(shù)據(jù)(關(guān)鍵字)是否在map中出現(xiàn)的方法比較多,這里給出三種數(shù)據(jù)查找方法

第一種:用count函數(shù)來判定關(guān)鍵字是否出現(xiàn),其缺點(diǎn)是無法定位數(shù)據(jù)出現(xiàn)位置,由于map的特性,一對(duì)一的映射關(guān)系,就決定了count函數(shù)的返回值只有兩個(gè),要么是0,要么是1,出現(xiàn)的情況,返回1

???????? map<int,string> mapStudent;

???? mapStudent.insert(pair<int,string>(1,"student_one"));

???? mapStudent.insert(pair<int,string>(2,"student_two"));

???? mapStudent.insert(pair<int,string>(3,"student_three"));

???? int t1,t2;

???? t1=mapStudent.count(4);

???? t2=mapStudent.count(1);

第二種:用find函數(shù)來定位數(shù)據(jù)出現(xiàn)位置,它返回的一個(gè)迭代器,當(dāng)數(shù)據(jù)出現(xiàn)時(shí),它返回?cái)?shù)據(jù)所在位置的迭代器

???????? map<string,int>mapStudent;

???????? mapStudent.insert(pair<string,int>("student_one",1));

???????? mapStudent.insert(pair<string,int>("student_two",2));

???????? mapStudent.insert(pair<string,int>("student_three",3));

???????? map<string,int>::iteratoriter;

???????? charch[]="student_three";

???????? iter=mapStudent.find(ch);

???????? if(iter!=mapStudent.end())

?????????????????? cout<<"Find,the value is: "<<iter->second<<endl;

???????? else

?????????????????? cout<<"Donot Find"<<endl;

清空map中的數(shù)據(jù)可以用clear()函數(shù),判定map中是否有數(shù)據(jù)可以用empty()函數(shù),它返回true則說明是空

//如果要?jiǎng)h除,用迭代器刪除

map<int,string>::iterator iter;

iter=mapStudent.find(1);

mapStudent.erase(iter);

//如果要?jiǎng)h除,用關(guān)鍵字刪除

intn=mapStudent.erase(1);//如果刪除了n會(huì)返回,否則返回

//用迭代器,成片的刪除

mapStudent.erase(mapStudent.begin(),mapStudent.end());

//一下代碼把整個(gè)map清空

mapStudent.erase(mapStudent.begin(),mapStudent.end());

排序

一、

#include <map>

#include <string>

using namespace std;

typedef struct tagStudentInfo

{

???????? int????? nID;

???????? string?? strName;

???????? booloperator < (tagStudentInfo const& _A) const

???????? {

?????????????????? //這個(gè)函數(shù)指定排序策略,按nID排序,如果nID相等的話,按strName排序

?????????????????? if(nID< _A.nID)

???????????return true;

?????????????????? if(nID== _A.nID)

???????????return strName.compare(_A.strName) < 0;

?????????????????? returnfalse;

???????? }

}StudentInfo, *PStudentInfo;? //學(xué)生信息

?

?

int main()

{

???//用學(xué)生信息映射分?jǐn)?shù)

???map<StudentInfo, int>mapStudent;

???StudentInfo studentInfo;

?

???studentInfo.nID = 1;

???studentInfo.strName ="student_one";

???mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

?

???studentInfo.nID = 2;

???studentInfo.strName ="student_two";

???mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

}

二、

#include <map>

#include <string>

#include <iostream>

using namespace std;

typedef struct tagStudentInfo

{

???int????? nID;

???string?? strName;

} StudentInfo, *PStudentInfo; //學(xué)生信息

?

struct sort

{

???bool operator() (StudentInfo const &_A, StudentInfo const &_B)const

??? {

???????if(_A.nID < _B.nID)

???????????return true;

???????if(_A.nID == _B.nID)

???????????return _A.strName.compare(_B.strName) < 0;

???????return false;

??? }

};

?

int main()

{

???//用學(xué)生信息映射分?jǐn)?shù)

???map<StudentInfo, int, sort>mapStudent;

???StudentInfo studentInfo;

???studentInfo.nID = 1;

???studentInfo.strName = "student_one";

???mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

???studentInfo.nID = 2;

???studentInfo.strName = "student_two";

???mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

???map<StudentInfo, int>::reverse_iterator iter;

???for(iter=mapStudent.rbegin();iter!= mapStudent.rend();iter++)

?????????????????? cout<<iter->second<<endl;

}

?

多重映射(multimap)??<map>

允許鍵對(duì)有相等的次序的映射?<map>

比如在電話簿中相同的人可以有兩個(gè)以上電話號(hào)碼,文件系統(tǒng)中可以將多個(gè)符號(hào)鏈接映射到相同的物理文件,或DNS服務(wù)器可以將幾個(gè)URLs映射到相同的IP地址。

查找

1. 直接找到每種鍵值的所有元素的第一個(gè)元素的游標(biāo)。

通過函數(shù):lower_bound( const keytype& x ), upper_bound( const keytype&x ) 可以找到比指定鍵值x的小的鍵值的第一個(gè)元素和比指定鍵值x大的鍵值的第一個(gè)元素。返回值為該元素的游標(biāo)。

細(xì)節(jié):當(dāng)?shù)竭_(dá)鍵值x已經(jīng)是最大時(shí),upper_bound返回的是這個(gè)multimap的end游標(biāo)。同理,當(dāng)鍵值x已經(jīng)是最小了,lower_bound返回的是這個(gè)multimap的begin游標(biāo)。

2. 指定某個(gè)鍵值,進(jìn)行遍歷

可以使用上面的lower_bound和upper_bound函數(shù)進(jìn)行游歷,也可以使用函數(shù)equal_range。其返回的是一個(gè)游標(biāo)對(duì)。游標(biāo)對(duì)pair::first是由函數(shù)lower_bound得到的x的前一個(gè)值,游標(biāo)對(duì)pair::second的值是由函數(shù)upper_bound得到的x的后一個(gè)值。

?multimap<int,int>a;

a.insert(pair<int,int>(1,11));

a.insert(pair<int,int>(1,12));

a.insert(pair<int,int>(1,13));

a.insert(pair<int,int>(2,21));

a.insert(pair<int,int>(2,22));

a.insert(pair<int,int>(3,31));

a.insert(pair<int,int>(3,32));

?

multimap<int,int>::iterator p_map;

pair<multimap<int,int>::iterator,multimap<int,int>::iterator> ret;

for(p_map = a.begin() ; p_map != a.end();)

{

???cout<<p_map->first<<" =>";

???ret = a.equal_range(p_map->first);

??? for(p_map= ret.first; p_map != ret.second; ++p_map)

???????cout<<" "<< (*p_map).second;

???cout<<endl;

}

總結(jié)

以上是生活随笔為你收集整理的STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。