STL 算法库algorithm

文章目录

      • 前言
      • 一.for_each
      • 二.count_if
      • 三.find_if
      • 四.unique
      • 五.sort 与 std::less std::greater
      • 六.remove_if
      • 七.move
      • 八.copy copy_if
      • 九.merge


前言

有了 #include 这样的头文件,可以用这个库来帮助我们完成算法,下面主要介绍algotithm里面的一些常用的算法,再学其他的一通百通。

一.for_each

用普通函数打印出数组中的奇数,用函数对象打印出数组中的偶数
注意,for_each每一次的循环中都相当于将printOdd(*it)调用,object(*it)调用

//函数
void printOdd(int Elem)
{
    if (Elem % 2 == 1)
    {
        cout << Elem << " ";
    }
}

class PrintEven
{
public:
    void operator()(int Elem)
    {
        if (Elem % 2 == 0)
            cout << Elem << " ";
    }
} object;

void test1()
{
    vector<int> vec{1, 2, 3, 4, 5, 6};
    for_each(vec.begin(), vec.end(), printOdd);
    cout << endl;

    for_each(vec.begin(), vec.end(), PrintEven());
    cout << endl;

}

二.count_if

用普通函数找出奇数的个数, 用函数对象找出偶数的个数

bool countOdd(int Elem){
    if (Elem % 2 == 1){
        return true;
    }
    return false;
}

class CountEven{
public:
    bool operator()(int Elem)
    {
        if (Elem % 2 == 0)
            return true;
        return false;
    }
}countObject;

void test2(){
    vector<int> vec{1,2,3,4,5,6,7,8};
    cout << "The number of odds is " << count_if(vec.begin(), vec.end(), countOdd) << endl;;
    cout << "The number of evens is " << count_if(vec.begin(), vec.end(), countObject) << endl;;
}


三.find_if


bool findOdd(int Elem){
    if (Elem % 2 == 1)
        return true;
    return false;
}

class Find9{
public:
    bool operator()(int Elem){
        if (Elem == 9)
            return true;
        return false;
    }
}findObject;

void test3(){
    vector<int> vec{1,2,3,4,5,6,7,8};
    auto it = find_if(vec.begin(), vec.end(), findOdd);
    cout << "find odd : " << *it << endl;

    auto it2 = find_if(vec.begin(), vec.end(), findObject);
    if (it2 != vec.end())
        cout << "there is 9" << endl;
    else 
        cout << "there isn't 9 " << endl;
}

四.unique

unique这里用到了算法中双指针的思想,返回值注意是前指针后移一位的结果。


bool Compare(int x, int y){
    return !(x == y);
}

void test4(){
    vector<int> vec{1,1,1,2,2,3,3,3};
    auto it = unique(vec.begin(), vec.end());
    
    for (auto& e: vec)
        cout << e << endl;
    cout << "--------------------" << endl;


    vec.erase(it, vec.end());
    
    for (auto& e: vec)
        cout << e << endl;
    cout << "--------------------" << endl;
    
    vector<int> vec2{1,1,1,2,2,3,3,3};
    auto it2 = unique(vec2.begin(), vec2.end(), Compare);
    cout << it2-vec2.begin() << endl;//distance = 3 与上一个完全相同
    
}


五.sort 与 std::less std::greater


template<typename T>
bool compare(const T& lhs, const T& rhs){
    return lhs > rhs;
}

template<typename T>
class SortClass{
public:
    bool operator()(const T& lhs, const T& rhs){
        return lhs < rhs;
    }
};

void test5(){
    vector<int> vec{2,1,3,4,5,3};
    sort(vec.begin(), vec.end(), compare<int>);

    for (auto& e: vec)
        cout << e << endl;
    cout << "------------" << endl;

    sort(vec.begin(), vec.end(), SortClass<int>());
    for (auto& e: vec)
        cout << e << endl;
    cout << "------------" << endl;

    sort(vec.begin(), vec.end(), std::greater<int>());
    for (auto& e: vec)
        cout << e << endl;
    cout << "------------" << endl;

}


六.remove_if

删除数组中的奇数


bool isOdd(int Elem){
    if (Elem % 2 == 1)
        return true;
}

void test6(){
    vector<int> vec{1,2,3,4,5,6};
    auto it = remove_if(vec.begin(), vec.end(), isOdd);
    vec.erase(it, vec.end());

    for (auto& e : vec)
        cout << e << endl;
}

七.move

注意在move的时候需要提前扩容,不然会出错

void test7(){
    vector<int> vec1{1,2,3};
    vector<int> vec2(3);
    std::move(vec1.begin(), vec1.end(), vec2.begin());
    
    for (auto& e :vec2)
        cout << e << endl;

    vector<int> vec3;
    vec3 = std::move(vec2);

    for (auto& e: vec3)
        cout << e << endl;


八.copy copy_if

bool Copy_isOdd(int Elem){
    if (Elem % 2 == 1)
        return true;
}

void test8(){
    vector<int> vec1{1,2,3,4,5,6};
    vector<int> vec2(6);
    std::copy(vec1.begin(), vec1.end(), vec2.begin());
    for (auto& e: vec2)
        cout << e << endl;

    //only copy odd
    vector<int> vec3(4);
    std::copy_if(vec1.begin(), vec1.end(), vec3.begin(), Copy_isOdd);

    for (auto& e: vec3)
        cout << e << endl;
}

九.merge

这个merge与归并排序相同

template<typename T>
bool mergeCompare(const T& lhs, const T& rhs){
    return lhs < rhs;
}

void test9(){
    vector<int> vec1{3,1,4};
    vector<int> vec2{2,6,5};
    vector<int> vec3(6);
    std::merge(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), vec3.begin(), mergeCompare<int>);
    
    for (auto & e : vec3)
        cout << e << endl;

}


你可能感兴趣的:(STL)