• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

p237-STL-4-函数对象

开发技术 开发技术 2周前 (04-07) 10次浏览

4.STL-函数对象

4.1函数对象

4.1.1函数对象的概念

p237-STL-4-函数对象

 

 

 4.1.2函数对象使用

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 
 5 
 6 class MyAdd
 7 {
 8 public:
 9 
10     int operator()(int v1, int v2)
11     {
12         return v1 + v2;
13     }
14 };
15 
16 
17 //1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
18 void test01()
19 {
20     MyAdd myadd;
21     cout << myadd(10, 10)<<endl;
22 
23 
24 }
25 
26 
27 //2、函数对象超出普通函数的概念,函数对象可以有自己的状态
28 class MyPrint
29 {
30 public:
31     MyPrint()
32     {
33         this->count = 0;
34     }
35 
36     void operator()(string test)
37     {
38         cout << test << endl;
39         this->count ++;
40     }
41 
42     int count; //内部自己状态
43 
44 };
45 
46 void test02()
47 {
48     MyPrint myPrint;
49     myPrint("hello world");
50     myPrint("hello world");
51     myPrint("hello world");
52     myPrint("hello world");
53     myPrint("hello world");
54 
55     cout << "myPrint调用次数为:" << myPrint.count << endl;
56 }
57 
58 //3、函数对象可以作为参数传递
59 void doPrint(MyPrint &mp, string test)
60 {
61     mp(test);
62 }
63 
64 void test03()
65 {
66     MyPrint myPrint;
67     doPrint(myPrint, "hello c++");
68 }
69 
70 int main()
71 {
72 
73     //test01();
74     //test02();
75     test03();
76     return 0;
77 }

特点:

//1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值

//2、函数对象超出普通函数的概念,函数对象可以有自己的状态

//3、函数对象可以作为参数传递

4.2谓词

4.2.1谓词概念

p237-STL-4-函数对象

 

 

 4.2.2一元谓词

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 //仿函数 返回值类型是bool数据类型,成为谓词
 7 //一元谓词
 8 
 9 
10 
11 class GreaterFive
12 {
13 public:
14     bool operator()(int val)
15     {
16         return val > 5;
17     }
18 
19 };
20 
21 void test01()
22 {
23     vector<int> v;
24     for (int i = 0; i < 10; i++)
25     {
26         v.push_back(i);
27     }
28 
29     //查找容器中有没有大于5的数字
30     //GreaterFive()匿名函数对象
31     
32     vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
33     if (it == v.end())
34     {
35         cout << "未找到" << endl;
36     }
37     else
38     {
39         cout << "找到了,大于5的数字是:" << *it << endl;
40     }
41 
42 }
43 
44 
45 int main()
46 {
47 
48     test01();
49     //test02();
50     //test03();
51 
52     return 0;
53 }

 4.2.3 二元谓词

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 //二元谓词
 7 
 8 class MyCompare
 9 {
10 
11 public:
12     bool operator()(int val1, int val2)
13     {
14         return val1 > val2;
15     }
16     
17 };
18 
19 
20 
21 void test01()
22 {
23     vector<int> v;
24     v.push_back(10);
25     v.push_back(40);
26     v.push_back(20);
27     v.push_back(30);
28     v.push_back(50);
29 
30     sort(v.begin(), v.end());
31 
32     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
33     {
34         cout << *it << " ";
35     }
36     cout << endl;
37 
38     //使用函数对象    改变算法策略,变为排序规则为从大到小
39     sort(v.begin(), v.end(), MyCompare());
40     cout << "----------------" << endl;
41     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
42     {
43         cout << *it << " ";
44     }
45     cout << endl;
46 
47 
48 
49 
50 }
51 
52 
53 int main()
54 {
55 
56     test01();
57     //test02();
58     //test03();
59 
60     return 0;
61 }

4.3内建函数对象

4.3.1内建函数对象意义

p237-STL-4-函数对象

 

 

 4.3.2算数仿函数

实现四则运算

 1 #include<iostream>
 2 using namespace std;
 3 #include<functional>
 4 #include<vector>
 5 #include<algorithm>
 6 
 7 //内建函数对象    算数仿函数
 8 
 9 //negate 一元仿函数 取反仿函数
10 //plus    二元仿函数 加法
11 
12 
13 
14 
15 void test01()
16 {
17     negate<int>n;
18     cout << n(50)<<endl;
19 }
20 
21 void test02()
22 {
23     plus<int>p;
24 
25     cout << p(10, 20) << endl;
26 
27 
28 }
29 
30 
31 int main()
32 {
33 
34     //test01();
35     test02();
36     //test03();
37 
38     return 0;
39 }

 4.3.3关系仿函数

p237-STL-4-函数对象

 

 

 常用的:大于

 1 #include<iostream>
 2 using namespace std;
 3 #include<functional>
 4 #include<vector>
 5 #include<algorithm>
 6 
 7 //内建函数对象    关系仿函数
 8 
 9 //大于    greater
10 class MyCompare
11 {
12 public:
13     bool operator()(int v1,int v2)
14     {
15         return v1 > v2;
16     }
17 };
18 
19 void test01()
20 {
21     vector<int>v;
22     v.push_back(10);
23     v.push_back(30);
24     v.push_back(40);
25     v.push_back(20);
26     v.push_back(50);
27 
28     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
29     {
30         cout << *it << " ";
31     }
32     cout << endl;
33 
34     //降序
35     //sort(v.begin(), v.end(), MyCompare());//默认升序排序
36     // greater<int>()内建函数对象
37     sort(v.begin(), v.end(), greater<int>());    //编译器提供的仿函数,里面的实现其实是一样的
38     for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
39     {
40         cout << *it << " ";
41     }
42     cout << endl;
43 
44 }
45 
46 void test02()
47 {
48     plus<int>p;
49 
50     cout << p(10, 20) << endl;
51 
52 
53 }
54 
55 
56 int main()
57 {
58 
59     test01();
60     //test02();
61     //test03();
62 
63     return 0;
64 }

用来作为sort排序参数

4.3.4逻辑仿函数

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 #include<functional>
 6 
 7 //内建函数对象 逻辑仿函数
 8 //逻辑非 logical_not
 9 
10 void test01()
11 {
12     vector<bool> v;
13     v.push_back(true);
14     v.push_back(false);
15     v.push_back(true);
16     v.push_back(false);
17 
18     for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
19     {
20         cout << *it << " ";
21     }
22     cout << endl;
23 
24     //利用逻辑非 将容器v 搬运到容器v2中 并执行取反 操作
25     vector<bool>v2;
26     v2.resize(v.size());    //必须 开辟空间才能搬运
27 
28 
29     transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
30 
31     for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
32     {
33         cout << *it << " ";
34     }
35     cout << endl;
36 
37 
38 }
39 
40 int main()
41 {
42     test01();
43 
44 
45     return 0;
46 }

 

 

5.STL-常用算法

p237-STL-4-函数对象

 

 

 5.1常用遍历算法

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 //常用遍历算法 for_each
 7 
 8 //普通函数
 9 void print01(int val)
10 {
11     cout << val << " ";
12 
13 }
14 
15 //仿函数
16 class print02
17 {
18 public:
19     void operator()(int val)
20     {
21         cout << val << " ";
22     }
23 };
24 
25 void test01()
26 {
27     vector<int> v;
28     for (int i = 0; i < 10; i++)
29     {
30         v.push_back(i);
31     }
32 
33     for_each(v.begin(), v.end(), print01);
34     cout << endl;
35 
36     for_each(v.begin(), v.end(),print02());
37     cout << endl;
38 
39 }
40 
41 
42 int main()
43 {
44 
45     test01();
46 
47     return 0;
48 }

实际中非常好用

5.1.2transform

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 //常用遍历算法 transform
 7 
 8 
 9 class Transform
10 {
11 public:
12     int operator()(int v)
13     {
14         return v+100;
15     }
16 
17 };
18 
19 class myPrint
20 {
21 public:
22     void operator()(int val)
23     {
24         cout << val << " ";
25     }
26 
27 };
28 
29 
30 void test01()
31 {
32     vector<int> v;
33     for (int i = 0; i < 10; i++)
34     {
35         v.push_back(i);
36     }
37 
38     vector<int>vTarget;    //目标容器
39 
40     vTarget.resize(v.size());    //目标容器需要提前开辟空间
41 
42     transform(v.begin(), v.end(), vTarget.begin(), Transform());    //搬运
43 
44     for_each(vTarget.begin(), vTarget.end(), myPrint());    //打印
45     cout << endl;
46 
47 
48 }
49 
50 
51 int main()
52 {
53 
54     test01();
55 
56     return 0;
57 }

 

5.2常用查找算法

p237-STL-4-函数对象

 

 

 5.2.1find

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<string>
 5 #include<algorithm>
 6 
 7 //常用查找算法
 8 //find
 9 
10 //查找 内置数据类型
11 void test01()
12 {
13     vector<int>v;
14     for (int i = 0; i < 10; i++)
15     {
16         v.push_back(i);
17     }
18 
19     //查找 容器中 是否有 5 这个元素
20     vector<int>::iterator it =  find(v.begin(), v.end(), 5);
21     if (it == v.end())
22     {
23         cout << "没有找到!" << endl;
24     }
25     else
26     {
27         cout << "找到了:" << *it << endl;
28     }
29 
30 
31 }
32 
33 class Person
34 {
35 public:
36     Person(string name, int age)
37     {
38         this->m_Name = name;
39         this->m_Age = age;
40     }
41 
42     //重载== 底层find知道如何对比Person数据类型
43     bool operator==(const Person &p)
44     {
45         if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
46         {
47             return true;
48         }
49         else
50         {
51             return false;
52         }
53     }
54 
55     string m_Name;
56     int m_Age;
57 
58 };
59 
60 //查找 自定义数据类型
61 void test02()
62 {
63     vector<Person>v;
64     //创建数据
65     Person p1("aaa", 10);
66     Person p2("bbb", 20);
67     Person p3("ccc", 30);
68     Person p4("ddd", 40);
69 
70     //放入到容器中
71     v.push_back(p1);
72     v.push_back(p2);
73     v.push_back(p3);
74     v.push_back(p4);
75 
76     Person pp("bbb", 20);
77 
78     vector<Person>::iterator it = find(v.begin(), v.end(), pp);
79     if (it == v.end())
80     {
81         cout << "没有找到!" << endl;
82     }
83     else
84     {
85         cout << "找到元素    姓名:" << it->m_Name
86             << "  年龄:" << it->m_Age << endl;
87     }
88 
89 
90 }
91 
92 int main()
93 {
94     //test01();
95     test02();
96 
97     return 0;
98 }

 

5.2.2find_if

p237-STL-4-函数对象

 

 

 

  1 #include<iostream>
  2 using namespace std;
  3 #include<vector>
  4 #include<string>
  5 #include<algorithm>
  6 
  7 //常用查找算法 find_if
  8 
  9 //find
 10 
 11 //1、查找 内置数据类型
 12 
 13 class GreaterFive
 14 {
 15 public:
 16     bool operator()(int val)
 17     {
 18         return val > 5;
 19     }
 20 
 21 
 22 
 23 };
 24 
 25 void test01()
 26 {
 27     vector<int>v;
 28     for (int i = 0; i < 10; i++)
 29     {
 30         v.push_back(i);
 31     }
 32 
 33     //查找 容器中 是否有大于 5 的元素
 34     vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
 35     if (it == v.end())
 36     {
 37         cout << "没有找到!" << endl;
 38     }
 39     else
 40     {
 41         cout << "找到等于5的数字为:" << *it << endl;
 42     }
 43 
 44 
 45 }
 46 
 47 
 48 //2、查找自定义数据类型
 49 
 50 class Person
 51 {
 52 public:
 53     Person(string name, int age)
 54     {
 55         this->m_Name = name;
 56         this->m_Age = age;
 57     }
 58     string m_Name;
 59     int m_Age;
 60 };
 61 
 62 class Greater20
 63 {
 64 public:
 65     bool operator()(Person &p)
 66     {
 67         return p.m_Age > 20;
 68     }
 69 };
 70 
 71 
 72 void test02()
 73 {
 74     vector<Person>v;
 75 
 76     //创建数据
 77     Person p1("aaa", 10);
 78     Person p2("bbb", 20);
 79     Person p3("ccc", 30);
 80     Person p4("ddd", 40);
 81 
 82     v.push_back(p1);
 83     v.push_back(p2);
 84     v.push_back(p3);
 85     v.push_back(p4);
 86 
 87     //找年龄大于20的人
 88     vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
 89 
 90     if (it == v.end())
 91     {
 92         cout << "没有找到" << endl;
 93     }
 94     else
 95     {
 96         cout << "找到  姓名:" << it->m_Name
 97             << "  年龄:" << it->m_Age << endl;
 98     }
 99 
100 
101 }
102 
103 
104 int main()
105 {
106     //test01();
107     test02();
108 
109     return 0;
110 }

5.2.3

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 
 6 //常用查找算法 adjacent_find
 7 
 8 
 9 void test01()
10 {
11     vector<int>v;
12     v.push_back(0);
13     v.push_back(2);
14     v.push_back(0);
15     v.push_back(3);
16     v.push_back(1);
17     v.push_back(4);
18     v.push_back(3);
19     //v.push_back(3);
20 
21     vector<int>::iterator pos =  adjacent_find(v.begin(), v.end());
22 
23     if (pos == v.end())
24     {
25         cout << "未找到相邻重复元素" << endl;
26     }
27     else
28     {
29         cout << "找到相邻重复元素:" << *pos << endl;
30     }
31 }
32 int main()
33 {
34     test01();
35 
36 
37     return 0;
38 }

 

面试题中记得用

5.2.4binary_search

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<algorithm>
 4 #include<vector>
 5 
 6 //常用查找算法 binary_search
 7 
 8 
 9 void test01()
10 {
11     vector<int>v;
12     for (int i = 0; i < 10; i++)
13     {
14         v.push_back(i);
15     }
16     //v.push_back(2);//如果是无序序列,结果未知
17     //查找容器中是否有9 元素
18     //注意:容器必须是有序的序列
19     bool ret = binary_search(v.begin(), v.end(), 9);
20     if (ret)
21     {
22         cout << "找到了元素" << endl;
23     }
24     else
25     {
26         cout << "未找到" << endl;
27     }
28 
29 }
30 
31 int main()
32 {
33     test01();
34 
35     return 0;
36 
37 }

5.2.5count

p237-STL-4-函数对象

 

 

 

 1 #include<iostream>
 2 using namespace std;
 3 #include<vector>
 4 #include<algorithm>
 5 #include<string>
 6 
 7 //常用查找算法 count
 8 
 9 //1、统计内置数据类型
10 void test01()
11 {
12     vector<int>v;
13     v.push_back(10);
14     v.push_back(40);
15     v.push_back(30);
16     v.push_back(40);
17     v.push_back(20);
18     v.push_back(40);
19 
20     int num = count(v.begin(), v.end(), 40);
21 
22     cout << "40的元素个数为:" << num << endl;
23 
24 }
25 
26 
27 //2、统计自定义数据类型
28 
29 class Person
30 {
31 public:
32     Person(string name, int age)
33     {
34         this->m_Name = name;
35         this->m_Age = age;
36     }
37 
38     bool operator==(const Person &p)    //重载  在底层 必须加const
39     {
40         if (this->m_Age == p.m_Age)
41         {
42             return true;
43         }
44         else
45         {
46             return false;
47         }
48     }
49 
50     string m_Name;
51     int m_Age;
52 
53 };
54 
55 
56 void test02()
57 {
58     vector<Person>v;
59 
60     Person p1("刘备", 35);
61     Person p2("关羽", 35);
62     Person p3("张飞", 35);
63     Person p4("赵云", 30);
64     Person p5("曹操", 40);
65 
66     //将人员插入到容器中
67     v.push_back(p1);
68     v.push_back(p2);
69     v.push_back(p3);
70     v.push_back(p4);
71     v.push_back(p5);
72 
73 
74     Person p("诸葛亮", 35);
75 
76     int num = count(v.begin(), v.end(), p);
77 
78     cout << "和诸葛亮同岁数的人的个数为:" << num << endl;
79 
80 }
81 
82 
83 int main()
84 {
85     //test01();
86     test02();
87 
88     return 0;
89 
90 
91 }

对于自定义数据类型,需要配合重载运算符operator==

5.2.6c++ount_if

p237-STL-4-函数对象

 

 

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>

//常用查找算法 count_if



//统计内置数据类型
class Greater20
{
public:
    bool operator()(int val)
    {
        return val > 20;
    }

};

void test01()
{
    vector<int>v;

    v.push_back(10);
    v.push_back(40);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(20);

    //Pred:就是要提供谓词
    int num = count_if(v.begin(), v.end(), Greater20());
    
    cout << "大于 20的元素的个数为:" << num << endl;

}

//统计自定义数据类型
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

    string m_Name;
    int m_Age;

};

class AgeGreater20
{
public:
    bool operator()(const Person & p)
    {
        return p.m_Age > 20;
    }

};



void test02()
{
    vector<Person>v;

    Person p1("刘备", 35);
    Person p2("关羽", 35);
    Person p3("张飞", 35);
    Person p4("赵云", 40);
    Person p5("曹操", 20);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    //统计 大于20岁的人员的个数
    int num = count_if(v.begin(), v.end(), AgeGreater20());

    cout << "大于20岁的人员的个数为:" << num << endl;



}


int main()
{
    //test01();
    test02();


    return 0;


}

 

5.3常用排序算法

p237-STL-4-函数对象

 

 

5.3.1sort

p237-STL-4-函数对象

 

 

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
#include<functional>

//常用排序算法 sort

void myPrint(int val)
{
    cout << val<<" ";
}


void test01()
{
    vector<int>v;

    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);

    //利用sort进行升序排列
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    //改变为 降序
    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

}

int main()
{

    test01();


    return 0;
}

sort()默认从小到大排序

要变为从打到小  可以  greater<int>()作为第三个参数

 

 

 

 

 

 

 

#include<iostream>using namespace std;#include<vector>#include<algorithm>#include<string>
//常用查找算法 count
//1、统计内置数据类型void test01(){vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(40);
int num = count(v.begin(), v.end(), 40);
cout << “40的元素个数为:” << num << endl;
}

//2、统计自定义数据类型
class Person{public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}
bool operator==(const Person &p)//重载  在底层 必须加const{if (this->m_Age == p.m_Age){return true;}else{return false;}}
string m_Name;int m_Age;
};

void test02(){vector<Person>v;
Person p1(“刘备”, 35);Person p2(“关羽”, 35);Person p3(“张飞”, 35);Person p4(“赵云”, 30);Person p5(“曹操”, 40);
//将人员插入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);

Person p(“诸葛亮”, 35);
int num = count(v.begin(), v.end(), p);
cout << “和诸葛亮同岁数的人的个数为:” << num << endl;
}

int main(){//test01();test02();
return 0;

}


程序员灯塔
转载请注明原文链接:p237-STL-4-函数对象
喜欢 (0)