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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数

發布時間:2024/9/27 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


1.STL(Standard Template Library,是用泛型技術來設計完成的實例)的概念與組成

Iterator(迭代器)

Container(容器)

Algorithm(算法)

Adaptors(配接器)

?

STL的六大組件分別是:

容器(Container

算法(Algorithm

迭代器(Iterator

仿函數(Function object

適配器(Adapter

空間配置器(allocator:只能分配內存等

?

2.容器與算法

案例如下:

#include<iostream>

#include<vector>//容器

#include<array>//數組

#include<algorithm>

?

usingnamespacestd;

?

//實現一個模板類,專門實現打印的功能

template<classT>?//類模板實現了方法

classmyvectorprint

{

public:

???void operator ()(constT &t)//重載,使用(),打印

???{

???????std::cout << t <<std::endl;

???}

};

?

voidmain()

{

???vector<int>?myvector;

???myvector.push_back(11);

???myvector.push_back(21);

???myvector.push_back(31);

???myvector.push_back(81);

???myvector.push_back(51);

?

???array<int, 10> myarray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

?

???myvectorprint<int>print;//對于打印進行實例化

?

???//begin,endl迭代器,是一個指針

???for_each(myvector.begin(),myvector.end(),print);

???

???std::cout << "---------------------" <<std::endl;

?

???for_each(myarray.begin(),myarray.end(),print);

???cin.get();

???//算法可以適用于任何一個容器,for_each是一個算法

}

?

3.容器

序列式容器(Sequence containers

每個元素都有固定位置----取決于插入實際和地點,和元素之無關

Vector,deque,list

關聯式容器(Associated containers

元素位置取決于特定的排序準則,和插入順序無關

setmultisetmapmultimap

4.vectors:

將元素置于一個動態數組中加以管理

可以隨機存取元素(用索引直接存取)

數組尾部添加或移除元素非常快速,但是在中部或頭部安插元素比較費時。

5.數組線程容器

#include<iostream>

#include<vector>

#include<array>

#include<tuple>

?

usingnamespacestd;

?

voidmain()

{

???//數組,靜態數組,棧上

???array<int, 5> myarray = { 1, 2, 3, 4, 5 };

???

???//動態數組,堆上

???vector <int>myvector;

???myvector.push_back(1);

?

???//不需要變長,容量較小時,使用array

???//不需要變長,容量較大是,使用vector

}

6.list容器(添加和迭代輸出)

#include<iostream>

#include<hash_set>

#include<list>???//實際上是一個雙向鏈表

#include<stdio.h>

?

//list使用于經常插入,經常刪除

?

usingnamespacestd;

?

voidmain()

{

???list<int>mylist;

?

???mylist.push_back(1);

???mylist.push_back(2);

???mylist.push_back(3);

???mylist.push_back(4);

???mylist.push_front(4);//往頭部插入

?

???//指針,指向一個迭代器,迭代器存儲了位置

???autoibegin =mylist.begin();

???autoiend =mylist.end();

???//list用迭代器進行遍歷

???for (;ibegin !=iend;ibegin++)

???{

???????cout << *ibegin <<endl;

???????printf("%p,&p\n",ibegin._Ptr,ibegin);//重載

???}

???cin.get();

}

運行結果是:

7.list刪除應該注意的地方

#include<iostream>

#include<hash_set>

#include<list>???//實際上是一個雙向鏈表

#include<stdio.h>

?

//list使用于經常插入,經常刪除

?

usingnamespacestd;

?

voidmain()

{

???list<int>mylist;

???mylist.push_back(1);

???mylist.push_back(2);

???mylist.push_back(3);

???mylist.push_back(4);

???mylist.push_back(5);

???//auto i = mylist.begin();刪除元素,依賴于迭代器

???//++i

???//++i

???//++i

???autoi =mylist.end();//end最后一個沒有實體

???i--;

???mylist.erase(i);//鏈式存儲,不允許下標訪問

???//只能用迭代器,鏈表迭代器只能用++--

???//mylist.clear();清空

???//指針,指向一個迭代器,迭代器存儲了位置

???autoibegin =mylist.begin();

???autoiend =mylist.end();

???for (;ibegin !=iend;ibegin++)

???{

???????if ((*ibegin) == 3)

???????{

???????????mylist.erase(ibegin);//刪除,刪除的時候迭代器會發生

???????????break;//這里一定要記住,要使用break;因為list原來的結構已經發生了變化

???????}

???????//cout <<*ibegin << endl;

???}

???{

???????//指針,指向一個迭代器,迭代器存儲了位置

???????autoibegin =mylist.begin();

???????autoiend =mylist.end();

?

???????for (;ibegin !=iend;ibegin++)

???????{

???????????cout << *ibegin <<endl;

???????}

???}

???cin.get();

}

運行結果:

8.通過數組的方式為list初始化

#include<iostream>

#include<hash_set>

#include<list>???//實際上是一個雙向鏈表

#include<stdio.h>

?

//list使用于經常插入,經常刪除

?

usingnamespacestd;

?

voidmain()

{

???inta[5] = { 1, 2, 3, 4, 5 };

???list<int>mylist(a,a + 5);//根據數組初始化

???//傳遞開始地址,傳遞結束地址

???//mylist(0)

???//mylist[1];只能用迭代器訪問

???mylist.push_back(10);

???mylist.push_front(12);//在前添加數值

???//指針,指向一個迭代器,迭代器存儲了位置

???autoibegin =mylist.begin();

???autoiend =mylist.end();

?

???for (;ibegin !=iend;ibegin++)

???{

???????if (*ibegin == 3)

???????{

???????????mylist.insert(ibegin, 30);

???????????break;//刪除或者插入,迭代器都會發生變化

???????}

???}

?

???mylist.remove(30);//直接一個函數,根據元素來刪除

?

???{

???????autoibegin =mylist.begin();//指針,指向一個迭代器,迭代器存儲了位置

???????autoiend =mylist.end();

?

???????for (;ibegin !=iend;ibegin++)

???????{

???????????cout << *ibegin <<endl;

???????}

???}

???cin.get();

}

運行結果:

9.數組初始化,并逆向輸出

#include<iostream>

#include<hash_set>

#include<list>???//實際上是一個雙向鏈表

#include<stdio.h>

?

//list使用于經常插入,經常刪除

?

usingnamespacestd;

?

voidmain()

{

???inta[5] = { 1, 2, 3, 4, 5 };

???list<int>mylist(a,a + 5);//根據數組初始化

???autorb =mylist.rbegin();

???autore =mylist.rend();

???//同時正向方向查找

???for (;rb !=re;rb++)

???{

???????cout << *rb <<endl;

???}

???cin.get();

}

運行結果:

10.list合并,排序

#include<iostream>

#include<hash_set>

#include<list>???//實際上是一個雙向鏈表

#include<stdio.h>

?

//list使用于經常插入,經常刪除

?

usingnamespacestd;

?

voidmain()

{

???inta[5] = { 1, 2, 3, 104, 5 };

???list<int > mylist1(a,a + 5);//根據數組初始化,

???intb[5] = { 11, 122, 33, 44, 55 };

???list<int > mylist2(b,b + 5);//根據數組初始化,

???mylist1.sort();

???mylist2.sort();//兩個list合并到list之前需要數組排序

?

???mylist1.merge(mylist2);//合并之前必須有序

?

???{

???????autoibegin =mylist1.begin();//指針,指向一個迭代器,迭代器存儲了位置

???????autoiend =mylist1.end();

?

???????for (;ibegin !=iend;ibegin++)

???????{

???????????cout << *ibegin <<endl;

???????}

???}

???cout <<"\n\n\n";

???{

???????autoibegin =mylist2.begin();//指針,指向一個迭代器,迭代器存儲了位置

???????autoiend =mylist2.end();

?

???????for (;ibegin !=iend;ibegin++)

???????{

???????????cout << *ibegin <<endl;

???????}

???}

???cin.get();

}

運行結果:

11.list中通過unique()方法去掉重復的元素

#include<iostream>

#include<hash_set>

#include<list>???//實際上是一個雙向鏈表

#include<stdio.h>

?

//list使用于經常插入,經常刪除

?

usingnamespacestd;

?

voidmain()

{

???inta[6] = { 1, 2, 98, 2, 5, 98 };

???list<int>mylist1(a,a + 6);//根據數組初始化

???{

???????autoibegin =mylist1.begin();

???????autoiend =mylist1.end();

???????for (;ibegin !=iend;ibegin++)

???????{

???????????cout << *ibegin <<endl;

???????}

???}

???mylist1.sort();

???mylist1.unique();//唯一依賴于排序,通過這個方法實現了去掉重復的

???cout <<"\n\n\n";

???{

???????//指針,指向一個迭代器,迭代器存儲了位置

???????autoibegin =mylist1.begin();

???????autoiend =mylist1.end();

?

???????for (;ibegin !=iend;ibegin++)

???????{

???????????cout << *ibegin <<endl;

???????}

???}

???cin.get();

}

運行結果:

list迭代輸出

#include<iostream>

#include<set>

#include<stdio.h>

#include<list>

#include<vector>

#include<algorithm>

#include<functional>

?

usingnamespacestd;

voidmain()

{

???list<int>mylist;

?

???mylist.push_back(1);

???mylist.push_back(2);

???mylist.push_back(3);

???mylist.push_back(4);

???//mylist[1];

???autoibegin =mylist.begin();//指針,指向一個迭代器,迭代器存儲了位置

???autoiend =mylist.end();

???//list用迭代器進行遍歷

???for (;ibegin !=iend;ibegin++)

???{

???????cout << *ibegin <<endl;

???????printf("%p,%p\n",ibegin._Ptr,ibegin);//重載

???}

?

???cin.get();

}

運行結果:

12算法find

#include<algorithm>

#include<iostream>

usingnamespacestd;

?

structprint

{

???void operator()(intx)//重載了()符號,直接調用()

???{

???????std::cout << x <<endl;

???}

};

?

voidprintA(intx)

{

???std::cout << x <<endl;

}

?

//find這個算法

voidmain()

{

???inta[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

???int *p =find(a,a + 10, 8);

???std::cout << (void*)a << (void*)(a + 10) << std::endl;

???std::cout << *p <<endl;

???std::cout << p <<endl;

???if (p == (a + 10))

???{

???????std::cout << "沒有找到\n";

???}

???//下面的方式是調用重載的print函數

???for_each(a,a + 10,print());//遍歷每一個元素

???//printA是一個函數指針,必須是函數類型

???std::cout << "-------------------------" <<std::endl;

???for_each(a,a + 10,printA);

?

???cin.get();

}

運行結果:

13.find_if,bind1st,仿函數

#include<iostream>

#include<set>

#include<stdio.h>

#include<list>

#include<vector>

#include<algorithm>??//find_if的頭文件

#include<functional>?//仿函數需要這里

usingnamespacestd;

?

boolless3(intx)

{

???returnx < 3;

}

?

voidmain()

{

???vector<int>mylist;

???mylist.push_back(1);

???mylist.push_back(2);

???mylist.push_back(16);

???mylist.push_back(17);

???mylist.push_back(18);

?

???autoib =mylist.begin();

???autoie =mylist.end();

???for (;ib !=ie;ib++)

???{

???????std::cout << *ib <<std::endl;

???}

???//防函數可以實現一定的算法策略

???//bind1st表示要綁定一個函數

???//綁定一個函數,greater<int>(),3,表示比三大的數

???//查找第一個比3大的數值,下面的代碼的意思是找到第一個3比取出的數值大的數的位置

???autoifind =find_if(++mylist.begin(),mylist.end(),bind1st(greater<int>(), 3));

???std::cout << "\n\n\n\n" << *ifind << endl;

?

???std::cout << "---------------------" <<std::endl;

???autoifind2 =find_if(mylist.begin(),mylist.end(),less3);

???std::cout << "\n\n\n\n" << *ifind << endl;

?

???cin.get();

}

總結

以上是生活随笔為你收集整理的STL之vector,数组线性容器array,list容器,算法find,find_if,bind1st,仿函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩黄色影院 | 亚洲av成人精品毛片 | 美女成人在线 | av网站免费看 | 免费av导航 | 91精品国产自产精品男人的天堂 | 日韩一级片在线播放 | 国产一区二区三区免费观看视频 | 怡红院最新网址 | 天天舔日日操 | 男人天堂va | 欧美视频a | 欧美精品亚洲精品日韩精品 | 日日网站 | 国产美女网站视频 | 国产精品人人妻人人爽 | 免费在线一区二区三区 | 11一12免费毛片 | av中文字幕在线播放 | 韩国毛片一区二区 | 成人在线免费观看视频 | 一区二区三区在线免费观看视频 | 刘亦菲毛片一区二区三区 | 国产精品三区在线观看 | 91看片就是不一样 | 女av在线 | 久久久久久久久免费看无码 | 久久久午夜精品福利内容 | 无码人妻aⅴ一区二区三区69岛 | 91日日| 婷婷四月 | 韩日在线视频 | 中文字幕偷拍 | 精品久久香蕉国产线看观看亚洲 | 精品在线免费视频 | 国产午夜在线一区二区三区 | 五月婷激情| 免费在线观看av片 | 少妇高潮惨叫久久久久久 | 色视频在线观看 | 操你啦在线视频 | 亚洲乱亚洲 | 91精品国产综合久久久蜜臀图片 | 国产一级一区二区 | 荡女精品导航 | 国产精品偷拍 | 欧美日韩一区二区三区不卡 | 日韩电影在线观看一区 | a级黄片毛片 | 国产日韩二区 | 香蕉视频在线看 | 欧美黑人一级爽快片淫片高清 | 少妇人妻偷人精品无码视频新浪 | 1000部国产精品成人观看 | 亚洲免费精品视频在线观看 | 2021天天操 | av色成人| 久久国产精品电影 | 久久九九热 | 国产午夜电影在线观看 | 少妇久久精品 | 成人av久久 | 好看的av在线 | 一级黄色片免费观看 | 国产精品制服诱惑 | 九九亚洲 | 免费人妻精品一区二区三区 | 波多野结衣mp4 | 黄色免费成人 | 午夜伊人网| 亚洲区自拍偷拍 | 伊人久久超碰 | av2018| 日韩av在线免费观看 | 黄色污污视频软件 | 欧美成人国产精品一区二区 | videos麻豆| 国产女人高潮的av毛片 | 中文字幕日产av | 毛片女人 | 双性娇喘浑圆奶水h男男漫画 | 亚洲欧洲一区二区三区 | 国产高清在线观看视频 | 欧美日韩日本国产 | 精品无码三级在线观看视频 | 国产视频一区二区在线 | 2023毛片| 日韩中字在线 | 91插插插影库永久免费 | 无码无遮挡又大又爽又黄的视频 | 国产亚洲精品久久久久四川人 | 日韩一区二区av | 怡红院一区 | 黄色片xxxx| 成人av网站大全 | 一级特黄色 | 婷婷久久精品 | 青青免费视频 | 欧美激情影音先锋 |