STL 算法

                                                                         

非变异算法

 

非变异算法就是:不直接改变其操作的数据结构的元素

1,循环: for_each

template<class Init,class Fun>

Fun for_each(Init first,Init last, Fun f);

f 为全局函数或者一元函数

example:::

1>

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 void PrintCube(int n)
 8 {
 9     cout << n * n * n << " ";
10 }
11 
12 int main()
13 {
14     const int VECTOR_SIZE = 8;
15 
16     typedef vector<int> IntVector;
17     typedef IntVector::iterator IntVectorIt;
18 
19     IntVector Numbers(VECTOR_SIZE);
20     IntVectorIt start,end,it;
21 
22     for(int i = 0 ;i < VECTOR_SIZE ;i ++)
23         Numbers[i] = i + 1;
24 
25     start = Numbers.begin();
26     end = Numbers.end();
27 
28     cout << "Numbers { ";
29     for(it = start;it != end;it ++)
30         cout << *it << " ";
31 
32     cout << " }\n" << endl;
33 
34     for_each(start,end,PrintCube);
35 
36     cout << endl;
37 
38     return 0;
39 }

2>

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 class PrintInfo
 7 {
 8 private:
 9     int nSum;
10     int nMax;
11     int nMin;
12     int count;
13 public:
14     PrintInfo() : count(0),nSum(0) {}
15 
16     int GetSum() { return nSum; }
17     int GetMax() { return nMax; }
18     int GetMin() { return nMin; }
19 
20     void operator () (int x)
21     {
22         if(count == 0)
23         {
24             nMax = x;
25             nMin = x;
26         }
27 
28         else
29         {
30             if(nMax < x)
31             {
32                 nMax = x;
33             }
34             if(nMin > x)
35             {
36                 nMin = x;
37             }
38         }
39 
40         nSum += x;
41         count ++;
42     }
43 };
44 
45 int main()
46 {
47     int A[] = {1,4,2,8,5,7};
48     const int N = sizeof(A) / sizeof(int);
49 
50     PrintInfo  P = for_each(A,A+N,PrintInfo());
51     cout << "SUM : " << P.GetSum() << endl;
52     cout << "MAX : " << P.GetMax() << endl;
53     cout << "MIN : " << P.GetMin() << endl;
54 
55     return 0;
56 }

3>

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4 
 5 using namespace std;
 6 
 7 template<class T,class _outPara>
 8 class PrintInfo : unary_function<T,_outPara>
 9 {
10 private:
11     T nSum;
12     T nMax;
13     T nMin;
14     int count;
15 public:
16     PrintInfo() : count(0),nSum(0) {}
17 
18     T GetSum() { return nSum; }
19     T GetMax() { return nMax; }
20     T GetMin() { return nMin; }
21 
22     _outPara operator() (T x)
23     {
24         if(count == 0)
25         {
26             nMax = x;
27             nMin = x;
28         }
29         else
30         {
31             if(nMax < x)
32             {
33                 nMax = x;
34             }
35             if(nMin > x)
36             {
37                 nMin = x;
38             }
39         }
40 
41         nSum += x;
42         count ++;
43     }
44 
45 };
46 
47 int main()
48 {
49     float A[] = {1.5,4.2,2.6,8.9,5.7,7.1};
50     const int N = sizeof(A) / sizeof(int);
51 
52     PrintInfo<float,void> &P = for_each(A,A+N,PrintInfo<float,void>());
53 
54     cout << "SUM : " << P.GetSum() << endl;
55     cout << "MAX : " << P.GetMax() << endl;
56     cout << "MIN : " << P.GetMin() << endl;
57 
58     return 0;
59 }

2,查询:

主要的查询函数又:

find(),find_if(),find_first_of(),adjacent_find(),find_end(),search(),search_n()

template<class Init,class T>

Init find(Init first,Init last, const T & val);

template<class Init,class Pred>

Init find_if(Init first, Init last, Pred pr);

template<class FwdIt1, class FwdIt2>

FwdIt1 find_first_of(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2, FwdIt2 last2);

template<class FwdIt2,class FwdIt2,class Pred>

FwdIt1 find_first_of(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2, Pred pr);

template<class FwdIt>

FwdIt adjacent_find(FwdIt first, FwdIt last);

template<class FwdIt,class Pred>

FwdIt adjacent_find(FwdIt first,FwdIt last, Pred pr);

template<class FwdIt1,class FwdIt2>

FwdIt1 find_end(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2);

template<class FwdIt1,class FwdIt2,class Pred>

FwdIt1 find_end(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2, Pred pr);

template<class FwdIt1,class FwdIt2>

FwdIt1 search(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2);

template<class FwdIt1,class FwdIt2,class Pred>

FwdIt1 search(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2 ,Pred pr);

template<class FwdIt,class Dist,class T>

FwdIt search_n(FwdIt first,FwdIt last,Dist n,const T &val);

template<class FwdIt ,class Dist,class T,class Pred>

FwdIt search_n(FwdIt first,FwdIt last,Dist n,const T & val,Pred pr);

Example ::::

1,>

 1 #include <algorithm>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 bool greater(int m)
 7 {
 8     return m > 4;
 9 }
10 
11 int main()
12 {
13     int a[] = {1,2,2,2,3,4,4,5,6,7,1,2,2,3};
14     int nSize = sizeof(a) / sizeof(int);
15 
16     cout << "org array: " << endl;
17     for(int i = 0 ;i < nSize ;i ++)
18     {
19         cout << a[i] << "\t";
20     }
21     cout << endl << endl;
22 
23     int *p1 = find(a,a+nSize,3);
24     if(p1 != a+nSize)
25         cout << "(find) first equal 3 is : " << p1-a <<"\t value : " << *p1 << endl;
26 
27     int *p2 = find_if(a,a+nSize,greater);
28     if(p2 != a + nSize)
29         cout << "(find_if) first > 4 is : "  << p2-a << "\t value : " << *p2 << endl;
30 
31     int b[] = {10,12,6};
32     int nSize2 = sizeof(b) / sizeof(int);
33     int *p3 = find_first_of(a,a+nSize, b,b+nSize2);
34     if(p3 != a+nSize)
35         cout << "(find_first_of) first appear b in array a is : " << p3-a << "\t value: " << *p3 << endl;
36 
37     int *p4 = adjacent_find(a,a+nSize);
38     if(p4 != a+nSize)
39         cout << "(adjacent_find) first appear familiar is : " << p4-a << "\t value : " << *p4 << endl;
40 
41     int c[]={2,3};
42     int nSize3 = sizeof(c) / sizeof(int);
43     int *p5 = find_end(a,a+nSize,c,c+nSize3);
44     if(p5 != a+nSize)
45         cout << "last match array c is : " << p5-a << endl;
46 
47     int *p6 = search(a,a+nSize,c,c+nSize3);
48     if(p6 != a + nSize)
49         cout << "First match array c is : " << p6-a << endl;
50 
51     int *p7 = search_n(a,a+nSize,3,2);
52     if(p7 != a + nSize)
53         cout << "First appear 3 times 2 is : " << p7-a << endl;
54 
55     return 0;
56 }

2,>

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <vector>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 class Student
 9 {
10 public:
11     int NO;
12     string strName;
13     Student(int NO,string strName)
14     {
15         this -> NO = NO;
16         this -> strName = strName;
17     }
18 
19     bool operator == (int NO)
20     {
21         return (this->NO == NO);
22     }
23 };
24 
25 int main()
26 {
27     vector<Student> v;
28 
29     Student s1(101,"zhangsan");
30     Student s2(102,"lisi");
31 
32     v.push_back(s1);
33     v.push_back(s2);
34 
35     vector<Student>::iterator begin,end,it_find;
36     begin = v.begin();
37     end = v.end();
38 
39     int nFindNO = 102;
40     it_find = find(begin,end,nFindNO);
41 
42     cout << "lookup ID : " << nFindNO << "information : " << endl;
43 
44     if(it_find != end)
45         cout << "ID : " << (*it_find).NO << "\t" << "name : " << (*it_find).strName << endl;
46     else
47         cout << "No ID student!!!" << endl;
48 
49     return 0;
50 }

3,>

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <vector>
  4 #include <string>
  5 
  6 using namespace std;
  7 
  8 const int NO_FIND = 1;
  9 const int GRADE_FIND = 2;
 10 
 11 class Student
 12 {
 13 public:
 14     int NO;
 15     string strName;
 16     int grade;
 17     static int mark;
 18 
 19 
 20     Student(int NO,string strName,int grade)
 21     {
 22         this -> NO = NO;
 23         this -> strName = strName;
 24         this -> grade = grade;
 25     }
 26     
 27     bool operator == (int n)
 28     {
 29         if(mark == NO_FIND)
 30             return NO == n;
 31         else
 32             return grade == n;
 33     }
 34 
 35     bool operator == (string name)
 36     {
 37         return strName.compare(name) == 0;
 38     }
 39 };
 40 
 41 int Student::mark = -1;
 42 
 43 ostream & operator << (ostream &os,Student &s)
 44 {
 45     os << s.NO << "\t" << s.strName << "\t" << s.grade << endl;
 46 
 47     return os;
 48 }
 49 
 50 class StudFindIf
 51 {
 52 private:
 53     int low;
 54     int high;
 55 public:
 56     StudFindIf(int low,int high)
 57     {
 58         this -> low = low;
 59         this -> high = high;
 60     }
 61 
 62     bool operator() (Student &s)
 63     {
 64         return s.grade >= low && s.grade <= high;
 65     }
 66 };
 67 
 68 class StudentCollect
 69 {
 70     vector<Student> vecStud;
 71 public:
 72     bool Add(Student &s)
 73     {
 74         vecStud.push_back(s);
 75         return true;
 76     }
 77 
 78     bool FindByNO(int no)
 79     {
 80         Student::mark = NO_FIND;
 81         vector<Student>::iterator te = find(vecStud.begin(),vecStud.end(),no);
 82 
 83         if(te != vecStud.end())
 84             cout << *te << endl;
 85         else
 86             cout << "ID " << no << "has no note" << endl;
 87 
 88         return true;
 89     }
 90 
 91     bool FindByNO(int no[],int nSize)
 92     {
 93         bool bFind = false;
 94         Student::mark = NO_FIND;
 95 
 96         vector<Student>::iterator te = find_first_of(vecStud.begin(),vecStud.end(),no,no+nSize);
 97 
 98         while(te != vecStud.end())
 99         {
100             bFind = true;
101             cout << *te << endl;
102 
103             te ++;
104             te = find_first_of(te,vecStud.end(),no,no+nSize);
105         }
106         if(!bFind)
107             cout << "have no notes" << endl;
108         return true;
109     }
110 
111     bool FindByName(string name)
112     {
113         bool bFind = false;
114         vector<Student>::iterator te = find(vecStud.begin(),vecStud.end(),name);
115 
116         while(te != vecStud.end())
117         {
118             bFind = true;
119             cout << *te << endl;
120 
121             te ++;
122             te = find(te,vecStud.end(),name);
123         }
124 
125         if(!bFind)
126             cout << "name:" << name << "no notes" << endl;
127 
128         return true;
129     }
130 
131     bool FindByGrade(int grade)
132     {
133         Student::mark = GRADE_FIND;
134         
135         bool bFind = false;
136         vector<Student>::iterator te = find(vecStud.begin(),vecStud.end(),grade);
137 
138         while(te != vecStud.end())
139         {
140             bFind = true;
141             cout << *te << endl;
142 
143             te ++;
144             te = find(te,vecStud.end(),grade);
145         }
146 
147         if(!bFind)
148             cout << "grade : " << grade << "has no notes" << endl;
149 
150         return true;
151     }
152 
153     bool FindByRange(int low,int high)
154     {
155         bool bFind = false;
156 
157         StudFindIf sf(low,high);
158 
159         vector<Student>::iterator te = find_if(vecStud.begin(),vecStud.end(),sf);
160         while(te != vecStud.end())
161         {
162             bFind = true;
163             cout << *te << endl;
164 
165             te ++;
166             te = find_if(te,vecStud.end(),sf);
167         }
168 
169         return true;
170     }
171 };
172 
173 int main()
174 {
175     Student s1(101,"zhangsan",50);
176     Student s2(102,"lisi",70);
177     Student s3(103,"zhangsan",60);
178     Student s4(104,"wangwu",50);
179     Student s5(105,"wagnwu",80);
180 
181     StudentCollect manage;
182     manage.Add(s1);
183     manage.Add(s2);
184     manage.Add(s3);
185     manage.Add(s4);
186     manage.Add(s5);
187 
188     cout << "find via ID(102): " << endl;
189     manage.FindByNO(102);
190     cout << "find via name(zhangsan) : " << endl;
191     manage.FindByName("zhangsan");
192     cout <<"find via grade(50) : " << endl;
193     manage.FindByGrade(50);
194 
195     int a[] = {101,105,103,107};
196     cout << "find via ID group {101,105,103,107} : " << endl;
197     manage.FindByNO(a,sizeof(a)/sizeof(int));
198 
199     cout << "find via grade range[55,70] : " << endl;
200     manage.FindByRange(55,70);
201 
202     return 0;
203 }

 3,计数:

count() ,  count_if()

template<class Init, class T>

size_t  count(Init first, Init last, const T &val);

template<class Init, class Pred , class Dist>

size_t  count_if(Init first,Init last, Pred pr);

示例:

1>

 1 #include <algorithm>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int A[] = {2,0,4,6,0,3,1,-7};
 9 
10     const int N = sizeof(A) / sizeof(int);
11 
12     cout << "Number of zeros : " << count(A,A+N,0) << endl;
13 
14     return 0;
15 }

2,>

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 class Student
 8 {
 9 public:
10     int NO;
11     string strName;
12     int grade;
13 
14     Student(int NO,string strName,int grade)
15     {
16         this -> NO = NO;
17         this -> strName = strName;
18         this -> grade = grade;
19     }
20 
21     bool operator == (int grade)
22     {
23         return this -> grade == grade;
24     }
25 };
26 
27 int main()
28 {
29     Student s1(1000,"zhangsan",80);
30     Student s2(1001,"lisi",85);
31     Student s3(1002,"wangwu",80);
32     Student s4(1003,"zhaoliu",80);
33 
34     vector<Student> v;
35     v.push_back(s1);
36     v.push_back(s2);
37     v.push_back(s3);
38     v.push_back(s4);
39 
40     int nCount = count(v.begin(),v.end(),80);
41     cout << "the counts grade equal to 80 is : " << nCount << endl;
42 
43     return 0;
44 }

3,>

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 class Student
 8 {
 9 public:
10     int NO;
11     string strName;
12     int grade;
13 
14     Student(int NO,string strName,int grade)
15     {
16         this -> NO = NO;
17         this -> strName = strName;
18         this -> grade = grade;
19     }
20 
21     bool operator == (int grade)
22     {
23         return this -> grade == grade;
24     }
25 };
26 
27 
28 class MatchExpress
29 {
30     int grade;
31 public:
32     MatchExpress(int grade)
33     {
34         this -> grade = grade;
35     }
36 
37     bool operator() (Student &s)
38     {
39         return s.grade > grade;
40     }
41 };
42 
43 int main()
44 {
45     Student s1(1000,"zhangsan",80);
46     Student s2(1001,"lisi",85);
47     Student s3(1002,"wangwu",80);
48     Student s4(1003,"zhaoliu",80);
49 
50     vector<Student> v;
51     v.push_back(s1);
52     v.push_back(s2);
53     v.push_back(s3);
54     v.push_back(s4);
55 
56     int nCount = count_if(v.begin(),v.end(),MatchExpress(80));
57     cout << "the counts grade equal to 80 is : " << nCount << endl;
58 
59     return 0;
60 }

4,比较

equal(),mismatch()

template<class Init1,class Init2>

bool equal(Init1 first,Init1 last,Init2 x);

template<class Init1,class Init2,class Pred>

bool equal(Init1 first,Init1 last,Init2 x,Pred pr);

template<class Init1,class Init2>

pair<Init1,Init2>mismatch(Init1 first,Init last,Init2 x);

template<class Init1,class Init2,class Pred>

pair<Init1,Init2>mismatch(Init1 first,Init1 last,Init2 x,Pred pr);

示例:

1>

 1 #include <algorithm>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int A1[] = {3,1,4,1,5,9,3};
 9     int A2[] = {3,1,4,2,8,5,7};
10 
11     const int N = sizeof(A1)/sizeof(int);
12 
13     cout << "Result of comparison : " << equal(A1,A1+N,A2) <<endl;
14 
15     return 0;
16 }

2,

 1 #include <algorithm>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int A1[] = {3,1,4,1,5,9,3};
 9     int A2[] = {3,1,4,2,8,5,7};
10 
11     const int N = sizeof(A1)/sizeof(int);
12 
13     pair<int *,int *>result = mismatch(A1,A1+N,A2);
14 
15     cout << "The first mismatch is in position " << result.first - A1 << endl;
16     cout << "Values are: " << *(result.first) <<"," << *(result.second) << endl;
17 
18     return 0;
19 }

3,

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <vector>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 class Student
 9 {
10 public:
11     int NO;
12     string strName;
13     int grade;
14 
15     Student(int NO,string strName,int grade)
16     {
17         this -> NO = NO;
18         this -> strName = strName;
19         this -> grade = grade;
20     }
21 
22     bool operator == (Student &s)
23     {
24         return this->grade == s.grade;
25     }
26 };
27 
28 
29 int main()
30 {
31     Student s1(1001,"aaa",90);
32     Student s2(1002,"bbb",80);
33     Student s3(1003,"ccc",70);
34     vector<Student> v1;
35     v1.push_back(s1);
36     v1.push_back(s2);
37     v1.push_back(s3);
38 
39     
40     Student s4(1004,"ddd",90);
41     Student s5(1005,"eee",80);
42     Student s6(1006,"fff",75);
43     vector<Student> v2;
44     v2.push_back(s4);
45     v2.push_back(s5);
46     v2.push_back(s6);
47 
48     cout << "Find the first unequal student infomation : " << endl;
49     pair<Student *,Student *> result = mismatch(v1.begin(),v1.end(),v2.begin());
50 
51     Student & stu1 = *result.first;
52     Student & stu2 = *result.second;
53 
54     cout << "ID : " << stu1.NO << "\tname : " << stu1.strName << "\tgrade : "<< stu1.grade << endl;
55     cout << "ID : " << stu2.NO << "\tname : " << stu2.strName << "\tgrade : "<< stu2.grade << endl;
56 
57     return 0;
58 }
原文地址:https://www.cnblogs.com/lfsblack/p/2769734.html