c++进阶-STL常用算法

2019-03-17  本文已影响0人  Peakmain

预定义函数

c/c++提供了很多定义好的函数对象,常见的有less,greater(降序),plus(相加),equal_to,find_if

#include<algorithm>
void main(){
    //c/c++提供了很多定义好的函数对象
    //常见的less,greater(降序),plus(相加),equal_to,
    plus<string> strAdd;
    string str=strAdd("Peakmain", "25");
    cout << str.c_str() << endl;

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

    //判断是否含有aaa
    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;
    }
    else{
        cout << "没有找到" << endl;
    }
    getchar();
}

函数适配器

正如上面调用的方法bind2nd,这个就是一个适配器,看下它的定义

template<class _Fn2,
    class _Ty> inline
    binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right)
    {   // return a binder2nd functor adapter
    typename _Fn2::second_argument_type _Val(_Right);
    return (_STD binder2nd<_Fn2>(_Func, _Val));
    }

上面的实际就是将_Right对象转换成aaa
再看个案例

//自定义仿函数(函数对象)
class Equal{
public:
    bool operator()(const int &number){
        return number == 3;
    }
};
void main(){
    vector<int> vector1;
    vector1.push_back(3);
    vector1.push_back(5);
    vector1.push_back(6);
    vector1.push_back(2);
    vector1.push_back(9);
    vector1.push_back(3);
    //第一种方式:自定义仿函数 找集合中等于3的个数
    int count = count_if(vector1.begin(), vector1.end(), Equal());
    //第二种方式:预定义好的函数+函数适配器
    int count1 = count_if(vector1.begin(), vector1.end(), bind2nd(equal_to<int>(),2));
    cout << count << endl;
    getchar();
}

常用预定义算法,循环,增,删,改,查

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

int transform_print(int number){
    return number-2;
}
void main(){
    vector<int> vector1;
    vector1.push_back(3);
    vector1.push_back(5);
    vector1.push_back(6);
    vector1.push_back(2);
    vector1.push_back(9);

    vector<int> vector2;
    vector2.resize(vector1.size());
    //第三个参数的意思是:将数据添加到vector2的集合中
    transform(vector1.begin(), vector1.end(),vector2.begin(), transform_print);
    for_each(vector1.begin(), vector1.end(), print);
    getchar();
}

find

void main(){
    vector<int> vector1;
    vector1.push_back(3);
    vector1.push_back(5);
    vector1.push_back(6);
    vector1.push_back(2);
    vector1.push_back(9);
    vector<int>::iterator it= find(vector1.begin(), vector1.end(), 2);
    if (it != vector1.end()){
        cout << "包含" << endl; 
    }
    else{
        cout << "不包含" << endl;
    }
    getchar();
}

count count_if

void main(){
    vector<int> vector1;
    vector1.push_back(3);
    vector1.push_back(5);
    vector1.push_back(6);
    vector1.push_back(2);
    vector1.push_back(9);
    //等于2的个数
    int number = count(vector1.begin(), vector1.end(), 2);
    cout << "等于2的个数:" << number << endl;
    number = count_if(vector1.begin(), vector1.end(), bind2nd(less<int>(), 5));
    cout << "小于5的个数:" <<number<< endl;
    number = count_if(vector1.begin(), vector1.end(), bind2nd(greater<int>(), 5));
    cout << "大于5的个数:" << number << endl;
    getchar();
}

merge将两个数进行合并

void print(int number){
    cout << number << endl;
}
void 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);
    getchar();
}

sort排序

void print(int number){
    cout << number << endl;
}
void main(){
    vector<int> vector1;
    vector1.push_back(4);
    vector1.push_back(2);
    vector1.push_back(3);

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

random_shuffle打乱循序

void print(int number){
    cout << number << endl;
}
void main(){
    vector<int> vector1;
    vector1.push_back(1);
    vector1.push_back(2);
    vector1.push_back(3);
    vector1.push_back(4);
    //打乱循序
    random_shuffle(vector1.begin(), vector1.end());
    for_each(vector1.begin(), vector1.end(), print);
    getchar();
}

copy

void print(int number){
    cout << number << endl;
}
void main(){
    vector<int> vector1;
    vector1.push_back(1);
    vector1.push_back(2);
    vector1.push_back(3);
    vector<int> vector2;
    vector2.resize(3);
    copy(vector1.begin(), vector1.end() ,vector2.begin());
    for_each(vector2.begin(), vector2.end(), print);
    getchar();
}

replace

void print(int number){
    cout << number << endl;
}
void main(){
    vector<int> vector1;
    vector1.push_back(1);
    vector1.push_back(2);
    vector1.push_back(3);

    replace(vector1.begin(), vector1.end(),2,22);
    for_each(vector1.begin(), vector1.end(), print);
    getchar();
}
上一篇下一篇

猜你喜欢

热点阅读