zoukankan      html  css  js  c++  java
  • #include <algorithm>

    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("没有找到
    ");
    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 }
  • 相关阅读:
    c语言 12
    c语言中用结构体表示点的坐标,并计算两点之间的距离
    c语言 12
    c语言中结构体数组
    c语言 12-3
    c语言 12-2
    codevs3164 质因数分解
    codevs4438 YJQ Runs Upstairs
    codevs4439 YJQ Requires Food
    codevs4437 YJQ Arranges Sequences
  • 原文地址:https://www.cnblogs.com/denggelin/p/5615588.html
Copyright © 2011-2022 走看看