《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第16章 標(biāo)準(zhǔn)模板庫_第1頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第16章 標(biāo)準(zhǔn)模板庫_第2頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第16章 標(biāo)準(zhǔn)模板庫_第3頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第16章 標(biāo)準(zhǔn)模板庫_第4頁
《C++程序設(shè)計(jì)及項(xiàng)目實(shí)踐》 課件 第16章 標(biāo)準(zhǔn)模板庫_第5頁
已閱讀5頁,還剩269頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第16章標(biāo)準(zhǔn)模板庫16.1容器16.2算法16.3 本章小結(jié)111容器提供了模板類表示的泛型(虛擬類型)數(shù)據(jù)結(jié)構(gòu),包括vector、list、queue、set、multiset等,每一種容器類都可以傳入一個(gè)具體的數(shù)據(jù)類型,具現(xiàn)(實(shí)例)化成一個(gè)非模板的類16.1容器22STL中的容器按照分類可以組織成如下類:(1)數(shù)組array:固定大小的容器,提供了對元素的快速隨機(jī)訪問。(2)序列容器:包括vector、deque、list,這些容器按線性順序存儲

元素。(3)關(guān)聯(lián)容器:包括set、multiset、map、multimap,這些容器根據(jù)鍵值進(jìn)行組織和訪問元素。(4)容器適配器:包括stack、queue、priority_queue,這些適配器提供了特定的接口,基于序列容器實(shí)現(xiàn)不同的數(shù)據(jù)結(jié)構(gòu)。(5)無序容器:包括unordered_set、unordered_multiset、unordered_map、unordered_multimap,這些容器使用哈希(散列)函數(shù)進(jìn)行組織和訪問元素。(6)字符串:特定版本的容器,用于處理字符序列。33416.1.1數(shù)組arrayarray容器具有如下的一些常用的成員函數(shù),包括元素訪問,迭代器,容量,操作相關(guān)。

元素訪問類相關(guān)函數(shù)。成員函數(shù)功能介紹at(n)返回下標(biāo)n處的元素,若n超出array的范圍,則拋出out_of_range異常operator[]直接根據(jù)下標(biāo)位置返回array中的元素front()返回第一個(gè)元素back()返回最后一個(gè)元素data()返回array中存儲元素的指針,01#include<iostream>02#include<array>03usingnamespacestd;0405intmain()06{07constintN=10;08array<int,N>a{};09for(inti=0;i<N;i++)10{11a[i]=i*i;12}13cout<<"thefrontelement:"<<a.front()<<endl;14cout<<"thebackelement:"<<a.back()<<endl;15int*p=a.data();16for(int*q=p;q<p+N;q++)17{18cout<<*q<<'';19}20cout<<endl;21for(inti=0;i<N;i++)22{23cout<<a.at(i)<<'';24}25cout<<endl;26try27{28cout<<a.at(N);29}30catch(out_of_range&e)31{32cout<<e.what()<<endl;33}34}array元素訪問示例56

迭代器訪問函數(shù)成員函數(shù)功能介紹begin()返回指向容器中第一個(gè)元素的隨機(jī)迭代器end()返回指向容器中最后一個(gè)元素之后一個(gè)位置的隨機(jī)迭代器rbegin()返回指向容器中最后一個(gè)元素的隨機(jī)迭代器,也就是反向的begin()rend()返回指向容器中第一個(gè)元素之前一個(gè)位置的隨機(jī)迭代器,也就是反向的end()cbegin()與begin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素cend()與end()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crbegin()與rbegin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crend()與rend()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素01#include<iostream>02#include<array>03usingnamespacestd;0405intmain()06{07constintN=5;08array<int,N>numbers{3,5,7,9,11};09for(autoit=numbers.begin();it!=numbers.end();it++)10{11*it=*it+1;12}13cout<<"1)";15};14for(autocit=numbers.begin();cit!=numbers.end();cit++)15{16cout<<*cit<<'';17}18cout<<endl;

77array中迭代器使用示例19for(autorit=numbers.rbegin();rit!=numbers.rend();rit++)20{21*rit=*rit**rit;22}23cout<<"2)";24for(autocrit=numbers.crbegin();crit!=numbers.crend();crit++)25{26cout<<*crit<<'';27}28return0;29}

88array中迭代器使用示例(3)容量相關(guān)函數(shù)99

成員函數(shù)功能介紹empty()檢查array是否為空size()返回array中元素個(gè)數(shù)max_size()與size()相同(由于array固定長度)容量相關(guān)函數(shù)示例01#include<iostream>02#include<array>03usingnamespacestd;0405intmain()06{07constintN=5;08array<int,N>numbers1{3,5,7,9,11};09array<int,0>numbers2;10cout<<"1)"<<numbers1.empty()<<'\t'11<<numbers2.empty()<<endl;12cout<<"2)"<<numbers1.size()<<'\t'13<<numbers2.max_size()<<endl;14return0;15}1010(4)操作相關(guān)函數(shù)1111成員函數(shù)功能介紹fill()填充元素swap()交換數(shù)組1212array操作相關(guān)函數(shù)示例01

#include<iostream>02

#include<array>03

usingnamespacestd;04

#defineN505

06

voidshow(array<int,N>arr,intn)07

{08

for(inti=0;i<n;i++)09

{10

cout<<arr.at(i)<<'';11

}12

}13

intmain()14

{15

array<int,N>numbers1{3,5,7,9,11};16

array<int,N>numbers2{2,4,6};17

numbers2.fill(10);18

numbers1.swap(numbers2);19

cout<<"1)";20

show(numbers1,N);21

cout<<endl<<"2)";22

show(numbers2,N);23

return0;24

}1316.1.1數(shù)組arrayvector容器具有如下的一些常用的成員函數(shù),包括元素訪問,迭代器,容量,操作相關(guān)。(1)元素訪問類相關(guān)函數(shù)成員函數(shù)功能介紹at(n)返回下標(biāo)n處的元素,若n超出vector的范圍,則拋出out_of_range異常operator[]直接根據(jù)下標(biāo)位置返回vector中的元素front()返回第一個(gè)元素的引用back()返回最后一個(gè)元素的引用data()返回vector中存儲元素的指針vector元素訪問函數(shù)示例1401#include<iostream>02#include<vector>03usingnamespacestd;0405intmain()06{07vector<int>v1{1,3,5,7,9};08vector<int>v2(5,10);09cout<<"1)";10for(inti=0;i<5;i++)11{12cout<<v1.at(i)<<'';13}14cout<<endl<<"2)";15for(inti=0;i<5;i++)16{17cout<<v2[i]<<'';18}1419v1.front()=10;20v1.back()=100;21cout<<endl<<"3)";22for(inti=0;i<5;i++)23{24cout<<v1.at(i)<<'';25}26cout<<endl<<"4)";27int*p=v1.data();28for(inti=0;i<5;i++)29{30cout<<p[i]<<'';31}32return0;33}15152.迭代器相關(guān)函數(shù)成員函數(shù)功能介紹begin()返回指向容器中第一個(gè)元素的隨機(jī)迭代器end()返回指向容器中最后一個(gè)元素之后一個(gè)位置的隨機(jī)迭代器rbegin()返回指向容器中最后一個(gè)元素的隨機(jī)迭代器,也就是反向的begin()rend()返回指向容器中第一個(gè)元素之前一個(gè)位置的隨機(jī)迭代器,也就是反向的end()cbegin()與begin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素cend()與end()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crbegin()與rbegin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crend()與rend()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素vector迭代器相關(guān)函數(shù)使用示例1601#include<iostream>02#include<vector>03usingnamespacestd;0405intmain()06{07vector<int>v{1,3,5,7,9};08for(autoit=v.begin();it!=v.end();it++)09{10*it*=*it;11}12cout<<"1)";13for(autocit=v.cbegin();cit!=v.cend();cit++)14{15cout<<*cit<<'';16}17for(autorit=v.rbegin();rit!=v.rend();rit++)18{1619*rit+=*rit;20}21cout<<"\n2)";22for(autocrit=v.crbegin();crit!=v.crend();crit++)23{24cout<<*crit<<'';25}26return0;27}1717(3)容量相關(guān)函數(shù)成員函數(shù)功能介紹empty()判斷vector是否為空size()獲得vector中元素個(gè)數(shù)max_size()vector可以容納的最大元素個(gè)數(shù)reserve()重新分配并保留當(dāng)前向量值capacity()當(dāng)前階段分配的內(nèi)存可以容納的元素個(gè)數(shù)shrink_to_fit()將當(dāng)前階段分配的未使用的內(nèi)存去除vector容量相關(guān)函數(shù)示例181801#include<iostream>02#include<vector>03usingnamespacestd;0405intmain()06{07vector<int>v1;08vector<int>v2{1,3,5,7,9};09cout<<"1)";10cout<<v1.empty()<<'\t'<<v2.empty()<<endl;11cout<<"2)";12cout<<v1.size()<<'\t'<<v2.size()<<endl;13cout<<"3)";14cout<<v1.max_size()<<'\t'<<v2.max_size()<<endl;15cout<<"4)";191916cout<<v1.capacity()<<'\t'<<v2.capacity()<<endl;17v1.reserve(10);18v2.reserve(20);19cout<<"5)";20cout<<v1.capacity()<<'\t'<<v2.capacity()<<endl;21cout<<"6)";22cout<<v1.size()<<'\t'<<v2.size()<<endl;23v1.shrink_to_fit();24v2.shrink_to_fit();25cout<<"7)";26cout<<v1.capacity()<<'\t'<<v2.capacity()<<endl;27cout<<"8)";28cout<<v1.size()<<'\t'<<v2.size()<<endl;29return0;30}2021(4)修改類函數(shù)。成員函數(shù)功能介紹clear()刪除vector中的所有元素insert()插入元素emplace()插入元素emplace_back()末尾插入元素erase()刪除指定的元素push_back()在末尾增加一個(gè)元素pop_back()去掉末尾的元素resize()調(diào)整vector長度swap()交換vector內(nèi)容vector修改類函數(shù)示例212201

#include<iostream>02

#include<vector>03

usingnamespacestd;04

05

voidshow(intid,vector<int>&v)06

{07

cout<<id<<")";08

for(inti:v)09

{10

cout<<i<<'';11

}12

cout<<endl;13

}15

intmain()16

{17

vector<int>v;18

autoit=v.begin();19

v.insert(it,100);20

show(1,v);21

it=v.begin();22

v.insert(it,2,200);23

show(2,v);24

it=v.begin();25

v.emplace(it,300);26

v.emplace_back(1000);27

show(3,v);222328

v.push_back(400);29

show(4,v);30

v.pop_back();31

show(5,v);32

v.erase(v.begin());33

show(6,v);34

v.resize(2);35

show(7,v);36

vector<int>v2{1,3,5,7,9};37

v.swap(v2);38

show(8,v);39

show(9,v2);40

return0;41

}16.1.3雙端隊(duì)列deque2324deque容器具有如下的一些常用的成員函數(shù),包括元素訪問,迭代器,容量,修改相關(guān)。(1)元素訪問類相關(guān)函數(shù)。成員函數(shù)功能介紹at(n)返回下標(biāo)n處的元素,若n超出deque的范圍,則拋出out_of_range異常operator[]直接根據(jù)下標(biāo)位置返回vector中的元素front()返回第一個(gè)元素的引用back()返回最后一個(gè)元素的引用2424deque元素訪問示例01

#include<iostream>02

#include<deque>03

#include<string>04

usingnamespacestd;05

06

intmain()07

{08

deque<string>data01={"one","two","three","four"};09

deque<string>data02{4,"hello"};10

cout<<"1)"<<data01.at(0)<<endl;11

cout<<"2)"<<data01[3]<<endl;12

data01.front()="head";13

data01.back()="tail";14

cout<<"3)";252515

for(stringstr:data01)16

{17

cout<<str<<'\t';18

}19

cout<<endl;20

cout<<"4)";21

try{22

data01.at(4);23

}24

catch(out_of_range&exec)25

{26

cout<<exec.what()<<endl;27

}28

cout<<"5)";29

for(autostr:data02)30

{31

cout<<str<<'\t';32

}33

return0;34

}262.迭代器相關(guān)函數(shù)成員函數(shù)功能介紹begin()返回指向容器中第一個(gè)元素的隨機(jī)迭代器end()返回指向容器中最后一個(gè)元素之后一個(gè)位置的隨機(jī)迭代器rbegin()返回指向容器中最后一個(gè)元素的隨機(jī)迭代器,也就是反向的begin()rend()返回指向容器中第一個(gè)元素之前一個(gè)位置的隨機(jī)迭代器,也就是反向的end()cbegin()與begin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素cend()與end()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crbegin()與rbegin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crend()與rend()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素272727deque迭代器使用示例01

#include<iostream>02

#include<deque>03

usingnamespacestd;04

05

intmain()06

{07

deque<int>data={1,3,5,7,9};08

for(autoit=data.begin();it!=data.end();it++)09

{10

*it=*it+*it;11

}12

cout<<"1)";13

for(autocit=data.cbegin();cit!=data.cend();cit++)14

{15

cout<<*cit<<'\t';16

}2828deque迭代器使用示例17

for(autorit=data.rbegin();rit!=data.rend();rit++)18

{19

*rit=*rit**rit;20

}21

cout<<endl<<"2)";22

for(autocrit=data.crbegin();crit!=data.crend();crit++)23

{24

cout<<*crit<<'\t';25

}26

return0;27

}2929(3)容量相關(guān)函數(shù)。成員函數(shù)功能介紹empty()判斷deque是否為空size()獲得deque中元素個(gè)數(shù)max_size()deque可以容納的最大元素個(gè)數(shù)shrink_to_fit()將當(dāng)前階段分配的未使用的內(nèi)存去除3031deque容量函數(shù)使用示例01

#include<iostream>02

#include<deque>03

usingnamespacestd;04

05

intmain()06

{07

deque<int>data01;08

deque<int>data02{100,200,300,400};09

cout<<"1)"<<boolalpha<<data01.empty()<<endl;10

cout<<"2)"<<data02.empty()<<endl;11

cout<<"3)"<<data01.size()<<endl;12

cout<<"4)"<<data02.size()<<endl;13

cout<<"5)"<<data01.max_size()<<endl;14

return0;15

}3132(4)修改類函數(shù)成員函數(shù)功能介紹clear()刪除deque中的所有元素insert()插入元素emplace()插入元素emplace_back()末尾插入元素emplace_front()頭部插入元素erase()刪除指定的元素push_back()在末尾增加一個(gè)元素pop_back()去掉末尾的一個(gè)元素push_front()在頭部增加一個(gè)元素pop_front()去掉頭部的一個(gè)元素resize()調(diào)整deque長度swap()交換deque內(nèi)容3233deque修改類函數(shù)使用示例01

#include<iostream>02

#include<deque>03

#include<string>04

usingnamespacestd;05

06

classStudent07

{08

private:09

intnum;10

stringname;11

public:12

Student(intnum,stringname)13

{14

this->num=num;15

this->name=name;16

cout<<"constructor:"<<num<<","<<name<<endl;17

}18

friendostream&operator<<(ostream&os,Studentstu);19

};333421

ostream&operator<<(ostream&os,Studentstu)22

{23

os<<stu.num<<','<<;24

returnos;25

}26

27

voidshow(intid,deque<Student>&data)28

{29

cout<<id<<")";30

for(Studentstu:data)31

{32

cout<<stu<<'|';33

}34

cout<<endl;35

}343537

intmain()38

{39

deque<Student>data;40

Studentone{100,"zhang"};41

Studenttwo{200,"li"};42

data.emplace(data.begin(),300,"wang");43

data.emplace_back(300,"wang");44

data.emplace_front(one);45

show(1,data);46

data.insert(data.begin(),one);47

show(2,data);48

data.erase(data.begin());49

show(3,data);50

data.push_front(one);51

data.push_back(two);52

show(4,data);53

data.pop_front();54

show(5,data);55

data.clear();56

show(6,data);57

return0;58

}353616.1.4

雙向鏈表listlist容器具有如下的一些常用的成員函數(shù),包括元素訪問,迭代器,容量,修改相關(guān)。(1)元素訪問類相關(guān)函數(shù)成員函數(shù)功能介紹front()返回第一個(gè)元素的引用back()返回最后一個(gè)元素的引用363701

#include<iostream>02

#include<list>03

usingnamespacestd;04

05

voidshow(intid,list<int>&data)06

{07

cout<<id<<")";08

for(inti:data)09

{10

cout<<i<<'\t';11

}12

cout<<endl;13

}14

15

intmain()16

{17

list<int>data01{2,4,6,8,10};18

list<int>data02=data01;19

list<int>data03(data02);20

list<int>data04(5,200);21

data03.front()=100;22

data03.back()=500;23

show(1,data01);24

show(2,data02);25

show(3,data03);26

show(4,data04);27

return0;28

}3738(2)迭代器相關(guān)函數(shù)成員函數(shù)功能介紹begin()返回指向容器中第一個(gè)元素的隨機(jī)迭代器end()返回指向容器中最后一個(gè)元素之后一個(gè)位置的隨機(jī)迭代器rbegin()返回指向容器中最后一個(gè)元素的隨機(jī)迭代器,也就是反向的begin()rend()返回指向容器中第一個(gè)元素之前一個(gè)位置的隨機(jī)迭代器,也就是反向的end()cbegin()與begin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素cend()與end()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crbegin()與rbegin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素crend()與rend()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素383901

#include<iostream>02

#include<list>03

usingnamespacestd;04

05

intmain()06

{07

list<int>data{2,4,6,8,10};08

for(autoit=data.begin();it!=data.end();it++)09

{10

*it+=100;11

}12

cout<<"1)";13

for(autocit=data.cbegin();cit!=data.cend();cit++)14

{15

cout<<*cit<<'\t';16

}list迭代器使用示例394017

for(autorit=data.rbegin();rit!=data.rend();rit++)18

{19

*rit+=1000;20

}21

cout<<"\n2)";22

for(autocrit=data.crbegin();crit!=data.crend();crit++)23

{24

cout<<*crit<<'\t';25

}26

return0;27

}4041(3)容量相關(guān)函數(shù)成員函數(shù)功能介紹empty()判斷l(xiāng)ist是否為空size()獲得list中元素個(gè)數(shù)max_size()list可以容納的最大元素個(gè)數(shù)414201

#include<iostream>02

#include<list>03

usingnamespacestd;04

05

intmain()06

{07

list<int>data01;08

list<int>data02{2,4,6,8,10};09

cout<<"1)"<<boolalpha<<data01.empty()<<'\t'<<data02.empty()<<endl;10

cout<<"2)"<<data01.size()<<'\t'<<data02.size()<<endl;11

cout<<"3)"<<data01.max_size()<<'\t'<<data02.max_size()<<endl;12

return0;13

}4243(4)修改類函數(shù)成員函數(shù)功能介紹clear()刪除list中的所有元素insert()插入元素emplace()插入元素emplace_back()末尾插入元素emplace_front()頭部插入元素erase()刪除指定的元素push_back()在末尾增加一個(gè)元素pop_back()去掉末尾的一個(gè)元素push_front()在頭部增加一個(gè)元素pop_front()去掉頭部的一個(gè)元素resize()調(diào)整list長度swap()交換list內(nèi)容434401

#include<iostream>02

#include<list>03

#include<string>04

usingnamespacestd;05

06

classStudent07

{08

private:09

intnum;10

stringname;11

public:12

Student(intnum,stringname)13

{14

this->num=num;15

this->name=name;16

}17

friendostream&operator<<(ostream&os,Studentstu);18

};444520

ostream&operator<<(ostream&os,Studentstu)21

{22

os<<stu.num<<','<<;23

returnos;24

}25

26

voidshow(intid,list<Student>&stuList)27

{28

cout<<id<<")";29

for(Studentstu:stuList)30

{31

cout<<stu<<'|';32

}33

cout<<endl;34

}454637

intmain()38

{39

list<Student>stuList;40

Studentone{100,"zhang"};41

stuList.insert(stuList.end(),one);42

stuList.emplace(stuList.end(),200,"li");43

stuList.emplace_front(300,"wang");44

stuList.emplace_back(400,"zhou");45

show(1,stuList);46

//前后部各增加了一個(gè)對象47

Studenttwo{500,"chu"};48

Studentthree{600,"cheng"};49

stuList.push_back(two);50

stuList.push_front(three);51

show(2,stuList);52

//前部彈出兩個(gè)對象,后部彈出一個(gè)對象53

stuList.pop_back();54

stuList.pop_front();55

stuList.pop_front();56

show(3,stuList);57

return0;58

}4647(4)操作類函數(shù)成員函數(shù)功能介紹merge()合并兩個(gè)listsplice()將另一個(gè)list中的元素移動到當(dāng)前l(fā)istremove()刪除指定值的元素remove_if()刪除滿足某個(gè)標(biāo)準(zhǔn)的元素reverse()反向一個(gè)listunique()只保留list中的唯一值(去重)sort()對list進(jìn)行排序4748list操作類函數(shù)使用示例01

#include<iostream>02

#include<list>03

usingnamespacestd;04

05

voidshow(intid,stringstrList,list<int>&iList)06

{07

cout<<id<<")"<<strList<<":";08

for(inti:iList)09

{10

cout<<i<<'';11

}12

cout<<endl;13

}484915

intmain()16

{17

list<int>iList01={9,8,6,7,10,16};18

list<int>iList02={20,19,20,19,9,5,4,3,2,1};19

show(1,"iList01",iList01);20

//對iList01從小到大排序21

iList01.sort();22

show(2,"iList01",iList01);23

//反轉(zhuǎn)iList0224

iList02.reverse();25

show(3,"iList02",iList02);26

//合并兩個(gè)列表,首先需要排好序27

iList02.sort();28

iList01.merge(iList02);29

show(4,"iList01",iList01);30

show(5,"iList02",iList02);31

//粘接兩個(gè)列表32

list<int>iList03{100,200,300,400};33

autoit=iList01.begin();34

advance(it,2);//迭代器位置正向移動2個(gè)位置35

iList01.splice(it,iList03);36

show(6,"iList01",iList01);37

show(7,"iList03",iList03);38

cout<<"8)"<<*it<<endl;39

//刪除指定值的元素40

iList01.remove(100);41

show(9,"iList01",iList01);42

//刪除滿足條件的元素43

iList01.remove_if([](intn){returnn>100;});44

show(10,"iList01",iList01);45

//保留不重復(fù)的元素495046

iList01.unique();47

show(11,"iList01",iList01);48

//從大到小排序49

iList01.sort(greater<int>());50

show(12,"iList01",iList01);51

//從小到大排序52

iList01.sort();53

show(13,"iList01",iList01);54

return0;55

}505116.1.5

單向鏈表forward_listforward_list容器具有如下的一些常用的成員函數(shù),包括元素訪問,迭代器,容量,修改相關(guān)。(1)元素訪問類相關(guān)函數(shù)成員函數(shù)功能介紹front()返回第一個(gè)元素的引用5152forward_list元素訪問函數(shù)使用示例05

voidshow(intid,forward_list<int>&iList)06

{07

cout<<id<<"):";08

for(inti:iList)09

{10

cout<<i<<'';11

}12

cout<<endl;13

}14

15

intmain()16

{17

forward_list<int>iList01={9,8,6,7,10,16};18

show(1,iList01);19

iList01.front()=100;20

show(2,iList01);21

return0;22

}5253(2)迭代器相關(guān)函數(shù)

成員函數(shù)功能介紹before_begin()返回指向容器中第一個(gè)元素前面的一個(gè)位置cbefore_begin()與before_begin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素begin()返回指向容器中第一個(gè)元素的隨機(jī)迭代器end()返回指向容器中最后一個(gè)元素之后一個(gè)位置的隨機(jī)迭代器cbegin()與begin()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素cend()與end()類似,但是前面加上了c(onst)常量限制,不能利用迭代器修改容器中元素535401

#include<iostream>02

#include<forward_list>03

usingnamespacestd;04

05

voidshow(intid,forward_list<int>&iList)06

{07

cout<<id<<"):";08

for(inti:iList)09

{10

cout<<i<<'';11

}12

cout<<endl;13

}forward_list迭代器使用示例545515

intmain()16

{17

forward_list<int>iList01={9,8,6,7,10,16};18

show(1,iList01);19

autobb=iList01.before_begin();20

iList01.insert_after(bb,100);21

show(2,iList01);22

for(autoit=iList01.begin();it!=iList01.end();it++)23

{24

*it=*it**it;25

}26

cout<<"3)";27

for(autocit=iList01.cbegin();cit!=iList01.cend();cit++)28

{29

cout<<*cit<<'';30

}31

return0;32

}5556(3)容量相關(guān)函數(shù)成員函數(shù)功能介紹empty()判斷l(xiāng)ist是否為空max_size()list可以容納的最大元素個(gè)數(shù)565701

#include<iostream>02

#include<forward_list>03

usingnamespacestd;04

05

intmain()06

{07

forward_list<int>iList01;08

forward_list<int>iList02={9,8,6,7,10,16};09

cout<<"1)"<<boolalpha<<iList01.empty()<<'\t'<<iList02.empty()<<endl;10

cout<<"2)"<<iList01.max_size()<<'\t'<<iList02.max_size()<<endl;11

return0;12

}forward_list容量函數(shù)使用示例5758(4)修改類函數(shù)成員函數(shù)功能介紹clear()刪除forward_list中的所有元素insert_after()插入一個(gè)或多個(gè)元素emplace_after()插入一個(gè)或多個(gè)元素erase_after()刪除一個(gè)或多個(gè)元素emplace_front()頭部插入元素push_front()在頭部增加一個(gè)元素pop_front()去掉頭部的一個(gè)元素resize()調(diào)整forward_list長度swap()交換forward_list內(nèi)容5859forward_list修改類函數(shù)使用示例06

classStudent07

{08

private:09

intnum;10

stringname;11

public:12

Student(intnum,stringname)13

{14

this->num=num;15

this->name=name;16

}17

friendostream&operator<<(ostream&os,Studentstu);18

};19

20

ostream&operator<<(ostream&os,Studentstu)21

{22

os<<stu.num<<','<<;23

returnos;24

}26

voidshow(intid,forward_list<Student>&stuList)27

{28

cout<<id<<")";29

for(Studentstu:stuList)30

{31

cout<<stu<<'|';32

}33

cout<<endl;34

}596036

intmain()37

{38

forward_list<Student>stuList;39

Studentone{100,"zhang"};40

stuList.emplace_front(300,"wang");41

autoit=stuList.before_begin();42

stuList.insert_after(it,one);43

stuList.emplace_after(stuList.begin(),200,"li");44

show(1,stuList);45

//前增加了一個(gè)對象46

Studenttwo{500,"chu"};47

stuList.push_front(two);48

show(2,stuList);49

//前部彈出對象50

stuList.pop_front();51

show(3,stuList);52

//刪除第二個(gè)對象53

stuList.erase_after(stuList.cbegin());54

show(4,stuList);55

//清空56

stuList.clear();57

show(5,stuList);58

return0;59

}6061(4)操作類函數(shù)成員函數(shù)功能介紹merge()合并兩個(gè)forward_listsplice_after()將另一個(gè)forward_list中的元素移動到當(dāng)前forward_listremove()刪除指定值的元素remove_if()刪除滿足某個(gè)標(biāo)準(zhǔn)的元素reverse()反向一個(gè)forward_listunique()只保留forward_list中的唯一值(去重)sort()對forward_list元素進(jìn)行排序6162forward_list操作類函數(shù)使用示例05

voidshow(intid,stringstrList,forward_list<int>&iList)06

{07

cout<<id<<")"<<strList<<":";08

for(inti:iList)09

{10

cout<<i<<'';11

}12

cout<<endl;13

}15

intmain()16

{17

forward_list<int>iList01={9,8,6,7,10,16};18

forward_list<int>iList02={20,19,20,19,9,5,4,3,2,1};19

show(1,"iList01",iList01);20

//對iList01從小到大排序21

iList01.sort();22

show(2,"iList01",iList01);23

//反轉(zhuǎn)iList0224

iList02.reverse();626325

show(3,"iList02",iList02);26

//合并兩個(gè)列表,首先需要排好序27

iList02.sort();28

iList01.merge(iList02);29

show(4,"iList01",iList01);30

show(5,"iList02",iList02);31

//粘接兩個(gè)列表32

forward_list<int>iList03{100,200,300,400};33

autoit=iList01.begin();34

advance(it,2);35

iList01.splice_after(it,iList03);36

show(6,"iList01",iList01);37

show(7,"iList03",iList03);38

cout<<"8)"<<*it<<endl;636439

//刪除指定值的元素40

iList01.remove(100);41

show(9,"iList01",iList01);42

//刪除滿足條件的元素43

iList01.remove_if([](intn){returnn>100;});44

show(10,"iList01",iList01);45

//保留不重復(fù)的元素46

iList01.unique();47

show(11,"iList01",iList01);48

//從大到小排序49

iList01.sort(greater<int>());50

show(12,"iList01",iList01);51

//從小到大排序52

iList01.sort();53

show(13,"iList01",iList01);54

return0;55

}646516.1.6

集合Setset容器具有如下的一些常用的成員函數(shù),包括迭代器,容量,修改,查找,觀察者相關(guān)。(1)迭代器相關(guān)函數(shù)成員函數(shù)功能介紹begin()返回指向容器中第一個(gè)元素的隨機(jī)迭代器,為常量迭代器,不能修改。end()返回指向容器中最后一個(gè)元素之后一個(gè)位置的隨機(jī)迭代器,為常量迭代器,不能修改。rbegin()返回指向容器中最后一個(gè)元素的隨機(jī)迭代器,也就是反向的begin(),為常量迭代器,不能修改。rend()返回指向容器中第一個(gè)元素之前一個(gè)位置的隨機(jī)迭代器,也就是反向的end(),為常量迭代器,不能修改。cbegin()同begin()cend()同end()crbegin()同rbegin()crend()同rend()656601

#include<iostream>02

#include<set>03

usingnamespacestd;04

05

intmain()06

{07

set<int>iset={9,8,6,7,10,16,9,8,7};08

cout<<"1)";09

for(autocit=iset.cbegin();cit!=iset.cend();cit++)10

{11

cout<<*cit<<'';12

}13

cout<<"\n2)";14

for(autorit=iset.rbegin();rit!=iset.rend();rit++)15

{16

cout<<*rit<<'';17

}18

return0;19

20

}set迭代器函數(shù)使用示例6667(2)容量相關(guān)函數(shù)成員函數(shù)功能介紹empty()判斷set是否為空size()獲得set中元素個(gè)數(shù)max_size()set可以容納的最大元素個(gè)數(shù)676801

#include<iostream>02

#include<set>03

usingnamespacestd;04

05

intmain()06

{07

set<int>iset={9,8,6,7,10,16,9,8,7};08

cout<<"1)"<<boolalpha<<iset.empty()<<endl;09

cout<<"2)"<<iset.size()<<endl;10

cout<<"3)"<<iset.max_size()<<endl;11

return0;12

}set容量相關(guān)函數(shù)使用示例6869(3)修改類函數(shù)成員函數(shù)功能介紹clear()刪除set中的所有元素insert()插入一個(gè)元素emplace()插入一個(gè)元素emplace_hint()根據(jù)某個(gè)迭代器插入元素erase()刪除一個(gè)或多個(gè)元素swap()交換set內(nèi)容697001

#include<iostream>02

#include<set>03

usingnamespacestd;04

05

voidshow(intid,set<int>&iset)06

{07

cout<<id<<")";08

for(inti:iset)09

{10

cout<<i<<'';11

}12

cout<<endl;13

}15

intmain()16

{17

set<int>iset={9,8,6,7,10,16,9,8,7};18

show(1,iset);19

iset.insert(iset.begin(),20);20

iset.insert(30);21

iset.insert({12,11});22

show(2,iset);23

iset.erase(30);24

iset.erase(iset.begin());25

show(3,iset);26

iset.clear();27

show(4,iset);28

return0;29

}7071(4)查找類函數(shù)成員函數(shù)功能介紹count()返回和某個(gè)key相等的元素個(gè)數(shù)find()返回和某個(gè)key相等的元素位置equal_range()返回和某個(gè)key匹配的元素范圍lower_bound()返回大于等于某個(gè)key值的第一個(gè)元素的位置upper_bound()返回大于key值的第一個(gè)元素的位置717206

{07

set<int>iset={9,8,6,7,10,16,9,8,7,13,12};08

cout<<"1)"<<iset.count(100)<<'\t'<<iset.count(10)<<endl;09

cout<<"2)";10

for(autoit=iset.find(9);it!=iset.end();it++)11

{12

cout<<*it<<'';13

}14

cout<<endl;15

autolower=iset.lower_bound(10);16

autoupper=iset.upper_bound(10);17

cout<<"3)"<<*lower<<'\t'<<*upper<<endl;18

auto[lb,ub]=iset.equal_range(10);19

cout<<"4)";20

for(autoit=lb;it!=ub;it++)21

{22

cout<<*it<<'';23

}24

return0;25

}7273(5)觀察者函數(shù)成員函數(shù)功能介紹key_comp()返回和key值比較的函數(shù)對象value_comp()同key_comp()737405

classModCmp06

{07

public:08

booloperator()(constintlhs,constintrhs)const09

{10

return(lhs%10)>=(rhs%10);11

}12

};13

14

voidshow(intid,set<int,ModCmp>&iset)15

{16

cout<<id<<")";17

for(autoit=iset.begin();it!=iset.end();it++)18

{19

cout<<*it<<'';20

}21

cout<<endl;22

}747524

intmain()25

{26

set<int,ModCmp>iset={19,18,17,9,8,7};27

show(1,iset);28

cout<<"2)keycompareresult:"<<endl;29

autocomp_func=iset.key_c

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論