STL-常用算法手册 | <algorithm> | <functional> | <numeric>

目录

  • STL常用算法
    • 概述
    • 遍历算法
      • for_each
      • transform
    • 常见查找算法
      • find
      • find_if
      • adjacent_find
      • binary_search
      • count
      • count_if
    • 常见排序算法
      • sort
      • random_shuffle
      • merge
      • reverse
    • 常见拷贝和替换算法
      • copy
      • replace
      • replace_if
      • swap
    • 常见算术生成法
      • accumulate
      • fill
    • 常见集合算法
      • set_intersection
      • set_union
      • set_difference

STL常用算法

概述

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第1张图片

遍历算法

for_each

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第2张图片

#include 
#include 

//普通函数
void print01(int val) 
{
	cout << val << " ";
}
//函数对象
class print02 
{
 public:
	void operator()(int val) 
	{
		cout << val << " ";
	}
};

//for_each算法基本用法
void test01() {

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

	//遍历算法  两种
	for_each(v.begin(), v.end(), print01);
	cout << endl;  //输出换行

	for_each(v.begin(), v.end(), print02());
	cout << endl;  //输出换行
}

transform

搬运的目标容器必须要提前开辟空间,否则无法正常搬运
STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第3张图片

#include
#include

//常用遍历算法  搬运 transform

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

};

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

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

	vector<int>vTarget; //目标容器

	vTarget.resize(v.size()); // 目标容器需要提前开辟空间

	transform(v.begin(), v.end(), vTarget.begin(), TransForm());

	for_each(vTarget.begin(), vTarget.end(), MyPrint());
}

常见查找算法

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第4张图片

find

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第5张图片

#include 
#include 
#include 
void test01() {

	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i + 1);
	}
	//查找容器中是否有 5 这个元素
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it == v.end()) 
	{
		cout << "没有找到!" << endl;
	}
	else 
	{
		cout << "找到:" << *it << endl;
	}
}

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

public:
	string m_Name;
	int m_Age;
};

void test02() {

	vector<Person> v;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

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

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

find_if

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第6张图片

class Greater20
{
public:
	bool operator()(Person &p)
	{
		return p.m_Age > 20;
	}

};

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

adjacent_find

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第7张图片

#include 
#include 

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

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

binary_search

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第8张图片

#include 
#include 

void test01()
{
	vector<int>v;

	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//二分查找
	bool ret = binary_search(v.begin(), v.end(),2);
	if (ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

int main() {

	test01();

	system("pause");

	return 0;
}

count

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第9张图片

#include 
#include 

//内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);

	int num = count(v.begin(), v.end(), 4);

	cout << "4的个数为: " << num << endl;
}

//自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	bool operator==(const Person & p)
	{
		if (this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person> v;

	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
    
    Person p("诸葛亮",35);

	int num = count(v.begin(), v.end(), p);
	cout << "num = " << num << endl;
}
int main() {

	//test01();

	test02();

	system("pause");

	return 0;
}

count_if

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第10张图片

#include 
#include 

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

//内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);

	int num = count_if(v.begin(), v.end(), Greater4());

	cout << "大于4的个数为: " << num << endl;
}

//自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

class AgeLess35
{
public:
	bool operator()(const Person &p)
	{
		return p.m_Age < 35;
	}
};
void test02()
{
	vector<Person> v;

	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);

	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(), AgeLess35());
	cout << "小于35岁的个数:" << num << endl;
}


int main() {

	//test01();

	test02();

	system("pause");

	return 0;
}

常见排序算法

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第11张图片

sort

sort属于开发中最常用的算法之一,需熟练掌握

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第12张图片

#include 
#include 

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

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

	//sort默认从小到大排序
	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;
}

random_shuffle

random_shuffle洗牌算法比较实用,使用时记得加随机数种子

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第13张图片

#include 
#include 
#include 

class myPrint
{
public:
	void operator()(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);
	}
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	//打乱顺序
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

merge

merge合并的两个容器必须的有序序列
STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第14张图片

#include 
#include 

class myPrint
{
public:
	void operator()(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 + 1);
	}

	vector<int> vtarget;
	//目标容器需要提前开辟空间
	vtarget.resize(v1.size() + v2.size());
	//合并  需要两个有序序列
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
	for_each(vtarget.begin(), vtarget.end(), myPrint());
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

reverse

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第15张图片

#include 
#include 

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

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

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

	cout << "反转后: " << endl;

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

int main() {

	test01();

	system("pause");

	return 0;
}

常见拷贝和替换算法

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第16张图片

copy

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第17张图片

copy(v1.begin(), v1.end(), v2.begin());

replace

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第18张图片

20改为2000
replace(v.begin(), v.end(), 20,2000);

replace_if

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第19张图片

// 自定义伪函数
class ReplaceGreater30
{
public:
	bool operator()(int val)
	{
		return val >= 30;
	}
};
// 将>=30的数字,替换为3000
replace_if(v.begin(), v.end(), ReplaceGreater30(), 3000);

swap

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第20张图片

vector<int> v1;
vector<int> v2;

swap(v1, v2);

常见算术生成法

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第21张图片

accumulate

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第22张图片

vector<int> v;
	for (int i = 0; i <= 100; i++) {
		v.push_back(i);
	}
// 从零开始累加
int total = accumulate(v.begin(), v.end(), 0);

fill

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第23张图片

	vector<int> v;
	v.resize(10);
	//填充
	fill(v.begin(), v.end(), 100);   //将元素的值填充为100

常见集合算法

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第24张图片

set_intersection

求交集的两个集合必须的有序序列
目标容器开辟空间需要从两个容器中取小值
set_intersection返回值既是交集中最后一个元素的位置

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第25张图片

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

	vector<int> vTarget;
	//取两个里面较小的值给目标容器开辟空间
	vTarget.resize(min(v1.size(), v2.size()));

	//返回目标容器的最后一个元素的迭代器地址
	vector<int>::iterator itEnd = 
        set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	for_each(vTarget.begin(), itEnd, myPrint());
	cout << endl;

set_union

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第26张图片

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

vector<int> vTarget;
//取两个容器的和给目标容器开辟空间
vTarget.resize(v1.size() + v2.size());

//返回目标容器的最后一个元素的迭代器地址
vector<int>::iterator itEnd = 
       set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

for_each(vTarget.begin(), itEnd, myPrint());
cout << endl;

set_difference

差集的两个集合必须的有序序列
目标容器开辟空间需要从两个容器取较大值

STL-常用算法手册 | <algorithm> | <functional> | <numeric>_第27张图片

vector v1;
for (int i = 0; i < 10; i++) {undefined
v1.push_back(i + 1);
}
vector v2;
v2.resize(v1.size());
copy(v1.begin(), v1.end(), v2.begin());

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

你可能感兴趣的:(C/C++,STL,c++,基础)