C++进阶编程 --- 5.STL常用算法

第五章:

5.STL 常用算法

概述:

  • 主要由algorithm functional numeric头文件组成

  • algortithm涉及到比较、交换、查找、遍历、复制等等

  • numeric体积很小,只包括几个在序列上进行简单的数学运算的模板函数

  • functional 定义了一些模板类,用于声明函数对象

5.1 遍历算法
  • for_each //遍历容器

  • transform //搬运容器到另一个容器中

5.1.1 for_each

函数原型

  • for_each(iterator beg, iterator end, __func);

    //_func 函数或者函数对象

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

//普通函数
void myPrint01(int val)
{
    cout << val << " ";
}

//仿函数
class myPrint02
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

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

    for_each(v.begin(), v.end(), myPrint01);
    cout << endl;

    for_each(v.begin(), v.end(), myPrint02());
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.1.2 transform

函数原型

  • transform(iterator beg1, iterator end1, iterator beg2, _func);
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>

class myTransform
{
public:
    int operator()(int val)
    {
        return val;
    }
};

//仿函数
class myPrint02
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector<int>v;
    vector<int>aimT;

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

    aimT.resize(v.size());
    transform(v.begin(), v.end(), aimT.begin(), myTransform());

    for_each(v.begin(), v.end(), myPrint02());
    cout << endl;

    for_each(aimT.begin(), aimT.end(), myPrint02());
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.2 查找算法
5.2.1 find

作用:查找指定元素,找到并返回指定元素的迭代器,找不到就返回结束迭代器end()

函数原型

  • find(iterator beg, iterator end, value);
内置数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

void test01()
{
    vector<int> v1;

    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    vector<int>::iterator it = find(v1.begin(), v1.end(), 5);
    if (it != v1.end())
    {
        cout << "找到元素" << *it << endl;
    }
    else
    {
        cout << "没有找到元素" << endl;
    }
}

int main()
{
    test01();
    system("pause");
    return 0;
}
自定义数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class Student
{
public:
    Student(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    bool operator==(const Student& s) //重载==
    {
        if (this->m_Name == s.m_Name && this->m_Age == s.m_Age)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    string m_Name;
    int m_Age;
};

void test01()
{
    vector<Student> v2;

    Student s1("小明", 18);
    Student s2("小红", 19);
    Student s3("小王", 20);

    v2.push_back(s1);
    v2.push_back(s2);
    v2.push_back(s3);

    Student f("小明", 18);

    vector<Student>::iterator it = find(v2.begin(), v2.end(), f);
    if (it != v2.end())
    {
        cout << "找到元素" << " 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
    }
    else
    {
        cout << "没有找到元素" << endl;
    }
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.2.2 find_if

作用:按条件查找元素

函数原型

  • find_if(iterator beg, iterator end, _Pred);
    //_Pred 函数或谓词(返回bool类型的仿函数)
内置数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class ExceedFour
{
public:
    bool operator()(int val)
    {
        return val > 4;
    }
};

void test01()
{
    vector<int> v1;

    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    vector<int>::iterator it = find_if(v1.begin(), v1.end(), ExceedFour());
    if (it != v1.end())
    {
        cout << "找到大于4的元素" << *it << endl;
    }
    else
    {
        cout << "没有找到元素" << endl;
    }
}

int main()
{
    test01();
    system("pause");
    return 0;
}
自定义数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class Student
{
public:
    Student(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    string m_Name;
    int m_Age;
};

class ExceedNineTeen
    {
    public:
        bool operator()(Student &s)
        {
            return s.m_Age > 19;
        }
    };

void test01()
{
    vector<Student> v2;

    Student s1("小明", 18);
    Student s2("小红", 19);
    Student s3("小王", 20);

    v2.push_back(s1);
    v2.push_back(s2);
    v2.push_back(s3);

    vector<Student>::iterator it = find_if(v2.begin(), v2.end(), ExceedNineTeen());
    if (it != v2.end())
    {
        cout << "找到元素" << " 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
    }
    else
    {
        cout << "没有找到元素" << endl;
    }
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.2.3 adjacent_find

作用:查找相邻重复元素

函数原型

  • adjacent_find(iterator beg, iterator end);
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>

void test01()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(40);
    v.push_back(50);

    vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
    if (pos != v.end())
    {
        cout << "找到相邻重复元素:" << *pos << endl;
    }
    else
    {
        cout << "未找到" << endl;
    }

}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.2.4 binary_search

作用:查找指定元素是否存在

函数原型

  • bool binary_search(iterator beg, iterator end);

注意:无序序列中无法使用

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

void test01()
{
    vector<int>v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);

    bool ret = binary_search(v.begin(), v.end(), 4);
    if (ret)
    {
        cout << "找到了" << endl;
    }
    else
    {
        cout << "没找到" << endl;
    }
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.2.5 count

作用:统计元素个数

函数原型

  • count(iterator beg, iterator end, value);
内置数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

void test01()
{
    vector<int>v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(40);

    int num = count(v.begin(), v.end(), 40);
    cout << "40的个数:" << num << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
自定义数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class Student
{
public:
    Student(string name, int age)
    {
        this->m_Age = age;
        this->m_Name = name;
    }
    bool operator==(const Student& s)
    {
        if (this->m_Age == s.m_Age)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    string m_Name;
    int m_Age;
};

void test01()
{
    vector<Student>v;

    Student s1("小明", 18);
    Student s2("小王", 22);
    Student s3("小红", 19);
    Student s4("小李", 19);
    Student s5("小黑", 20);
    Student sf("小茂", 19);

    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);


    int num = count(v.begin(), v.end(), sf);
    cout << "与小茂同岁的个数:" << num << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.2.6 count_if

作用:按条件统计元素个数

函数原型

  • count_if(iterator beg, iterator end, _Pred)

    //_Pred 谓词

内置数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class ExceedThirty
{
public:
    bool operator()(int v)
    {
        return v > 30;
    }
};

void test01()
{
    vector<int>v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(40);

    int num = count_if(v.begin(), v.end(), ExceedThirty());
    cout << "40的个数:" << num << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
自定义数据类型
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class Student
{
public:
    Student(string name, int age)
    {
        this->m_Age = age;
        this->m_Name = name;
    }
    string m_Name;
    int m_Age;
};

class ExceedNineTeen
{
public:
    bool operator()(const Student& s)
    {
        return s.m_Age > 19;
    }
};

void test01()
{
    vector<Student>v;

    Student s1("小明", 18);
    Student s2("小王", 22);
    Student s3("小红", 19);
    Student s4("小李", 19);
    Student s5("小黑", 20);

    v.push_back(s1);
    v.push_back(s2);
    v.push_back(s3);
    v.push_back(s4);
    v.push_back(s5);


    int num = count_if(v.begin(), v.end(), ExceedNineTeen());
    cout << "大于19岁的个数:" << num << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.3 排序算法
  • sort

  • random_shuffle

  • merge

  • reverse

5.3.1 sort

作用:对容器中的元素进行排序

函数原型

  • sort(iterator beg, iterator end, _Pred);
    //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(60);
    v.push_back(20);
    v.push_back(50);
    v.push_back(30);

    //升序
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    //降序
    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.3.2 random_shuffle

作用:指定范围内的元素随机调整次序

该函数也被称为洗牌函数

函数原型

  • random_shuffle(iterator beg, iterator end);
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <ctime>

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    srand((unsigned int)time(NULL));
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    random_shuffle(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.3.3 merge

作用:两个容器元素合并存储到另一容器中

函数原型

  • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)

注意:两个容器必须是有序的

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

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;
    vector<int>v2;
    vector<int>aimV;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i+3);
    }
    aimV.resize(v1.size() + v2.size());
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), aimV.begin());
    for_each(aimV.begin(), aimV.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.3.4 reverse

作用:将容器内元素进行反转

函数原型

  • reverse(iterator beg, iterator end);
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;

    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    cout << "反转前:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;

    cout << "反转后:" << endl;
    reverse(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.4 拷贝和替换算法
  • copy

  • replace

  • replace_if

  • swap

5.4.1 copy

作用:容器内指定范围的元素拷贝到另一容器中

函数原型

  • copy(iterator beg, iterator end, iterator dest);

    //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

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

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;
    vector<int>v2;

    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    v2.resize(v1.size());

    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;

    copy(v1.begin(), v1.end(), v2.begin());
    for_each(v2.begin(), v2.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.4.2 replace

作用:将容器内指定范围的原元素修改为新元素

函数原型

  • replace(iterator beg, iterator end, oldvalue, newvalue);
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;

    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    cout << "替换前:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;

    cout << "替换后:" << endl;
    replace(v1.begin(), v1.end(), 2, 20);
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.4.3 replace_if

作用:将区间内满足条件的元素,替换为指定元素

函数原型

  • replace_if(iterator beg, iterator end, _Pred, newvalue);
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>

void myPrint(int val)
{
    cout << val << " ";
}

class ExceedEqualFour
{
public:
    bool operator()(int val)
    {
        return val >= 4;
    }
};

void test01()
{
    vector<int>v1;

    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }

    cout << "替换前:" << endl;
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;

    cout << "替换后:" << endl;
    replace_if(v1.begin(), v1.end(), ExceedEqualFour(), 20);  
    for_each(v1.begin(), v1.end(), myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.4.4 swap

作用:互换两个容器的元素

函数原型

  • swap(container c1, container c2);
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+10);
	}

	cout << "交换前:" << endl;
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;

	cout << "交换后:" << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
{
	test01();
	system("pause");
	return 0;
}
5.5 算术生成算法

注意:算术生成算法属于小型算法,使用时需包含头文件numeric

  • accumulate

  • fill

5.5.1 accumulate

作用:计算区间内容器元素累计总和

函数原型

  • accumulate(iterator beg, iterator end, value);

    //value 起始值

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

void test01()
{
	vector<int>v1;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	
	int total = accumulate(v1.begin(), v1.end(), 0);
	cout << "total = " << total << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
5.5.2 fill

作用:向容器中填充指定元素

函数原型

  • fill(iterator beg, iterator end, value)
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <numeric>

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1;
	v1.resize(10);
	fill(v1.begin(), v1.end(), 10);
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
d(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
5.6 集合算法
  • set_intersection

  • set_union

  • set_difference

5.6.1 set_intersection

作用:求两个集合的交集

函数原型

  • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

//dest 目标容器开始迭代器

注意:两个集合必须是有序序列

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

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;
    vector<int>v2;
    vector<int>aimT;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i + 3);
    }

    aimT.resize(min(v1.size(), v2.size()));

    cout << "v1和v2的交集为:" << endl;
    vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), aimT.begin());
    for_each(aimT.begin(), itEnd, myPrint);
    cout << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.6.2 set_union

作用:求两个集合的并集

函数原型

  • set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

//dest 目标容器开始迭代器

注意:两个集合必须是有序序列

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

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;
    vector<int>v2;
    vector<int>aimT;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i + 3);
    }

    aimT.resize(v1.size()+ v2.size());

    cout << "v1和v2的并集为:" << endl;
    vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), aimT.begin());
    for_each(aimT.begin(), itEnd, myPrint);
    cout << endl;

}

int main()
{
    test01();
    system("pause");
    return 0;
}
5.6.3 set_difference

作用:求两个集合的差集

函数原型

  • set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
    //dest 目标容器开始迭代器

注意:两个集合必须是有序序列,返回值是差集中最后一个元素的位置

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

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector<int>v1;
    vector<int>v2;
    vector<int>aimT;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i + 3);
    }

    aimT.resize(max(v1.size(), v2.size()));

    cout << "v1和v2的差集为:" << endl;
    vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), aimT.begin());
    for_each(aimT.begin(), itEnd, myPrint);
    cout << endl;

    cout << "v2和v1的差集为:" << endl;
    itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), aimT.begin());
    for_each(aimT.begin(), itEnd, myPrint);
    cout << endl;
}

int main()
{
    test01();
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值