亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C > 正文

淺析STL中的常用算法

2020-01-26 15:49:17
字體:
來源:轉載
供稿:網友

一、非變異算法

是一組不破壞操作數據的模板函數,用來對序列數據進行逐個處理、元素查找、子序列搜索、統計和匹配。非變異算法具有極為廣泛的適用性,基本上可應用與各種容器。

1查找容器元素find

它用于查找等于某值的元素。它在迭代器區間[first,last)(閉開區間)上查找等于value值的元素,如果迭代器i所指的元素滿足*i=value,則返回迭代器i;未找到滿足條件的元素,返回last。函數原型:find( v1.begin(), v1.end(), num_to_find );

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

int num_to_find = 6;

vector<int> v1;

for( int i = 0; i < 10; i++ )

v1.push_back(2*i);

vector<int>::iterator result;

result = find( v1.begin(), v1.end(), num_to_find );

if( result == v1.end() )

cout << "未找到任何元素匹配 " << num_to_find << endl;

else

cout << "匹配元素的索引值是 " << result-v1.begin() << endl;

}


2條件查找容器元素find_if

利用返回布爾值的謂詞判斷pred,檢查迭代器區間[first,last)(閉開區間)上的每一個元素,如果迭代器i滿足pred(*i)=true,表示找到元素并返回迭代值i(找到的第一個符合條件的元素);未找到元素,返回末位置last。函數原型:find_if(v.begin(),v.end(),divby5);

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool divby5(int x)

{

return x%5?0:1;

}

void main()

{

vector<int> v(20);

for(int i=0;i<v.size();i++)

{

v[i]=(i+1)*(i+3);

cout<<v[i]<<' ';

}

cout<<endl;

vector<int>::iterator ilocation;

ilocation=find_if(v.begin(),v.end(),divby5);

if(ilocation!=v.end())

cout<<"找到第一個能被5整除的元素:"<<*ilocation<<endl<<"元素的索引位置是: "<<ilocation-v.begin()<<endl;

}


3統計等于某值的容器元素個數count

list<int> l;
count(l.begin(),l.end(),value)

4條件統計count_if

count_if(l.begin(),l.end(),pred)。謂詞pred含義同find_if中的謂詞。例子可以參考例2.

5子序列搜索search

search算法函數在一個序列中搜索與另一序列匹配的子序列。參數分別為一個序列的開始位置,結束位置和另一個序列的開始,結束位置。

函數原型:search(v1.begin(),v1.end(),v2.begin(),v2.end());

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v1;

cout<<"v1:";

for(int i=0;i<5;i++)

{

v1.push_back(i+5);

//注意:v1定義時沒有給定大小,因此這里不能直接使用賦值語句。

cout<<v1[i]<<' ';

}

cout<<endl;

vector<int> v2;

cout<<"v2:";

for(i=0;i<2;i++)

{

v2.push_back(i+7);

cout<<v2[i]<<' ';

}

cout<<endl;

vector<int>::iterator ilocation;

ilocation=search(v1.begin(),v1.end(),v2.begin(),v2.end());

if(ilocation!=v1.end())

cout<<"v2的元素包含在v1中,起始元素為"<<"v1["<<ilocation-v1.begin()<<']'<<endl;

else

cout<<"v2的元素不包含在v1中"<<endl;

}


6重復元素子序列搜索search_n

search_n算法函數搜索序列中是否有一系列元素值均為某個給定值的子序列。函數原型:search_n(v.begin(),v.end(),3,8),在v中找到3個連續的元素8

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(1);

v.push_back(8);

v.push_back(8);

v.push_back(8);

v.push_back(6);

v.push_back(6);

v.push_back(8);

vector<int>::iterator i;

i=search_n(v.begin(),v.end(),3,8);

if(i!=v.end())

cout<<"在v中找到3個連續的元素8"<<endl;

else

cout<<"在v中未找到3個連續的元素8"<<endl;

}


7最后一個子序列搜索find_end

函數原型find_end(v1.begin(),v1.end(),v2.begin(),v2.end());在V1中要求的位置查找V2中要求的序列。

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

 

void main()

{

vector<int> v1;

v1.push_back(-5);

v1.push_back(1);

v1.push_back(2);

v1.push_back(-6);

v1.push_back(-8);

v1.push_back(1);

v1.push_back(2);

v1.push_back(-11);

vector<int> v2;

v2.push_back(1);

v2.push_back(2);

vector<int>::iterator i;

i=find_end(v1.begin(),v1.end(),v2.begin(),v2.end());

if(i!=v1.end())

cout<<"v1中找到最后一個匹配v2的子序列,位置在" <<"v1["<<i-v1.begin()<<"]"<<endl;

}


二、變異算法

是一組能夠修改容器元素數據的模板函數。copy(v.begin(),v.end(),l.begin());將v中的元素復制到l中。

1 元素復制copy

復制代碼 代碼如下:

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(1);

v.push_back(3);

v.push_back(5);

 

list<int> l;

l.push_back(2);

l.push_back(4);

l.push_back(6);

l.push_back(8);

l.push_back(10);

copy(v.begin(),v.end(),l.begin());

list<int>::iterator i;

for(i=l.begin();i!=l.end();i++)

cout<<*i<<' ';

cout<<endl;

}


2 元素變換transform改變

函數原型:transform(v.begin(),v.end(),l.begin(),square);也是復制,但是要按某種方案復制。

復制代碼 代碼如下:

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

 

int square(int x)

{

return x*x;

}

void main()

{

vector<int> v;

v.push_back(5);

v.push_back(15);

v.push_back(25);

list<int> l(3);

transform(v.begin(),v.end(),l.begin(),square);

list<int>::iterator i;

for(i=l.begin();i!=l.end();i++)

cout<<*i<<' ';

cout<<endl;

}


3 替換replace

replace算法將指定元素值替換為新值。

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(13);

v.push_back(25);

v.push_back(27);

v.push_back(25);

v.push_back(29);

replace(v.begin(),v.end(),25,100);

vector<int>::iterator i;

for(i=v.begin();i!=v.end();i++)

cout<<*i<<' ';

cout<<endl;

}


輸出結果為13 100 27 100 29

4 條件替換replace_if

函數原型:replace_if(v.begin(),v.end(),odd,100);

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool odd(int x)

{

return x%2;

}

void main()

{

vector<int> v;

for(int i=1;i<10;i++)

v.push_back(i);

replace_if(v.begin(),v.end(),odd,100);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


5 n次填充fill_n

函數原型fill_n(v.begin(),5,-1);向從v.begin開始的后面5個位置跳入-1

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v(10);

fill_n(v.begin(),5,-1);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


輸出結果:-1 -1 -1 -1 -1 0 0 0 0 0

6 隨機生成n個元素generate

函數原型:generate_n(v.begin(),5,rand);向從v.begin開始的后面5個位置隨機填寫數據。

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v(10);

generate_n(v.begin(),5,rand);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


7 條件移除remove_if

返回值相當于移除滿足條件的元素后形成的新向量的end()值。

函數原型:remove_if(v.begin(),v.end(),even);

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

bool even(int x)

{

return x%2?0:1;

}

void main()

{

vector<int> v;

for(int i=1;i<=10;i++)

v.push_back(i);

vector<int>::iterator ilocation,result;

cout<<"移除前:";

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

result=remove_if(v.begin(),v.end(),even);

cout<<"移除后:";

for(ilocation=v.begin();ilocation!=result;ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


8 剔除連續重復元素unique

函數原型:unique(v.begin(),v.end());

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(2);

v.push_back(6);

v.push_back(6);

v.push_back(6);

v.push_back(9);

v.push_back(6);

v.push_back(3);

vector<int>::iterator ilocation,result;

result=unique(v.begin(),v.end());

for(ilocation=v.begin();ilocation!=result;ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


輸出結果:2 6 9 6 3

三、排序算法

1、創建堆make_heap

2、元素入堆push_heap(默認插入最后一個元素)

3、元素出堆pop_heap(與push_heap一樣,pop_heap必須對堆操作才有意義)

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(5);

v.push_back(6);

v.push_back(4);

v.push_back(8);

v.push_back(2);

v.push_back(3);

v.push_back(7);

v.push_back(1);

v.push_back(9);

make_heap(v.begin(),v.end());

v.push_back(20);

push_heap(v.begin(),v.end());

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

pop_heap(v.begin(),v.end());

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


4 堆排序sort_heap

使用:

復制代碼 代碼如下:

make_heap(v.begin(),v.end());

sort_heap(v.begin(),v.end());

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(3);

v.push_back(9);

v.push_back(6);

v.push_back(3);

v.push_back(17);

v.push_back(20);

v.push_back(12);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

make_heap(v.begin(),v.end());

sort_heap(v.begin(),v.end());

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}


輸出結果:

3 9 6 3 17 20 12
3 3 6 9 12 17 20

5 排序sort

函數原型:sort(v.begin(),v.end());

復制代碼 代碼如下:

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

void main()

{

vector<int> v;

v.push_back(2);

v.push_back(8);

v.push_back(-15);

v.push_back(90);

v.push_back(26);

v.push_back(7);

v.push_back(23);

v.push_back(30);

v.push_back(-27);

v.push_back(39);

v.push_back(55);

vector<int>::iterator ilocation;

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

sort(v.begin(),v.end());//比較函數默認

for(ilocation=v.begin();ilocation!=v.end();ilocation++)

cout<<*ilocation<<' ';

cout<<endl;

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
91精品啪在线观看麻豆免费| 日韩av三级在线观看| 亚洲二区在线播放视频| 在线日韩日本国产亚洲| 亚洲偷欧美偷国内偷| 欧美日韩中文字幕日韩欧美| 国产精品久久久av久久久| 亚洲高清一区二| 国内揄拍国内精品少妇国语| 欧美裸体xxxx极品少妇软件| 亚洲在线视频福利| 久久久久这里只有精品| 亚洲精品小视频在线观看| 久久不射热爱视频精品| 国产激情久久久| 欧美亚洲另类在线| 色妞久久福利网| 51视频国产精品一区二区| 国产精品99久久久久久人| 色伦专区97中文字幕| 亚洲bt天天射| 亚洲人高潮女人毛茸茸| 久久久久久久成人| 国产欧美一区二区三区视频| 日本19禁啪啪免费观看www| 国产91av在线| 国产精品久久久久久网站| 亚洲第一精品夜夜躁人人躁| 亚洲美女久久久| 精品高清美女精品国产区| 国产精品永久免费观看| 国语自产在线不卡| 日韩福利伦理影院免费| 97av在线视频| 欧美精品videos另类日本| 亚洲人成电影网站色| 国产精品免费观看在线| 精品亚洲国产成av人片传媒| 精品亚洲男同gayvideo网站| 成人久久18免费网站图片| 亚洲白虎美女被爆操| 久久精品色欧美aⅴ一区二区| 精品成人av一区| 91精品国产91久久久久福利| 深夜精品寂寞黄网站在线观看| 在线免费观看羞羞视频一区二区| 亚洲一区999| 97av在线视频免费播放| 欧洲成人在线观看| 97在线看福利| 在线视频一区二区| 久久精品最新地址| 欧美性xxxxxx| 日韩欧美在线视频日韩欧美在线视频| 久久久国产一区二区三区| 亚洲欧美一区二区三区四区| 日韩成人在线网站| 亚洲a在线观看| 91免费欧美精品| 成人网在线观看| 成人亚洲欧美一区二区三区| 国产成人精品优优av| 7777kkkk成人观看| 日韩电视剧在线观看免费网站| 日韩国产精品亚洲а∨天堂免| 日韩精品中文在线观看| 久久久久亚洲精品成人网小说| 欧美黑人极品猛少妇色xxxxx| 亚洲精品狠狠操| 亚洲一区二区在线| 国产精品福利在线观看| 不卡中文字幕av| 日韩在线中文字幕| 亚洲精选在线观看| 亚洲欧美激情另类校园| 久久国产天堂福利天堂| 国产午夜精品久久久| 奇米四色中文综合久久| 日韩精品在线观看一区二区| 国产一区二区三区日韩欧美| 亚洲美女免费精品视频在线观看| 日韩福利伦理影院免费| 欧美日韩亚洲视频一区| 久久久欧美一区二区| 日本欧美黄网站| 国产不卡精品视男人的天堂| 国产噜噜噜噜久久久久久久久| 国产精品日韩在线一区| 一区二区三区高清国产| 亚洲精品视频在线观看视频| 亚洲第一精品夜夜躁人人爽| 97在线视频免费| 久久频这里精品99香蕉| 欧美激情欧美激情在线五月| 91av视频在线观看| 欧美不卡视频一区发布| 久久久精品电影| 日韩精品欧美激情| 国产欧美日韩精品丝袜高跟鞋| 亚洲国产精品va在看黑人| 国产精品88a∨| 日韩视频永久免费观看| 欧美成人久久久| 亚洲一区二区免费| 日韩在线观看网站| 亚洲国产成人久久综合一区| 欧美最猛黑人xxxx黑人猛叫黄| 最新国产成人av网站网址麻豆| 精品视频在线播放色网色视频| 美女视频久久黄| 国产成人精品视频在线| 亚洲色图综合网| 亚洲网站在线播放| 91福利视频网| 中文亚洲视频在线| 亚洲综合在线播放| 欧美成人免费va影院高清| 成人免费视频网址| 992tv成人免费视频| 久久中文精品视频| 日韩美女在线观看| 热草久综合在线| 91九色在线视频| 久久亚洲影音av资源网| 国产精品高潮呻吟视频| 亚洲精品国产成人| 超碰91人人草人人干| 国产美女高潮久久白浆| 欧美激情中文字幕乱码免费| 国产精品色午夜在线观看| 亚洲色在线视频| 久久精品久久精品亚洲人| 热re99久久精品国产66热| 日韩av一区在线观看| 亚洲男人天堂九九视频| 8050国产精品久久久久久| 国产视频999| 日韩av电影手机在线观看| 91亚洲国产成人久久精品网站| 岛国av一区二区在线在线观看| 一区二区三区黄色| 欧美激情久久久久| 亚洲精选在线观看| 国内精品一区二区三区四区| 中文字幕日韩av电影| 最近2019好看的中文字幕免费| 大桥未久av一区二区三区| 欧美日韩激情视频| 国产精品美女免费视频| 亚洲免费伊人电影在线观看av| 国产一区二区色| 国产精品18久久久久久麻辣| 91av在线国产| 91高清视频在线免费观看| 亚州精品天堂中文字幕| 午夜精品99久久免费| 激情av一区二区| 国产精品扒开腿做爽爽爽男男| 亚洲男人天堂2024| 亚洲情综合五月天| 亚洲精品美女久久久久| 精品亚洲男同gayvideo网站| 国产69精品久久久久9999| 国产精品免费一区豆花|