STL算法

遍历算法

1.for_each

遍历容器

void myfunction(int val)
{
	cout << val << endl;
}

class MYFUN
{
public:
	void operator()(int val)
	{
		cout << val << endl;
	}
};
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(60);
	//for_each(v.begin(), v.end(), myfunction);//普通函数
	for_each(v.begin(), v.end(), MYFUN());//仿函数
}

int main()
{
	test01();//函数遍历
	return 0;
}

2.transform

搬运一个容器到另一个容器,在搬运之前,必须先为另一个容器开辟空间。

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

void test02()
{
	vector<int>v1;
	v1.push_back(10);
	v1.push_back(30);
	v1.push_back(20);

	vector<int>v2;
	v2.resize(v1.size());//开辟v2容器的空间大小
	transform(v1.begin(), v1.end(), v2.begin(), TransForm());//仿函数
	for_each(v2.begin(), v2.end(), MyPrint());
}

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

查找算法

1.find

按值查找元素,查到后返回指定元素迭代器,找不到返回结束迭代器end()
find(iterator beg, iterator end, value);

class Person
{
public:
	int m_age;
	string m_name;
	Person(string name,int age)
	{
		this->m_age = age;
		this->m_name = name;
	}
	bool operator==(const Person& p)//重载运算符
	{
		if (this->m_age == p.m_age && this->m_name == p.m_name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

void test03()
{
	Person p1("li", 20);
	Person p2("he", 30);
	vector<Person>v;
	v.push_back(p1);
	v.push_back(p2);
	Person p3("li", 20);
	vector<Person>::iterator it = find(v.begin(), v.end(), p3);
	if (it == v.end())
	{
		cout<<"未找到相同的数"<<endl;
	}
	else
	{
		cout << "找到相同的数" <<it->m_name<<" "<<it->m_age << endl;
	}

}
void test04()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	vector<int>::iterator it = find(v.begin(), v.end(), 100);
	if (it == v.end())
	{
		cout << "未找到相同的数" << endl;
	}
	else
	{
		cout << "找到相同的数" << *it<< endl;
	}

}


int main()
{
	test03();//自定义数据类型
	test04();//内置数据类型
	return 0;
}

注意重载运算符的使用,参见本链接重载运算符使用

2.find_if

按条件查找元素
find_if(iterator beg, iterator end, _Pred);
_Pred 函数或者谓词(返回bool类型的仿函数)

class Greator
{
public:
	bool operator()(int val)
	{
		return val > 15;
	}
};

void test05()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), Greator());
	if (it == v.end())
	{
		cout << "未找到相同大于5的数" << endl;
	}
	else
	{
		cout << "找到相同大于5的数" << *it << endl;
	}
}
class Person
{
public:
		Person(string name, int age)
		{
			this->m_name = name;
			this->m_age = age;
		}
public:
		string m_name;
		int m_age;

};
class Great123//仿函数、一元谓词
{
public:
	bool operator()(const  Person& p)
	{
		return p.m_age > 20;
	}
};



void test06()
{
	Person p1("li", 20);
	Person p2("he", 30);
	Person p3("yang", 40);
	vector<Person> v1;
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	vector<Person>::iterator it = find_if(v1.begin(), v1.end(), Great123());
	if (it == v1.end())
	{
		cout << "未找到相同年龄大于20的人" << endl;
	}
	else
	{
		cout << "找到相同年龄大于20的人:" << it->m_name << endl;
	}

	
}


int main()
{
	test05();//内置数据类型
	test06();//自定义数据类型
	return 0;
}

3.adjacent_find

查找相邻重复元素

class Person
{
public:
	string m_name;
	int m_age;
public:
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
public:
	bool operator==(const Person& p)
	{
		if (m_name == p.m_name && m_age == p.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

void test07()
{
	vector<Person>v;
	Person p1("he", 20);
	Person p2("he1", 30);
	Person p3("he", 20);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	vector < Person >::iterator it=adjacent_find(v.begin(), v.end());
	if (it ==v.end())
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
	}

}

int main()
{
	test07();//查找相邻的相同元素
	return 0;
}

4.binary_search

二分查找,查找指定元素,速度快,前提是有序序列
bool binary_search(iterator beg, iterator end, value)查找指定的元素,查到 返回true 否则false

5.count

查找某个元素出现的次数,在自定义数据类型时,需要用到(operator==)重载运算符。
count(iterator beg, iterator end, value);返回整型个数值

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Person
{
public:
	string m_name;
	int m_age;
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 && this->m_name == p.m_name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


};

void test01()
{
	Person p1("li",15);
	Person p2("li",15);
	Person p3("he",15);
	Person p4("li",15);
	vector<Person>v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	Person p5("he", 15);
	int num = count(v.begin(), v.end(), p5);
	cout << num << endl;
}
int main()
{
	test01();
	return 0;

}

6.count_if

按条件统计元素个数
count_if(iterator beg, iterator end, _Pred)
_Pred 谓词

方法同find_if 一样

排序算法

1.sort

sort(iterator beg, iterator end, _Pred)
_Pred 谓词
sort算法不应用于map,set容器
map容器排序,在定义map时添加第三个参数,比如
map<int,int,greater<int>>m;

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;




class Person
{
public:
	string m_name;
	int m_age;
public:
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}


};
class my_print
{
public:
	void operator()(Person& p)
	{
		cout << p.m_name << " ";
		cout << p.m_age << "  ";
	}

};
class my_sort
{
public:
	bool operator()(Person& p1, Person& p2)
	{
		if (p1.m_age > p2.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};
void test02()
{
	vector<Person>v1;
	Person p1("li", 24);
	Person p2("he", 29);
	Person p3("liu", 2);
	Person p4("yang", 18);
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);
	for_each(v1.begin(), v1.end(), my_print());
	cout << "\n********************************\n" << endl;
	sort(v1.begin(), v1.end(), my_sort());
	for_each(v1.begin(), v1.end(), my_print());

}


int main()
{
	//test01();//内置数据类型
	test02();//自定义数据类型
	return 0;
}

2.random_shuffle

指定范围内的元素随机排序,使用时需要加上随机种子
srand((unsigned int)time(NULL))
random_shuffle(iterator beg, iterator end);
random_shuffle洗牌算法比较实用,使用时记得加随机数种子

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

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

int main()
{
	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(), my_print());
	cout << endl;
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), my_print());
}

3.merge

两个容器中的元素合并到一个容器中
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

// 容器元素合并,并存储到另一容器中

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

// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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


int main()
{
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+1);
	}
	for_each(v1.begin(), v1.end(), my_print());
	cout << endl;
	for_each(v2.begin(), v2.end(), my_print());
	cout << endl;

	vector<int>v3;
	v3.resize(v1.size() + v2.size());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), my_print());
	return 0;
}

4.reverse

将容器内元素进行反转
reverse(iterator beg, iterator end);
指定区间内的元素进行反转
// beg 开始迭代器
// end 结束迭代器

拷贝和替换算法

1.copy

将指定区间内的元素拷贝给另一个容器
在copy给另一个容器之前,需要提前开辟空间

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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


int main()
{
	vector<int>v1;
	
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		
	}
	for_each(v1.begin(), v1.end(), my_print());
	cout << endl;


	vector<int>v3;
	v3.resize(v1.size() );
	copy(v1.begin(), v1.end(), v3.begin());

	for_each(v3.begin(), v3.end(), my_print());
	return 0;
}

2.replace

将指定区间内的旧元素替换成新元素
replace(iterator beg, iterator end, oldvalue, newvalue);

3.replace_if

按指定条件把指定区间内的元素替换成新元素
replace_if(iterator beg, iterator end, _pred, newvalue);

4.swap

交换两个容器内的元素
swap(container c1, container c2);

// 互换两个容器的元素

// c1容器1

// c2容器2

常用算数算法

1.accumulate

accumulate(iterator beg, iterator end, value);

// 计算容器元素累计总和
// beg 开始迭代器
// end 结束迭代器
// value 起始值,
需要引用头文件#include <numeric>

2.fill

向容器中填充指定的元素
函数原型:

fill(iterator beg, iterator end, value);

// 向容器中填充元素

// beg 开始迭代器

// end 结束迭代器

// value 填充的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值