日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++STL总结笔记(一)—— 容器和容器适配器

發(fā)布時間:2023/12/10 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++STL总结笔记(一)—— 容器和容器适配器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 一、概念
    • 1.1 順序容器
    • 1.2 容器適配器
    • 1.3 關聯(lián)容器
  • 二、程序示例
    • 1. vector和Set自定義數(shù)據(jù)類型的訪問
    • 2.vector容器嵌套
    • 3.list容器排序
    • 4.pair對組的使用
  • 總結


前言

STL是C++中的基于數(shù)據(jù)結構和算法的標準模板庫,可以大量節(jié)約系統(tǒng)開發(fā)時間,增加程序復用性。
STL的六大件包括容器、算法、迭代器、仿函數(shù)、適配器和空間配置器,其中幾乎所有代碼均使用了模板類和模板函數(shù)的概念。

一、概念

容器從字面意思理解就是放東西的器件,C++中的容器是指將對象放入存儲對象的模板類型里。
容器有以下幾種:

1.1 順序容器

順序容器也叫序列式容器,是各元素之間存在順序關系的線性表,其中元素的位置固定,但可用刪除或插入的操作進行改變,這種操作稱為質變算法。

順序容器由vector(向量)、list(列表)、deque(隊列)組成。

vector是最常見的容器類型,訪問其中的數(shù)據(jù)有很多方法,這里使用迭代器完成,一般使用iterator迭代器,它是指針的泛化,聲明的對象可以用*+對象完成查看。與普通數(shù)組的不同之處在于vector是可以動態(tài)擴展的數(shù)據(jù)結構,沒有長度的限制,其原理是重新開辟新空間來拷貝原來的數(shù)組內(nèi)容,然后刪除原有空間。
其缺陷在于對頭部進行插入或刪除時效率較低。

//頭文件 #include<vector>//定義容器 vector<int> v; //插入數(shù)據(jù) v.push_back(1); v.push_back(3); v.push_back(5); //訪問數(shù)據(jù)1 for (vector<int>::iterator i = v.begin(); i != v.end(); i++) {cout << *i << endl; } //[]和at訪問 cout << v[0] << endl; cout << v.at(0) << endl; //拷貝構造完成賦值 vector<int>v2(v); v2.resize(3); v2.insert(v2.begin(),100); v2.erase(v2.begin());

deque(double-ended queue:雙端隊列)是一種對兩端元素進行添加和刪除操作的容器,又稱雙端數(shù)組,應用方式基本和vector一樣。
其特點:
1: 采用多個連續(xù)的內(nèi)存存儲數(shù)據(jù),并且在一個映射結構中保存對這些內(nèi)存和順序的跟蹤,索引數(shù)組是存儲每段數(shù)組的首地址。
2:和vevtor不同,deque向兩端插入或刪除元素時效率較高,但中間效率較低。
3:deque在訪問元素時比vector慢。
4:deque的迭代器也支持隨機訪問數(shù)組元素。
工作原理:
deque是由一個中控器來維護緩沖區(qū)的地址,其中緩沖區(qū)存放數(shù)據(jù)。

deque<int>d;for (int i = 0; i < 10; i++){d.push_back(i);}PrintDeque(d);deque<int>d1(d.begin(), d.end());PrintDeque(d1);//賦值deque<int>d2;d2 = d1;PrintDeque(d2);deque<int>d3;//尾插d3.push_back(1);//頭插d3.push_front(2);PrintDeque(d3);d3.insert(d3.begin(), 3);PrintDeque(d3);deque<int>::iterator i = d3.begin();d3.erase(i);//默認從小到大sort(d3.begin(), d3.end());PrintDeque(d3);d3.clear();PrintDeque(d3);

list叫雙向鏈表,其數(shù)據(jù)元素是通過鏈表指針串連成的線性表,由節(jié)點表示元素位置,節(jié)點由三部分組成,前驅元素指針域、數(shù)據(jù)域和后繼元素指針域。前驅元素指針域保存了前驅元素的首地址;數(shù)據(jù)域則是本節(jié)點的數(shù)據(jù);后繼元素指針域則保存了后繼元素的首地址。迭代器也是雙向迭代器。
缺點:
1:由于list元素節(jié)點并不要求在一段連續(xù)的內(nèi)存中,故不支持快速隨機存取,只能通過“++”或“–”操作將迭代器移動到后繼/前驅節(jié)點元素處。
2:遍歷速度慢,是用指針進行讀取的。
3:占用內(nèi)存大。
優(yōu)點:
1:可以快速的插入和刪除數(shù)據(jù)。原因是list是通過指針指向來串聯(lián)的,插入元素的時候只需要讓前后的指針指向新的元素節(jié)點即可,不需移動元素位置。
2:采用動態(tài)分配內(nèi)存,不會浪費。

//構造函數(shù)list<int>l;l.push_back(1);l.push_back(2);l.push_back(3); list<int>l2(l.begin(),l.end());PrintList(l2);list<int>l3(l2);PrintList(l3);list<int>l4(3,10);PrintList(l4);//賦值list<int>l5;l5 = l4;list<int>l6;l6.assign(l5.begin(), l5.end());list<int>l7;l7.assign(3, 10);PrintList(l7);//交換l.swap(l7);PrintList(l);PrintList(l7);//存取cout<<l.front()<<endl;cout << l.back() << endl;//list不支持隨機訪問list<int>::iterator i = l.begin();i++;//支持++,--訪問元素i--;//反轉l.swap(l7);l.reverse();PrintList(l);

1.2 容器適配器

容器適配器包括stack,queue,priority_queue三種,可以讓基本的容器類型采用另一種更適配于當前工作要求的工作方式實現(xiàn)。三種適配器都需滿足一定的約束條件,也可以可理解為加了限制條件的容器。

stack稱為棧容器,是以deque為底層容器,封閉一些功能而形成一種具有“先進后出”特性,并不允許遍歷行為的容器適配器,所以stack沒有迭代器。

#include<stack>stack<int>s;//從棧頂入棧s.push(1);s.push(2);//判斷棧頂是否為空,出棧while (!s.empty()){cout << s.top() << endl;s.pop();}cout << s.size()<<endl;

queue又稱隊列,必須符合先進先出原則,也不允許遍歷成員,所以也沒有迭代器。

#include<iostream> #include<string> #include<queue> #include<algorithm> using namespace std;void test() {queue<int>s;//從隊頂入隊s.push(1);s.push(2);//判斷隊頭是否為空,出隊while (!s.empty()){cout << s.front()<< endl;cout << s.back()<<endl;s.pop();}cout << s.size() << endl; }int main() {test();system("pause"); }

priority_queue稱為優(yōu)先隊列,自定義數(shù)據(jù)的優(yōu)先級, 讓優(yōu)先級高的排在隊列前面, 可以優(yōu)先出隊。

#include <queue>priority_queue<int> a;for (int i = 0; i < 10; i++){a.push(i);}while (!a.empty()){cout << a.top() << ' ';a.pop();}cout << endl;

1.3 關聯(lián)容器

關聯(lián)容器是一種二叉樹的結構,沒有嚴格的順序關系,元素位置會按照二叉樹的排序方式進行,在排序的時候按照升序排列。
主要包含有三種,set(集合),multiset(多重集合),map,multimap。

set和multiset均可以進行快速查找數(shù)據(jù),但區(qū)別在于set容器不允許有重復值,multiset可以有。
set在內(nèi)存中是通過鏈表進行排序的,所以插入時比vector和deque要快,但是比list慢;在訪問元素上比vector 慢,比list快,原因是list 是逐個搜索,它搜索的時間是跟容器的大小成正比,而關聯(lián)容器 查找的復雜度是Log(N) ,比vector少。

void printSet(set<int>&s) {for (set<int>::iterator i = s.begin(); i != s.end(); i++){cout << *i << " ";}cout << endl; } //降序.聲明仿函數(shù) class Comparedown { public:bool operator()(int a, int b)const{return a > b;} };int main() {set<int>s;//只能使用insert方法插入數(shù)據(jù)s.insert(1);s.insert(2);s.insert(3);//遍歷容器printSet(s);//拷貝構造set<int>s2(s);printSet(s2);//賦值set<int>s3;s3 = s2;printSet(s3);//插入s.insert(4);s.erase(s.begin());s.erase(2);printSet(s);//互換s.swap(s2);//查找set<int>::iterator i = s.find(3);if (i!= s.end()){cout << *i << endl;}else{cout << "null" << endl;}//統(tǒng)計int n = s.count(3);cout << n << endl;//排序set<int, Comparedown>s5;s5.insert(3);s5.insert(2);for (set<int, Comparedown>::iterator i = s5.begin(); i != s5.end(); i++){cout << *i << " ";}//多重集合multiset<int>ms;ms.insert(1);ms.insert(1);for (multiset<int>::iterator i = ms.begin(); i != ms.end(); i++){cout << *i << " ";} }

map 提供一種“鍵-值”關系的一對一的數(shù)據(jù)存儲能力,所有元素都是pair類型。
pair的第一個元素是鍵值,第二個為value值。
鍵值即索引值,在容器中不可重復,其內(nèi)部按鏈表的方式存儲,故也繼承了鏈表的優(yōu)缺點,在插入時所有元素都會根據(jù)鍵值自動排序。
與set相似,multimap可以實現(xiàn)重復數(shù)據(jù)的存儲。

void printMap(map<int,int>&m) {for (map<int,int>::iterator i = m.begin(); i != m.end(); i++){cout <<"key=" << (*i).first <<" " <<"value=" << i->second<<endl;}cout << endl; }class Comparedown { public:bool operator()(int a, int b)const{return a > b;} };void test() {map<int, int>m;//插入數(shù)據(jù)m.insert(pair<int, int>(1, 2));m.insert(pair<int, int>(3, 4));m.insert(make_pair(2, 6));m.insert(make_pair(5, 34));m.insert(map<int, int>::value_type(7, 8));printMap(m);//拷貝構造map<int, int>map1(m);//賦值map<int, int>map2;map2 = m;//按key進行刪除m.erase(1);printMap(m);//按key查找map<int, int>::iterator i = m.find(3);if (i != m.end()){cout << i->first << i->second << endl;}else{cout << "未找到" << endl;}//統(tǒng)計key,但key不重復,所以n值為0或1int n = m.count(3);cout << n << endl;//用仿函數(shù)完成從大到小排序map<int, int,Comparedown>map4;map4.insert(make_pair(2, 6));map4.insert(make_pair(6, 6));map4.insert(make_pair(3, 6));for (map<int, int, Comparedown>::iterator i = map4.begin(); i != map4.end(); i++){cout << "key=" << (*i).first << " " << "value=" << i->second << endl;} }int main() {test();system("pause"); }

二、程序示例

1. vector和Set自定義數(shù)據(jù)類型的訪問

class Person { public:Person(string name, int age){this->Name = name;this->Age = age;}string Name;int Age; }; //存數(shù)據(jù) void test() {//vectorvector<Person> v;Person p1("sun", 4);Person p2("aun", 3);Person p3("cun", 2);Person p4("dun", 1);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);for (vector<Person>::iterator i = v.begin(); i != v.end(); i++){cout << (*i).Name <<" " <<(*i).Age<<endl;cout << i->Name << " " << i->Age << endl;}//setset<Person> s;Person p1("sun", 4);Person p2("aun", 3);Person p3("cun", 2);Person p4("dun", 1); }//存指針 void test1() {vector<Person*> v;Person p1("sun", 4);Person p2("aun", 3);Person p3("cun", 2);Person p4("dun", 1);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);v.push_back(&p4);for (vector<Person*>::iterator i = v.begin(); i != v.end(); i++){cout << (*(*i)).Name <<" " << (*(*i)).Age<< endl;cout << (*i)->Name << " " << (*i)->Age << endl;} }

2.vector容器嵌套

void test() {vector<vector<int>>doublev;//創(chuàng)建內(nèi)層容器vector<int>doublev1;//放數(shù)據(jù)for (int i = 0; i < 3; i++){doublev1.push_back(i + 1);}//放入外層容器doublev.push_back(doublev1);//遍歷數(shù)據(jù)for (vector<vector<int>>::iterator i = doublev.begin(); i != doublev.end(); i++){for (vector<int>::iterator j = (*i).begin(); j != (*i).end(); j++){cout << *j << endl;cout << &j << endl;//內(nèi)層嵌套的迭代器指針地址}cout << &i << endl;//外層嵌套的迭代器指針地址} }

3.list容器排序

class Cat { public:Cat(string name, int color,int age){this->Name = name;this->Color = color;this->Age = age;}public:string Name;int Color;int Age; };//指定排序規(guī)則 bool Compare(Cat& c1, Cat& c2) {if (c1.Color == c2.Color){return c1.Age < c2.Age;}else{return c1.Color < c2.Color;} }void test() {list<Cat>L;Cat cat1("小100", 76, 3);Cat cat2("小200", 32, 2);Cat cat3("小300", 32, 4);Cat cat4("小400", 32, 3);Cat cat5("小500", 54, 1);//插入L.push_back(cat1);L.push_back(cat2);L.push_back(cat3);L.push_back(cat4);L.push_back(cat5);for (list<Cat>::iterator i = L.begin(); i != L.end(); i++){cout << (*i).Name << " " << (*i).Color << " " << (*i).Age << endl;}L.sort(Compare);for (list<Cat>::iterator i = L.begin(); i != L.end(); i++){cout << (*i).Name << " " << (*i).Color << " " << (*i).Age << endl;} }int main() {test();system("pause"); }

4.pair對組的使用

pair是一個模板結構體類型,set集合的insert方法就是pair類模板定義的,pair的參數(shù)類型有兩種,一個是迭代器,另外一個是bool類。bool返回迭代器是否應用方法成功,pair里的對象可以由pair的兩個函數(shù)first和second訪問。

multiset只返回迭代器類型,故可以重復插入。

//set pair<iterator, bool> insert(value_type&& _Val) {const auto _Result = _Emplace(_STD move(_Val));return {iterator(_Result.first, _Get_scary()), _Result.second}; } //pair定義 struct pair { // store a pair of values using first_type = _Ty1; using second_type = _Ty2;//multiset iterator insert(value_type&& _Val) {return iterator(_Emplace(_STD move(_Val)).first, _Get_scary()); } // pair<set<int>::iterator, bool> p = s.insert(4); if (p.second)//first返回迭代器,second返回bool {cout << "插入成功" << endl; }

pair既可以將2個數(shù)據(jù)組合成一組數(shù)據(jù),也可以讓函數(shù)返回2個數(shù)據(jù),具體應用如下:

pair<string, int> p1(string("a"), 3);cout << p1.first << p1.second << endl;pair<string, int> p2 = make_pair(string("a"), 3);cout << p2.first << p2.second << endl;

總結

容器的構造函數(shù)和成員函數(shù)的調用規(guī)則基本是類似的,但每種都會存在一點差別,重點記憶vector, list,map三種的調用方式。

總結

以上是生活随笔為你收集整理的C++STL总结笔记(一)—— 容器和容器适配器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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