#include


 

1 adjacent_find

查找重復的元素

2 find_if

查找符合條件的第一個元素

3 find_if_not

查找不符合條件的第一個元素

4 for_each

可以遍歷每一個元素

5 partial_sort

部分排序

6 partition

服務於快速排序法的分區

7 prev_permutation

排序

8 random_shuffle

隨機排序

9 rotate

旋轉

 

adjacent_find

查找重復的元素

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <set>
 4 
 5 void main()
 6 {
 7     std::multiset<int>myset;
 8 
 9     myset.insert(3);
10     myset.insert(1);
11     myset.insert(2);
12     myset.insert(1);
13     myset.insert(2);
14 
15     auto it = adjacent_find(myset.begin(), myset.end());
16 
17     std::cout << *it << std::endl;
18 
19     it = adjacent_find(it++, myset.end());
20 
21     std::cout << *it << std::endl;
22 
23     it = adjacent_find(it++, myset.end());
24 
25     std::cout << *it << std::endl;
26 }

 

find

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 void main()
 6 { 7 std::vector<int>myv; 8 9 myv.push_back(1); 10 myv.push_back(2); 11 myv.push_back(3); 12 13 auto i = find_if(myv.begin(), myv.end(), [](int v)->bool {return v < 4; });//返回第一個小於4的元素 14 15 if (i == myv.end()) 16  { 17 std::cout << "not found" << std::endl; 18  } 19 else 20  { 21 std::cout << *i << std::endl; 22  } 23 }

 

find_if

查找符合條件的第一個元素

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 void main()
 6 { 7 std::vector<int>myv; 8 9 myv.push_back(1); 10 myv.push_back(2); 11 myv.push_back(3); 12 13 auto i = find_if(myv.begin(), myv.end(), [](int v)->bool {return v < 4; });//返回第一個小於4的元素 14 15 if (i == myv.end()) 16  { 17 std::cout << "not found" << std::endl; 18  } 19 else 20  { 21 std::cout << *i << std::endl; 22  } 23 }

 

find_if_not

查找不符合條件的第一個元素

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 void main()
 6 { 7 std::vector<int>myv; 8 9 myv.push_back(1); 10 myv.push_back(2); 11 myv.push_back(3); 12 13 auto i = find_if_not(myv.begin(), myv.end(), [](int v)->bool {return v < 4; });//返回第一個不是小於4的元素 14 15 if (i == myv.end()) 16  { 17 std::cout << "not found" << std::endl; 18  } 19 else 20  { 21 std::cout << *i << std::endl; 22  } 23 }

 

//[地址](參數) {語句; }

//&res直接操作一個變量,res等價於返回值,x代表參數,每次充當迭代器指向的元素

//不僅僅適用於array,也適用於vector

 

vector使用for_each

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5 
 6 void main()
 7 {
 8     std::vector<int>myvector;//創建一個數組,數組元素是int類型
 9 
10     myvector.push_back(11);//尾部插入
11     myvector.push_back(12);
12     myvector.push_back(13);
13 
14     int res = 0;//保存結果
15 
16     //[地址](參數) {語句; }
17     //&res直接操作一個變量,res等價於返回值,x代表參數,每次充當迭代器指向的元素
18     //不僅僅適用於array,也適用於vector
19     for_each(myvector.begin(), myvector.end(), [&res](int x) {res += x; });
20 
21     std::cout << res;//打印
22 
23     system("pause");
24 }

 

普通數組使用for_each

 

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 struct print
 5 {
 6     void operator ()(int x)//重載()
 7     {
 8         std::cout << x << std::endl;
 9     }
10 };
11 
12 void printA(int x)
13 {
14     std::cout << x << std::endl;
15 }
16 
17 int main()
18 {
19     int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
20     int *p = std::find(a, a + 10, 11);
21 
22     std::cout << a << " " << a + 10 << std::endl;
23     std::cout << *p << std::endl;
24 
25     if (p == a + 10)
26     {
27         printf("沒有找到\n");
28     }
29 
30     std::for_each(a, a + 10, print());//第三個參數是函數指針,必須是函數類型
31     std::for_each(a, a + 10, printA);
32 
33     return 0;
34 }

 

5 partial_sort

部分排序

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <vector>
 5 
 6 struct student
 7 {
 8 public:
 9     std::string name;
10     int score;
11 public:
12     student(std::string str, int num) :name(str), score(num)
13     {
14 
15     }
16     bool operator<(const student &s1)const
17     {
18         return this->score < s1.score;
19     }
20 };
21 
22 void main()
23 {
24     std::vector<student>ss;
25     
26     {
27         student s1("AA", 100);
28         ss.push_back(s1);
29     }
30 
31     {
32         student s1("BB", 99);
33         ss.push_back(s1);
34     }
35 
36     {
37         student s1("CC", 98);
38         ss.push_back(s1);
39     }
40 
41     {
42         student s1("DD", 97);
43         ss.push_back(s1);
44     }
45 
46     partial_sort(ss.begin(), ss.begin() + 2, ss.end());
47 
48     for (int i = 0; i < 2; i++)
49     {
50         std::cout << ss[i].name << " " << ss[i].score << std::endl;
51     }
52 }

 

partition

服務於快速排序法的分區

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 template <class T>
 6 struct show
 7 {
 8 public:
 9     void operator()(T &t)
10     {
11         std::cout << t << " ";
12     }
13 };
14 
15 bool isok(int num)
16 {
17     return num == 5;
18 }
19 
20 void main()
21 {
22     std::vector<int>myv;
23 
24     for (int i = 0; i < 10; i++)
25     {
26         myv.push_back(i);
27     }
28 
29     for_each(myv.begin(), myv.end(), show<int>());
30     std::cout << std::endl;
31 
32     partition(myv.begin(), myv.end(), isok);
33 
34     for_each(myv.begin(), myv.end(), show<int>());
35     std::cout << std::endl;
36 }

 

prev_permutation

排序

 

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 void main()
 5 {
 6     int a[4] = { 2,4,3,1 };
 7 
 8     do
 9     {
10         std::cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << std::endl;
11     } while (std::prev_permutation(a, a + 4));
12 }

 

random_shuffle

隨機排序

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 template <class T>
 6 struct show
 7 {
 8 public:
 9     void operator()(T &t)
10     {
11         std::cout << t << " ";
12     }
13 };
14 
15 void main()
16 {
17     std::vector<int>myv;
18 
19     for (int i = 0; i < 10; i++)
20     {
21         myv.push_back(i);
22     }
23 
24     for_each(myv.begin(), myv.end(), show<int>());
25     std::cout << std::endl;
26 
27     random_shuffle(myv.begin(), myv.end());//隨機排序
28 
29     for_each(myv.begin(), myv.end(), show<int>());
30     std::cout << std::endl;
31 
32     random_shuffle(myv.begin(), myv.end());//隨機排序
33 
34     for_each(myv.begin(), myv.end(), show<int>());
35     std::cout << std::endl;
36 }

 

rotate

旋轉

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 template <class T>
 6 struct show
 7 {
 8 public:
 9     void operator()(T &t)
10     {
11         std::cout << t << " ";
12     }
13 };
14 
15 bool isok(int num)
16 {
17     return num == 5;
18 }
19 
20 void main()
21 {
22     std::vector<int>myv;
23 
24     for (int i = 0; i < 10; i++)
25     {
26         myv.push_back(i);
27     }
28 
29     for_each(myv.begin(), myv.end(), show<int>());
30     std::cout << std::endl;
31 
32     rotate(myv.begin(), myv.begin() + 1, myv.end());
33 
34     for_each(myv.begin(), myv.end(), show<int>());
35     std::cout << std::endl;
36 
37     rotate(myv.begin(), myv.begin() + 2, myv.end());
38 
39     for_each(myv.begin(), myv.end(), show<int>());
40     std::cout << std::endl;
41 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM