天天看點

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation



1find查找

#include<iostream>

#include<vector>

#include<algorithm>

#include<list>

#include<set>

#include<string>

usingnamespacestd;

voidmain()

{

   vector<int>myv;

   myv.push_back(1);

   myv.push_back(2);

   myv.push_back(3);

   myv.push_back(11);

   myv.push_back(22);

   myv.push_back(33);

   for_each(myv.begin(),myv.end(),

[](intv){cout

<<v <<endl;

});

   auto i

=find(myv.begin(),myv.end(),

23);

   //說明已經到了最後面

   if

(i ==

myv.end())

   {

       std::cout

<< "23玩失蹤";

   }

   else

       //如果找到了則輸出結果

<< *i;

   cin.get();

}

運作結果:

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

2find_if,find_if_not

   std::cout

<< "-------" <<std::endl;

   //找到第一個比11大的值

   autoii

=find_if(myv.begin(),myv.end(),

[](intv)->bool{return

(v > 11); });

   //找到第一個不比4大的值

=find_if_not(myv.begin(),myv.end(),

(v > 4); });

(ii ==

<< "沒找到"

<< endl;

<< *ii <<endl;

<< "玩失蹤";

運作結果是:

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

3sort,fill,for_each

template <classt>

classshow

public:

   void

operator()(t &t)

       cout

<<t <<" ";

};

   list<int>list1;

   vector<int>v1;

   list1.push_back(121);

   list1.push_back(12);

   list1.push_back(122);

   list1.push_back(23);

   v1.push_back(121);

   v1.push_back(12);

   v1.push_back(122);

   v1.push_back(17);

   v1.push_back(23);

   //list不能通過sort進行排序,如果放開這一句将出現錯誤

   //sort(list1.begin(), list1.end());

   //排序,簡單的排序

   sort(v1.begin(),v1.end());

   //算法依賴于資料結構(鍊式,線性),不同的資料結構,算法不一樣

   //填充,指定位置資料進行初始化,将begin()+2到最後的數值填充為3

   fill(v1.begin()

+ 3, v1.end(),

3);

   for_each(list1.begin(),list1.end(),show<int>());

   cout

<<"\n";

   for_each(v1.begin(),v1.end(),show<int>());

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

4.count,統計某個節點值相同的元素有多少個

   multiset<int

> myset;

   myset.insert(3);

   myset.insert(1);

   myset.insert(2);

<<"統計總共的節點"

   inti

= 0;

   for

(autoib

=myset.begin(),ie

=myset.end();ib

!=ie;ib++,i++)

   {}

<<i <<endl;

   //統計有多少個節點1

<<"統計值為1的節點個數有多少個"

   intnum

=count(myset.begin(),myset.end(),

1);

<<num <<endl;

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

5. adjacent_find,依次列印出結果:

   void  operator

()(t &t)

   for_each(myset.begin(),myset.end(),show<constint>());

   //通過這種方式一個個的列印出結果

   autoit

=adjacent_find(myset.begin(),myset.end());

<<"\n" << *it

<<endl;

   it++;

   //查找相同的資料,可以自己确定位置

   it

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

6. random_shuffle實作随機排序

(inti

= 0;i < 10;i++)

       v1.push_back(i);

   //通過random_shuffle的方式實作随機排序vector

   random_shuffle(v1.begin(),v1.end());

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

用途:随機洗牌

7. partition分區

boolisok(intnum)

   return

(num >= 10);

   v1.push_back(20);

   v1.push_back(6);

   v1.push_back(27);

   v1.push_back(5);

   v1.push_back(67);

   v1.push_back(10);

   v1.push_back(13);

   v1.push_back(14);

   v1.push_back(3);

   //服務于快速排序的分區

   partition(v1.begin(),v1.end(),isok);

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

分析,本質:

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

8. prev_permutation檢視排序過程

   inta[4]

= { 2, 1, 3, 10 };

   do

<<a[0] <<"

" <<a[1]

<<" " <<a[2]

<<" " <<a[3]

       //通過下面這個代碼檢視排序過程

while (prev_permutation(a,a+4));

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

9.sort排序

   vector<char>myvector;

   myvector.push_back('b');

   myvector.push_back('a');

   myvector.push_back('c');

   myvector.push_back('y');

   myvector.push_back('z');

   myvector.push_back('x');

   for_each(myvector.begin(),myvector.end(),show<char>());

   //sort(one.begin(), one.begin() + 3);

   sort(myvector.begin(),myvector.begin()

+ 3);

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

10. partial_sort部分排序

classstudent

   stringname;

   intscore;

   student(stringstr,intnum)

:name(str),score(num)

   bool

operator <(conststudent

&s1)const

       returnthis->score

< s1.score;

intmain()

   vector<student>ss;

       students1("totoa",

106);

       ss.push_back(s1);

       students1("totob",

101);

       students1("totoc",

103);

       students1("totod",

105);

       students1("totoe",

67);

       students1("totof",

58);

       students1("totog",

111);

   //部分排序

   partial_sort(ss.begin(),ss.begin()

+ 4, ss.end());//部分排序

= 0;i < 7;i++)

<< ss[i].name

<< ss[i].score

<< "\n";

STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation

繼續閱讀