STL常用算法

275 阅读2分钟

STL常用算法

概述:

算法主要由头文件<algorithm><functional><numeric>组成。
<algorithm>是有所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、赋值、修改等等。
<numeric>体积小,只包括几个在序列上面进行简单数学运算的模板函数。
<functional>定义了一些模板类,用以声明函数对象。

常用的遍历算法

算法简介:

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

函数原型:

  1. for_each(iterator beg , iterator end , _func);
  2. //遍历算法,遍历容器元素
  3. //beg开始迭代器
  4. //end结束迭代器
  5. //_func函数货值函数对象

示例:

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

//仿函数
class Print01
{
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(), Print);
	cout << endl;
	
	for_each(v.begin(), v.end(), Print01());
	cout << endl;

}

函数原型:

  1. transform(iterator beg1 , iterator end1 , iterator beg2 , _func);
  2. //beg1 源容器开始迭代器
  3. //end1 源容器结束迭代器
  4. //beg2 目标容器开始迭代器
  5. //_func 函数或者函数对象

示例:

class Transform
{
public:
	int operator()(int val)
	{
		return val + 100;
	}
	
};

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());
	cout << endl;

}

常用查找算法

算法简介:

  • find //查找元素
  • find_if //按条件查找元素
  • adjacent_find //查找相邻重复元素
  • binary_search //二分查找法
  • count //统计元素个数
  • count_if //按条件统计元素个数

函数原型:

  • find(iterator beg , iterator end , value);
  • //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
  • //beg开始迭代器
  • //end结束迭代器
  • //value查找元素

示例:

//查找	内置数据类型
void test01()
{
	vector<int>v;
	for (int i = 0;i < 10;i ++ )
	{
		v.push_back(i);
	}

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

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

	//重载	== 底层find知道如何对比Person数据类型
	bool operator==(const Person &p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

//自定义数据类型
void test02()
{
	vector<Person>v;
	//创建数据
	Person p1("张三", 100);
	Person p2("王五", 200);
	Person p3("李四", 300);

	//放入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);

	//Person pp("李四", 300);

	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(iterator beg , iterator end , _Pred);
  • //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
  • //beg开始迭代器
  • //end结束迭代器
  • //_Pred函数或者谓词(返回bool类型的仿函数)

示例:

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

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

};

//1、查找内置数据类型
void test01()
{
	vector<int>v;

	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end())
	{
		cout << "未找到!" << endl;
	}
	else
	{
		cout << "找到了大于7的数字为:" << *it << endl;
	}
}

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

	string m_Name;
	int m_Age;
};

class Greater20
{
public:
	bool operator()(Person &p)
	{
		return p.m_Age > 20;
	}
};
void test02()
{
	vector<Person>v;
	//创建数据
	Person p1("Tom", 20);
	Person p2("Yom", 26);
	Person p3("Lom", 30);

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

	//找到年龄大于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;
	}
} 

int main()
{
	//test01();
	test02();

	system("pause");
	return 0;
}

函数原型:

  • adjacent_find(iterator beg , iterator end);
  • //查找相邻重复元素,返回相邻元素的第一个位置的迭代器
  • //beg开始迭代器
  • //end结束迭代器

示例:

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

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

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

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

函数原型:

  • bool binary_search(iterator beg , iterator end , value);
  • //查找指定的元素,查找返回true 否则flase
  • //注意:在无序序列中不可以用
  • //beg开始迭代器
  • //end结束迭代器
  • //value查找的元素

示例:

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

{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	//v.push_back(2);	如果是无序序列,结果未知
	//查找容器中是否有9元素
	//注意:在无序序列中不可以用
	bool ret = binary_search(v.begin(), v.end(), 1);
	if (ret)
	{
		cout << "未找到元素!" << endl;
	}
	else
	{
		cout << "找到了元素!" << endl;
	}
}

int main()
{
	test01();

	system("pause");
	return 0;
}

函数原型:

  • count(iterator beg , iterator end , value);
  • //统计元素出现次数
  • //beg开始迭代器
  • //end结束迭代器
  • //value统计的元素

示例:

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

//1、统计内置数据类型
void test01()
{
	vector<int>v;

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

	int num = count(v.begin(), v.end(), 40);
	cout << "40的元素的个数:" << num << endl;
}
//2、统计自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Nmae = name;
	}

	bool operator==( const Person & p)
	{
		if (this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	string m_Nmae;
	int m_Age;
};
void test02()
{
	vector<Person>v;

	Person p1("刘备", 33);
	Person p2("张飞", 33);
	Person p3("关羽", 33);
	Person p4("马超", 32);
	Person p5("赵云", 32);

	//将人员插入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p("诸葛亮", 33);
	int num = count(v.begin(), v.end(), p);
	cout << "和诸葛亮同岁的人数为: " << num << endl;
}

int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

函数原型:

  • count_if(iterator beg , iterator end , _Pred);
  • //按条件统计元素出现次数
  • //beg开始迭代器
  • //end结束迭代器
  • //_Pred谓词

示例:

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

class Greater20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};


//1、统计内置数据类型
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(30);

	int num = count_if(v.begin(), v.end(), Greater20());
	cout << "在容器中大于20的元素个数为:" << num << endl;

}


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

	string m_Name;
	int m_Age;
};

class Greater
{
public:
	bool operator()(const Person & p)
	{
		return p.m_Age > 30;
	}
};

void test02()
{
	vector<Person>v;

	Person p1("李逵", 30);
	Person p2("林冲", 31);
	Person p3("史进", 32);
	Person p4("武松", 38);
	Person p5("宋江", 40);

	//将数据插入容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p4);
	v.push_back(p3);
	v.push_back(p5);

	
	//统计	大于30人员个数
	int num = count_if(v.begin(), v.end(), Greater());
	cout << "大于30岁年龄的个数为:" << num << endl;
}

int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}