首页 > 其他 > 详细

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

时间:2014-08-31 22:57:32      阅读:440      评论:0      收藏:0      [点我收藏+]


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

   {

       //如果找到了则输出结果

       std::cout << *i;

   }

   cin.get();

}

运行结果:

bubuko.com,布布扣

2find_if,find_if_not

#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; });

 

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

 

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

   autoii =find_if(myv.begin(),myv.end(), [](intv)->bool{return (v > 11); });

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

   auto i =find_if_not(myv.begin(),myv.end(), [](intv)->bool{return (v > 4); });

 

   if (ii == myv.end())

   {

       std::cout << "没找到" << endl;

   }

   else

   {

       std::cout << *ii <<endl;

   }

 

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

 

   if (i == myv.end())

   {

       std::cout << "玩失踪";

   }

   else

   {

       std::cout << *i;

   }

   cin.get();

}

运行结果是:

bubuko.com,布布扣

3sort,fill,for_each

#include<iostream>

#include<vector>

#include<algorithm>

#include<list>

#include<set>

#include<string>

 

usingnamespacestd;

template <classT>

classshow

{

public:

   void operator()(T &t)

   {

       cout <<t <<" ";

   }

};

 

voidmain()

{

   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>());

 

   cin.get();

}

运行结果:

bubuko.com,布布扣

4.count,统计某个节点值相同的元素有多少个

#include<iostream>

#include<vector>

#include<algorithm>

#include<list>

#include<set>

#include<string>

 

usingnamespacestd;

 

voidmain()

{

   multiset<int > myset;

   myset.insert(3);

   myset.insert(1);

   myset.insert(2);

   myset.insert(1);

   myset.insert(2);

   myset.insert(1);

   myset.insert(2);

   myset.insert(1);

   myset.insert(1);

   myset.insert(1);

 

   cout <<"统计总共的节点" << endl;

   inti = 0;

   for (autoib =myset.begin(),ie =myset.end();ib !=ie;ib++,i++)

   {}

   cout <<i <<endl;

   

   //统计有多少个节点1

   cout <<"统计值为1的节点个数有多少个" << endl;

   intnum =count(myset.begin(),myset.end(), 1);

   cout <<num <<endl;

 

   cin.get();

}

运行结果:

bubuko.com,布布扣

5. adjacent_find,依次打印出结果:

#include<iostream>

#include<vector>

#include<algorithm>

#include<list>

#include<set>

#include<string>

 

usingnamespacestd;

 

template <classT>

classshow

{

public:

   void  operator ()(T &t)

   {

       cout <<t <<" ";

   }

 

};

 

voidmain()

{

   multiset<int > myset;

   myset.insert(3);

   myset.insert(1);

   myset.insert(2);

   myset.insert(1);

   myset.insert(2);

   myset.insert(1);

   myset.insert(2);

 

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

   //通过这种方式一个个的打印出结果

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

   cout <<"\n" << *it <<endl;

   it++;

   cout <<"\n" << *it <<endl;

   it++;

   cout <<"\n" << *it <<endl;

   it++;

   cout <<"\n" << *it <<endl;

   //查找相同的数据,可以自己确定位置

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

 

   cout <<"\n" << *it <<endl;

   it++;

   cout <<"\n" << *it <<endl;

   it++;

   cout <<"\n" << *it <<endl;

   it++;

   cout <<"\n" << *it <<endl;

   

   cin.get();

}

运行结果:

bubuko.com,布布扣

6. random_shuffle实现随机排序

#include<iostream>

#include<vector>

#include<algorithm>

#include<list>

#include<set>

#include<string>

 

usingnamespacestd;

 

template <classT>

classshow

{

public:

   void  operator ()(T &t)

   {

       cout <<t <<" ";

   }

 

};

 

voidmain()

{

   vector<int>v1;

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

   {

       v1.push_back(i);

   }

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

   cout <<"\n";

   //通过random_shuffle的方式实现随机排序vector

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

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

   cout <<"\n";

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

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

   cout <<"\n";

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

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

   

   cin.get();

}

运行结果:

bubuko.com,布布扣

用途:随机洗牌

7. partition分区

#include<iostream>

#include<vector>

#include<algorithm>

 

usingnamespacestd;

 

template <classT>

classshow

{

public:

   void  operator ()(T &t)

   {

       cout <<t <<" ";

   }

};

 

boolisok(intnum)

{

   return (num >= 10);

}

 

voidmain()

{

   vector<int>v1;

   v1.push_back(20);

   v1.push_back(23);

   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);

 

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

   //服务于快速排序的分区

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

   

   cout <<"\n";

 

   cout <<endl;

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

 

   cin.get();

}

运行结果:

bubuko.com,布布扣

分析,本质:

bubuko.com,布布扣

8. prev_permutation查看排序过程

#include<iostream>

#include<vector>

#include<algorithm>

 

usingnamespacestd;

 

voidmain()

{

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

   do

   {

       cout <<a[0] <<" " <<a[1] <<" " <<a[2] <<" " <<a[3] <<"\n";

       //通过下面这个代码查看排序过程

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

   cin.get();

}

运行结果:

bubuko.com,布布扣

9.sort排序

#include<iostream>

#include<vector>

#include<algorithm>

 

usingnamespacestd;

 

template <classT>

classshow

{

public:

   void  operator ()(T &t)

   {

       cout <<t <<" ";

   }

};

 

voidmain()

{

   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);

   cout <<"\n";

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

 

   cin.get();

}

运行结果:

bubuko.com,布布扣

10. partial_sort部分排序

#include<iostream>

#include<vector>

#include<algorithm>

#include<list>

#include<set>

#include<string>

 

usingnamespacestd;

classstudent

{

public:

   stringname;

   intscore;

 

public:

   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);

       ss.push_back(s1);

   }

   {

       students1("totoC", 103);

       ss.push_back(s1);

   }

   {

       students1("totoD", 105);

       ss.push_back(s1);

   }

   {

       students1("totoE", 67);

       ss.push_back(s1);

   }

   {

       students1("totoF", 58);

       ss.push_back(s1);

   }

   {

       students1("totoG", 111);

       ss.push_back(s1);

   }

   //部分排序

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

 

   for (inti = 0;i < 7;i++)

   {

       std::cout << ss[i].name << ss[i].score << "\n";

   }

   cin.get();

}

运行结果:

bubuko.com,布布扣

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

原文:http://blog.csdn.net/tototuzuoquan/article/details/38964877

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!