«

5 STL-常用算法

时间:2023-10-31 21:29     作者:wen     分类: C++


[toc]

STL-常用算法

概述:

常用遍历算法

学习目标:

算法简介:

for_each

功能描述:

函数原型:

示例:

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

//普通函数
void printA(int val)
{
    cout << val << " ";
}
//仿函数
class PrintB 
{
public:
    void operator()(int val) {
        cout << val << " ";
    }
};

//for_each遍历
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //普通函数
    for_each(v.begin(), v.end(), printA);
    cout << endl;
    //仿函数
    for_each(v.begin(), v.end(), PrintB());
    cout << endl;
    //匿名函数
    for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
}

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

transform

功能描述:

函数原型:

示例:

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

//仿函数
class Transform 
{
public:
    int operator()(int val) {
        return val + 10;
    }
};

class MyPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

//transform
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    vector<int> vTarge;//目标容器
    vTarge.resize(v.size());//目标容器需要提前开辟空间
    transform(v.begin(), v.end(), vTarge.begin(), Transform());
    for_each(vTarge.begin(), vTarge.end(), MyPrint());
}

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


常用查找算法

学习目标:

算法简介:

find

功能描述

函数原型:

示例:

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

//find 查找
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vector<int>::iterator pos = find(v.begin(), v.end(), 4);
    if (pos != v.end())
    {
        cout << *pos << endl;
    }
    else {
        cout << "没找到" << endl;
    }
}

//自定义数据类型
class Person
{
public:
    string m_name;
    int m_age;

    Person(const string& m_name, int m_age)
        : m_name(m_name), m_age(m_age)
    {
    }

    // 重载 == 底层find知道如何对比Person数据类型
    bool operator==(const Person& other) const
    {
        return m_name == other.m_name && m_age == other.m_age;
    }
};
void testB()
{
    vector<Person> v;
    Person p1("唐僧", 28);
    Person p2("孙悟空", 999);
    Person p3("沙和尚", 500);
    Person p4("猪八戒", 666);

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

    Person p5("孙悟空", 999);

    vector<Person>::iterator pos = find(v.begin(), v.end(), p5);
    if (pos == v.end())
    {
        cout << "没找到" << endl;
    }
    else {
        cout << "姓名: " << pos->m_name << " 年龄: " << pos->m_age << endl;
    }
}

int main()
{
    testA();
    testB();
    return 0;
}


find_if

功能描述:

函数原型:

示例:

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

class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};

//find_if 查找
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vector<int>::iterator pos = find_if(v.begin(), v.end(), GreaterFive());
    if (pos != v.end())
    {
        cout << *pos << endl;
    }
    else {
        cout << "没找到" << endl;
    }
}

//自定义数据类型
class Person
{
public:
    string m_name;
    int m_age;

    Person(const string& m_name, int m_age)
        : m_name(m_name), m_age(m_age)
    {
    }

    // 重载 == 底层find知道如何对比Person数据类型
    bool operator==(const Person& other) const
    {
        return m_name == other.m_name && m_age == other.m_age;
    }
};

class Greater300
{
public:
    bool operator()(Person& p)
    {
        return p.m_age > 300;
    }
};

void testB()
{
    vector<Person> v;
    Person p1("唐僧", 28);
    Person p2("孙悟空", 999);
    Person p3("沙和尚", 500);
    Person p4("猪八戒", 666);

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

    vector<Person>::iterator pos = find_if(v.begin(), v.end(), Greater300());
    if (pos == v.end())
    {
        cout << "没找到" << endl;
    }
    else {
        cout << "姓名: " << pos->m_name << " 年龄: " << pos->m_age << endl;
    }
}

int main()
{
    testA();
    testB();
    return 0;
}


adjacent_find

功能描述:

函数原型:

示例:

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

//adjacent_find 查找
void testA()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(20);
    v.push_back(10);

    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
    if (pos != v.end())
    {
        cout << *pos << endl;
    }
    else {
        cout << "没找到" << endl;
    }
}

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


binary_search

功能描述:

函数原型:

示例:

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

//binary_search  查找
void testA()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //v.push_back(2); //如果是无序序列,结果未知!

    //注意: 容器必须是一个有序的序列
    bool flag = binary_search(v.begin(), v.end(), 9);
    if (flag)
    {
        cout << "找到" << endl;
    }
    else {
        cout << "未找到" << endl;
    }
}

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


count

功能描述:

函数原型:

示例:

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

//count  统计
void testA()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    int num = count(v.begin(), v.end(), 10);
    cout << num << endl;
}

//自定义数据类型
class Person
{
public:
    string m_name;
    int m_age;
public:

    Person(const string& m_name, int m_age)
        : m_name(m_name), m_age(m_age)
    {
    }

    bool operator==(const Person& other) const
    {
        return  m_age == other.m_age;
    }
};
void testB()
{
    vector<Person> v;
    Person p1("刘备", 36);
    Person p2("关羽", 36);
    Person p3("张飞", 30);
    Person p4("赵云", 28);
    Person p5("曹操", 36);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    Person p("诸葛亮", 36);
    int num = count(v.begin(), v.end(), p);
    cout << num << endl;
}

int main()
{
    testA();
    testB();
    return 0;
}


count_if

功能描述:

函数原型:

示例:

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

//count_if   条件统计
class Greater10
{
public:
    bool operator()(int val)
    {
        return val > 10;
    }
};
void testA()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    int num = count_if(v.begin(), v.end(), Greater10());
    cout << num << endl;
}

//自定义数据类型
class Person
{
public:
    string m_name;
    int m_age;
public:

    Person(const string& m_name, int m_age)
        : m_name(m_name), m_age(m_age)
    {
    }

    bool operator==(const Person& other) const
    {
        return  m_age == other.m_age;
    }
};

class AgeGreater28
{
public:
    bool operator()(Person& p)
    {
        return p.m_age > 28;
    }
};
void testB()
{
    vector<Person> v;
    Person p1("刘备", 36);
    Person p2("关羽", 36);
    Person p3("张飞", 30);
    Person p4("赵云", 28);
    Person p5("曹操", 36);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    int num = count_if(v.begin(), v.end(), AgeGreater28());
    cout << num << endl;
}

int main()
{
    testA();
    testB();
    return 0;
}


常用排序算法

学习目标:

算法简介:

sort

功能描述:

函数原型:

示例:

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

// sort 排序
void testA()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(3);
    v.push_back(5);
    v.push_back(2);
    v.push_back(4);
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    //改为降序
    sort(v.begin(), v.end(), greater<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
}

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

random_shuffle

功能描述:

函数原型:

示例:

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

// random_shuffle 排序
void testA()
{
    srand((unsigned int)time(nullptr));
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    random_shuffle(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }

}

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

merge

功能描述:

函数原型:

示例:

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

// merge 合并
void testA()
{
    vector<int> v;
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
            v.push_back(i);
            v1.push_back(i+10);
    }
    vector<int> d;
    d.resize(v.size() + v1.size());
    merge(v.begin(), v.end(), v1.begin(), v1.end(), d.begin());
    for_each(d.begin(), d.end(), [](int val) {cout << val << " "; });
    cout << endl;
}

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


reverse

功能描述:

函数原型:

示例:

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

// reverse 反转
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    reverse(v.begin(), v.end());
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
    cout << endl;
}

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


常用拷贝和替换算法

学习目标:

算法简介:

copy

功能描述:

函数原型:

示例:

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

// copy 拷贝
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vector<int> vTarge;
    vTarge.resize(v.size());
    copy(v.begin(), v.end(), vTarge.begin());
    for_each(vTarge.begin(), vTarge.end(), [](int val) {cout << val << " "; });
}

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


replace

功能描述:

函数原型:

示例:

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

// replace 替换
void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    replace(v.begin(), v.end(), 3, 13);
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}

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


replace_if

功能描述:

函数原型:

示例:

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

// replace_if 替换
class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};

void testA()
{
    vector<int> v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    replace_if(v.begin(), v.end(), GreaterFive(), 13);
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}

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


swap

功能描述:

函数原型:

示例:

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

// swap 替换

void testA()
{
    vector<int> v;
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
        v1.push_back(i+10);
    }

    cout << "交换前: " << endl;
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
    cout << endl;
    for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
    cout << endl;
    cout << "交换后: " << endl;
    swap(v, v1);
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
    cout << endl;
    for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
    cout << endl;
}

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


常用算术生成算法

学习目标:

注意:

算法简介:


accumulate

功能描述

函数原型:

示例:

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

// accumulate 累加

void testA()
{
    vector<int> v;
    for (int i = 0; i <= 100; i++)
    {
        v.push_back(i);
    }

    int count = accumulate(v.begin(), v.end(), 0);

    cout << count <<  endl;
}
int main()
{
    testA();
    return 0;
}



fill

功能描述

函数原型:

示例:

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

// accumulate 累加

void testA()
{
    vector<int> v;
    v.resize(10);

    fill(v.begin(), v.end(), 5050);
    for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
    cout << endl;
}
int main()
{
    testA();
    return 0;
}


常用集合算法

学习目标:

算法简介:

set_intersection

功能描述:

函数原型:

示例:

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

// set_intersection 交集

void testA()
{
    vector<int> v;
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
        v1.push_back(i + 3);
    }
    vector<int> v2;
    // 开辟空间取小容器的值
    //v2.resize(v.size() > v1.size() ? v1.size():v.size());
    v2.resize(min(v.size(),v1.size()));
    vector<int>::iterator v2_end =  set_intersection(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2_end, [](int val) {cout << val << " "; });
}
int main()
{
    testA();
    return 0;
}


set_union

功能描述:

函数原型:

示例:

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

// set_union 并集

void testA()
{
    vector<int> v;
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
        v1.push_back(i + 3);
    }
    vector<int> v2;
    // 开辟空间
    v2.resize(v.size()+v1.size());
    vector<int>::iterator v2_end = set_union(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2_end, [](int val) {cout << val << " "; });
}
int main()
{
    testA();
    return 0;
}


set_difference

功能描述:

函数原型:

示例:

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

// set_difference 差集

void testA()
{
    vector<int> v;
    vector<int> v1;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
        v1.push_back(i + 3);
    }
    vector<int> v2;
    // 开辟空间
    v2.resize(max(v.size(),v1.size()));
    cout << "v和v1的差集" << endl;
    vector<int>::iterator v2_end = set_difference(v.begin(), v.end(), v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(),  v2_end, [](int val) {cout << val << " "; });
    cout << endl;
    cout << "v1和v的差集" << endl;
    v2_end = set_difference(v1.begin(), v1.end(), v.begin(), v.end(), v2.begin());
    for_each(v2.begin(), v2_end, [](int val) {cout << val << " "; });
}
int main()
{
    testA();
    return 0;
}

标签: C/C++ c++提高编程