C++:11.C++中常用函数对象

源码阅读和追踪

仿函数

必须重载()运算符

1
2
3
4
5
6
7
8
9
10
class Equal{
private:
int equal_number;
public:
Equal(int equal_number):equal_number(equal_number){}

bool operator()(const int& number){
return equal_number == number;
}
};

1. 预定义函数对象和函数适配器(bind2nd)

c/c++ 提供了很多定义好的函数对象,常见的几个 less, greater, plus, equal_to,这些都是二元函数

bind1st和bind2nd是将二元函数转换为一元函数,比如一个比较大小的函数是二元函数。 当在某些情况下我们想要固定第一个参数或者第二个参数时,就成了一元函数

  • bind2nd(equal_to<string>(),"aaa")
  • set<string> :: iterator find_it = find_if(set1.begin(),set1.end(),bind2nd(equal_to<string>(),"aaa"));// 查找是否包含
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    int main(){
    // c/c++ 提供了很多定义好的函数对象
    // 常见的几个 less, greater , plus , equal_to
    plus<string> strAdd;
    string str = strAdd("aaa","bbb");

    // cout << str.c_str() << endl; // aaabbb

    set<string,greater<string>> set1;
    set1.insert("aaa");
    set1.insert("bbb");
    set1.insert("ccc");

    // 判断是不是包含 aaa
    // 怎么写仿函数,一定要确定好你的仿函数的参数
    // bind2nd 函数适配器, aaa 相当于 equal_to 中的 right

    set<string> :: iterator find_it = find_if(set1.begin(),set1.end(),bind2nd(equal_to<string>(),"aaa"));
    if(find_it!=set1.end()){
    cout << "找到了" << (*find_it).c_str() << endl;
    }
    }

2.自定义仿函数(函数对象) 和 count_if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Equal{
private:
int equal_number;
public:
Equal(int equal_number):equal_number(equal_number){}

bool operator()(const int& number){
return equal_number == number;
}
};

// count_if
int main(){
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
vector1.push_back(2);
vector1.push_back(4);
vector1.push_back(2);

// 找集合中 等于 2 的个数
int count = count_if(vector1.begin(),vector1.end(),Equal(2));
cout << "count = " << count << endl;

// 预定义好的函数对象 + 函数适配器
count = count_if(vector1.begin(),vector1.end(),bind2nd(equal_to<int>(),2));
cout << "count = " << count << endl;
}

打印结果:

1
2
count = 3
count = 3

3. foreach, transform

  • transform(vector1.begin(),vector1.end(),vector2.begin(),transform_print);:修改
  • for_each(vector2.begin(),vector2.end(),print);:循环遍历元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void print(int number){
cout << number << endl;
}

// 进行修改
int transform_print(int number){
// cout << number << endl;
return number + 3;
}

int main(){
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
vector1.push_back(4);

// for_each(vector1.begin(),vector1.end(),print);
vector<int> vector2;
vector2.resize(vector1.size());

transform(vector1.begin(),vector1.end(),vector2.begin(),transform_print);

for_each(vector2.begin(),vector2.end(),print);
}

打印结果:

1
2
3
4
4
5
6
7

4. find, find_if

  • find(first, end, value);:返回区间[first,end)中第一个值等于value的元素的位置。如果没有找到匹配元素,则返回end。(只能判断等于)
  • find_if (begin, end, func):从begin开始 ,到end为止,返回第一个让 func这个函数返回true的iterator。(所有判断都可以)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
vector1.push_back(4);

vector<int>::iterator find_it = find(vector1.begin(),vector1.end(),2);
if(find_it!=vector1.end()){
cout << "包含" << endl;
}else{
cout << "不包含" << endl;
}

// 有没有大于2的,自定义函数对象,预定义函数对象+函数适配器,省略
vector<int>::iterator find_it1 = find_if(vector1.begin(),vector1.end(),bind2nd(less<int>(),4));
while(find_it1!=vector1.end()){
if(*find_it1>=4){
find_it1++;
continue;
}
cout << *find_it1 << endl;
find_it1++;
}

打印结果:

1
2
3
4
包含
1
3
2

5. count,count_if

  • count(first, end, value);:返回区间[first,end)中值等于value的元素的个数。(只能判断等于)
  • count_if(begin, end, func):从begin开始 ,到end为止,返回让 func这个函数返回true的元素个数。(所有判断都可以)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
vector1.push_back(2);
vector1.push_back(4);

int number = count(vector1.begin(),vector1.end(),2);

cout << "等于2的个数:" << number << endl;

number = count_if(vector1.begin(),vector1.end(),bind2nd(less<int>(),2));

cout << "小于2的个数:" << number << endl;

number = count_if(vector1.begin(),vector1.end(),bind2nd(greater<int>(),2));

cout << "大于2的个数:" << number << endl;

打印结果:

1
2
3
等于2的个数:2
小于2的个数:1
大于2的个数:2

6. merge

  • merge(vector1.begin(),vector1.end(),vector2.begin(),vector2.end(),vector3.begin());:合并元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class _merge{
public:
bool operator()(int number1,int number2){
return true;
}
};

void print(int number){
cout << number << endl;
}

// merge
int main(){
// 两个有序数组进行合并 - 归并排序
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);

vector<int> vector2;
vector2.push_back(4);
vector2.push_back(5);
vector2.push_back(6);

vector<int> vector3;
vector3.resize(6);
merge(vector1.begin(),vector1.end(),vector2.begin(),vector2.end(),vector3.begin());

for_each(vector3.begin(),vector3.end(),print);
}

打印结果:

1
2
3
4
5
6
1
2
3
4
5
6

7. sort

  • sort(vector1.begin(),vector1.end(),less<int>());:按照从小到大的方式排序
  • random_shuffle(vector1.begin(),vector1.end());:打乱 vector1的顺序
1
2
3
4
5
6
7
8
9
10
11
12
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(3);
vector1.push_back(2);
vector1.push_back(4);

sort(vector1.begin(),vector1.end(),less<int>());
for_each(vector1.begin(),vector1.end(),print);

// 打乱顺序
random_shuffle(vector1.begin(),vector1.end());
for_each(vector1.begin(),vector1.end(),print);

8. copy,replace

  • copy(vector1.begin(),vector1.begin()+2,vector2.begin());:将vector1的前两位拷贝到vector2中,从vector2的开始位置拷入。
  • replace(vector1.begin(),vector1.end(),2,22);:将vector1中等于2的值替换成22
1
2
3
4
5
6
7
8
9
10
11
12
vector<int> vector1;
vector1.push_back(1);
vector1.push_back(2);
vector1.push_back(3);
vector1.push_back(4);

vector<int> vector2(2);
copy(vector1.begin(),vector1.begin()+2,vector2.begin());
// for_each(vector2.begin(),vector2.end(),print);

replace(vector1.begin(),vector1.end(),2,22);
for_each(vector1.begin(),vector1.end(),print);
-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%