stl算法

作者: arkliu | 来源:发表于2022-12-04 09:37 被阅读0次

for_each

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

template<class T>
void show(const T& val) {
    cout <<"value="<< val << endl;
}

template<class T>
class Myprint {
    public:
        void operator()(const T& val) {
            cout <<"value="<< val << endl;
        }
};

template<class T,class T2>
void my_for_each(const T first, const T last, T2 show) {
    for (auto it=first; it != last; it++)
    {
        show(*it);
    }
}

int main() {
    list<string> ls = {"aa","bb","cc","dd","ee","ff"};
    my_for_each(ls.begin(), ls.end(),show<string>);

    Myprint<string> myPrint;
    my_for_each(ls.begin(), ls.end(),myPrint);
    return 0;
}

std::find

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        int id;
        string name;

        bool operator==(const C1 c) {
            return c.id == this->id && c.name == this->name;
        }
};

void fun() {
    vector<int>v1{1,22,33,4,5};
    auto rs1 = find(v1.begin(),v1.end(), 33);
    if (rs1 == v1.end())
    {
        cout << "没找到.."<<endl;
    } else {
        cout <<*rs1<<endl;
    }

    vector<C1>v2{C1(123,"tom"),C1(345, "jerry")};
    auto rs2 = find(v2.begin(),v2.end(), C1(345, "jerry"));
    if (rs2 == v2.end())
    {
        cout << "没找到.."<<endl;
    } else {
        cout <<(*rs2).id<<"---"<<(*rs2).name<<endl;
    }
}

std::find_if

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

template<class T, class T2>
class Myprint {
    public:
        T2 m_val;
        bool operator()(const T& val) {
            if (val == m_val)
            {
                return true;
            }
            return false;
        }
};

template<class T,class T2>
T myfind_if(const T first, const T last, T2 show) {
    for (auto it=first; it != last; it++)
    {
        if (show(*it) == true)
        {
            return it;
        }
        return last;
    }
}

int main() {
    list<string> ls = {"ada","bb","cc","dd","ee","ff"};
    Myprint<string,string> myPrint;
    myPrint.m_val = "aa";
    auto it2 = myfind_if(ls.begin(), ls.end(),myPrint);
    if (it2 == ls.end())
    {
        cout <<"查找失败"<<endl;
    } else {
        cout <<"查找成功"<<endl;
    }
    return 0;
}

std::sort

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

int main()
{
    vector<int> v = {9,5,2,6,7,0,4,1,3,8}; 
 
    // 1. 默认升序排列
    std::sort(v.begin(), v.end());
    for (auto a : v) {
        cout << a << " ";
    }   
    cout << endl;

    //2. 用标准库比较函数对象排序
    std::sort(v.begin(), v.end(), std::greater<int>());
    for (auto a : v) {
        std::cout << a << " ";
    }   
    cout << endl;
 
    //3. 用 lambda 表达式排序
    std::sort(v.begin(), v.end(), [](int a, int b) {
        return b < a;   
    });
    for (auto a : v) {
        cout << a << " ";
    } 
    cout << endl;
}

统计算法

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

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        int id;
        string name;

        bool operator==(const C1 c) {
            return c.id == this->id && c.name == this->name;
        }
};

void fun() {
    vector<int>v1{1,22,33,22,4,5};
    int rs1 = count(v1.begin(),v1.end(), 22);
    cout << rs1<<endl;

    // 统计自定义数据类型
    vector<C1>v2{C1(123,"tom"),C1(345, "jerry"),C1(345, "jerry")};
    int rs2 = count(v2.begin(),v2.end(), C1(345, "jerry"));
    cout << rs2<<endl;
}

struct myFind1 {
    bool operator()(int a) {
        return a == 22;
    }
};

struct myFind2 :public unary_function<int,bool> {
    bool operator()(int a) const{
        return a == 22;
    }
};

struct myFind3 {
    bool operator()(const C1 &a) {
        return a.id == 345 && a.name == "jerry";
    }
};

void fun1() {
    // 统计等于22的元素个数
    vector<int>v1{1,22,22,22,33,4,5};
    int rs1 = count_if(v1.begin(), v1.end(), myFind1());
    cout << rs1<<endl;

    // 统计不等于22的元素个数
    int rs2 = count_if(v1.begin(), v1.end(), not1(myFind2()));
    cout << rs2<<endl;

    vector<C1>v2{C1(123,"tom"),C1(345, "jerry"),C1(345, "jerry")};
    int rs3 = count_if(v2.begin(),v2.end(), myFind3());
    cout << rs3<<endl;
}

合并算法

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

void fun() {
    vector<int>v1{1,2,3,4};
    vector<int>v2{3,4,5,6};
    vector<int>v3;
    v3.resize(v1.size()+v2.size());
   
    // merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin()); 默认升序合并
    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin(), greater<int>());
    for(auto start = v3.begin(); start!=v3.end(); start++) {
        cout <<*start<<"  ";
    }
}

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        int id;
        string name;
};

struct myCompare {
    bool operator()(const C1 &a, const C1 &b) {
        return a.id < b.id;
    }
};

// 自定义类型合并
void fun2() {
    vector<C1>v1{C1(1,"tom1"),C1(2, "tom2"),C1(3, "tom3"),C1(4, "tom4")};
    vector<C1>v2{C1(3,"jerry3"),C1(4,"jerry4"),C1(5,"jerry5"),C1(6,"jerry6")};
    vector<C1>v3;
    v3.resize(v1.size()+v2.size());

    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin(),myCompare());
    for(auto start = v3.begin(); start!=v3.end(); start++) {
        cout <<(*start).id<<" : "<<(*start).name<<"  ";
    }
}

int main() {
    fun2();
    return 0;
}
image.png

随机算法(洗牌算法)

#include <iostream>
#include<string>
#include <vector>
#include <sys/time.h>
#include <algorithm> // std::shuffle
#include <random> // std::default_random_engine
#include<chrono>
using namespace std; 

struct myRand{
    int operator()(int i) {
        return rand()%i;
    }  
};

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        int id;
        string name;
};

void fun() {
    srand(((unsigned int)time(NULL)));//随机数种子

   vector<int>v1{1,2,3,4,5,6};
   random_shuffle(v1.begin(), v1.end(), myRand());

   for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
    
    cout <<endl;
    vector<C1>v2{C1(1,"11"),C1(2, "22"),C1(3, "33"),C1(4, "44"),C1(5, "55"),C1(6, "66")};
    random_shuffle(v2.begin(), v2.end(), myRand());
    for(auto start = v2.begin(); start!=v2.end(); start++) {
        cout <<(*start).id<<" : "<<(*start).name<<"  ";
    }
}

void fun2() {
   vector<int>v1{1,2,3,4,5,6};
   random_device rd;
   unsigned seed = chrono::system_clock::now().time_since_epoch().count();
   shuffle(v1.begin(),v1.end(),default_random_engine(seed));
   for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
}

int main() {
    fun2();
    return 0;
}

排序算法

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

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        int id;
        string name;
};

struct mySort{
    bool operator()(const C1 &a,const C1 &b) {
        return a.id > b.id;
    }
};

void fun2() {
   vector<int>v1{12,21,33,41,15,26};
//    sort(v1.begin(), v1.end()); // 默认升序排列
   sort(v1.begin(), v1.end(), greater<int>()); // 降序排列
   for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
    cout << endl;
    // 自定义数据类型
    vector<C1>v2{C1(1,"tom1"),C1(2, "tom2"),C1(3, "tom3"),C1(4, "tom4"),
                C1(3,"jerry3"),C1(4,"jerry4"),C1(5,"jerry5"),C1(6,"jerry6")};
    sort(v2.begin(), v2.end(),mySort()); // 降序排列
    for(auto start = v2.begin(); start!=v2.end(); start++) {
        cout <<(*start).id<<":"<<(*start).name<<"  ";
    }
}

int main() {
    fun2();
    return 0;
}
image.png

反转算法

void fun2() {
   vector<int>v1{12,21,33,41,15,26};
   reverse(v1.begin(), v1.end());
   for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
}
image.png

算数生成算法

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

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        int id;
        string name;
};

struct myPlus{
    int operator()(int a, const C1 &b) {
        return a+b.id;
    }
};

void fun1() {
    vector<int>v1{1,2,3};
    // accumulate默认是加法
    auto rs = accumulate(v1.begin(), v1.end(), 10); // 10+1+2+3
    // auto rs = accumulate(v1.begin()+1, v1.end(), 10); // 10+2+3
    cout <<"rs:"<<rs<<endl;

    // 减法运算
    auto rs1 = accumulate(v1.begin(), v1.end(), 10, minus<int>()); // 10-1-2-3
    cout <<"rs1"<<rs1<<endl;

     // 乘法运算
    auto rs2 = accumulate(v1.begin(), v1.end(), 1, multiplies<int>()); // 1*1*2*3
    cout <<"rs2:"<<rs2<<endl;

    //自定义的数据类型
    vector<C1>v2 = {
        C1(1,"1"),
        C1(2,"2"),
        C1(3,"3"),
    };
    auto rs3 = accumulate(v2.begin(), v2.end(), 10, myPlus()); 
    cout <<"rs3:"<<rs3<<endl;
}

int main() {
    fun1();
    return 0;
}
image.png

填充算法

void fun2() {
    vector<int>v1(5); //空间代大小为5个
    fill(v1.begin(), v1.end(), 100);
    for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
    cout <<endl;

    vector<C1>v2;
    v2.resize(3);
    fill(v2.begin(), v2.end(), C1(1,"11"));
    for(auto start = v2.begin(); start!=v2.end(); start++) {
        cout <<(*start).id<<"  "<<(*start).name<<endl;
    }
}
image.png

集合算法

源容器元素都必须是有序的,且排序规则要保持一致,目标容器使用前要分配空间

  • 取交集
  • 取并集
  • 取差集
#include <iostream>
#include<string>
#include <vector>
#include <algorithm>
#include<numeric>
using namespace std; 

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        int id;
        string name;
};

struct myCompare{
    bool operator()(const C1 &a, const C1 &b) {
        return a.id < b.id;
    }
};

void fun1() {
    vector<int>v1{1,2,3,4,5};
    vector<int>v2{4,5,6,7,8,9};
    vector<int>v3;
    v3.resize(min(v1.size(), v2.size()));// 取交集最大个数为v1和v2里较小的元素个数

    set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    for(auto start = v3.begin(); start!=v3.end(); start++) {
        cout <<*start<<"  ";
    }
    cout <<endl;
    //自定义对象
    vector<C1>v4{C1(1,"1"),C1(2,"2"),C1(3,"3"),C1(4,"4"),C1(5,"5")};
    vector<C1>v5{C1(4,"4"),C1(5,"5"),C1(6,"6"),C1(7,"7"),C1(8,"8")};
    vector<C1>v6;
    v6.resize(min(v4.size(), v5.size()));

    set_intersection(v4.begin(), v4.end(), v5.begin(), v5.end(), v6.begin(),myCompare());
    for(auto start = v6.begin(); start!=v6.end(); start++) {
        cout <<(*start).id<<"  "<<(*start).name<<endl;
    }
}

int main() {
    fun1();
    return 0;
}
image.png

拷贝算法

copy只负责复制,不负责申请空间,所以复制前必须有足够的空间

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

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        int id;
        string name;
};

void fun2() {
   vector<int>v1{12,21,33,41,15,26};
   vector<int>v2;
   v2.resize(v1.size());
   std::copy(v1.begin(), v1.end(), v2.begin());
    for(auto start = v2.begin(); start!=v2.end(); start++) {
        cout <<*start<<"  ";
    }
    cout << endl;
    // 自定义数据类型
    vector<C1>v3{C1(1,"1"),C1(2,"2"),C1(3,"3"),C1(4,"4"),C1(5,"5")};
    vector<C1>v4;
    v4.resize(v3.size());
    std::copy(v3.begin(), v3.end(), v4.begin());
    for(auto start = v4.begin(); start!=v4.end(); start++) {
        cout <<(*start).id<<"  "<<(*start).name<<endl;
    }
}

int main() {
    fun2();
    return 0;
}
image.png

替换算法

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

class C1{
    public:
        C1(int id,string name) {
            this->id = id;
            this->name = name;
        }
        C1(){}
        bool operator==(const C1 &a) {
            return a.id == this->id && a.name == this->name;
        }
        int id;
        string name;
};

// 将对应的值替换为指定的值
void fun1() {
    vector<int>v1{12,21,33,41,33,26};
    replace(v1.begin(), v1.end(), 33, 1000); // 替换v1中的33为100
    for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
    cout << endl;

    //自定义类型
    vector<C1>v2{C1(1,"1"),C1(1,"1"),C1(3,"3"),C1(4,"4"),C1(5,"5")};
    replace(v2.begin(), v2.end(), C1(1,"1"), C1(100, "100")); 
    for(auto start = v2.begin(); start!=v2.end(); start++) {
        cout <<(*start).id<<"  "<<(*start).name<<endl;
    }
}

struct myCompare :public binary_function<int, int ,bool>{
    bool operator()(int a, int b) const{
        return a < b;
    }
};

// 将满足条件的值替换为指定的值
void fun2() {
    vector<int>v1{12,21,33,41,33,26};
    replace_if(v1.begin(), v1.end(), bind2nd(myCompare(), 33), 1000); 
    for(auto start = v1.begin(); start!=v1.end(); start++) {
        cout <<*start<<"  ";
    }
    cout << endl;
    
}

int main() {
    fun2();
    return 0;
}

相关文章

网友评论

      本文标题:stl算法

      本文链接:https://www.haomeiwen.com/subject/efknxdtx.html