天天看点

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

继续阅读