STL常用算法汇总

STL常用算法

概述

  • <algorithm><functional><numeric>//头文件
    

遍历算法

transform&for_each

功能描述
  • transform搬运容器到另一个容器中
  • for_each遍历容器
函数原型
  • transform(iterator beg1, iterator end1, iterator beg2, _func);
    //beg1源容器开始迭代器
    //end1源容器结束迭代器
    //beg2目标容器开始迭代器
    //func函数或者函数对象
    
  • for_each(iterator beg, iterator end, _func);
    //func函数或者函数对象(只需要写函数名即可)
    //beg容器开始迭代器
    //end容器结束迭代器
    

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Transform
{
public:
    int operator()(int v)
    {
        return v+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());
    //100 101 102 103 104 105 106 107 108 109 
    cout << endl;
}

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

查找算法

find

功能描述
  • 查找指定元素, 找到返回指定元素的迭代器,找不到返回结束迭代器end()

一定注意是返回的迭代器

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

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//查找指定数据类型 
void test01()
{
	vector<int> v;
	for(int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	
	vector<int>::iterator it = find(v.begin(), v.end(), 11);
	
	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;
	}
	
	//了解底层代码 重载== 运算符 在底层==是在if里面 所以返回bool 
	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() 
{
	Person p1("张三", 18);
	Person p2("李四", 19);
	Person p3("王五", 20);
	
	vector<Person> v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	
	//p2出错 在Person类中重载== 让底层find直到如何 对比person数据类型 
	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;
	}
}

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

find_if

功能描述
  • 按条件查找元素
函数原型
  • find_if(iterator beg, iterator end, _Pred);
    //按值查找元素, 找到返回指定位置迭代器,找不到返回结束迭代器位置
    //beg开始迭代器
    //end结束迭代器
    //_Pred函数或者谓词(返回bool类型的仿函数)
    

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

//查找指定数据类型 
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(), GreatFive());
	
	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;
	}
	
	
	string m_Name;
	int m_Age;
};

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

//查找自定义数据类型 
void test02() 
{
	Person p1("张三", 18);
	Person p2("李四", 19);
	Person p3("王五", 20);
	
	vector<Person> v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	
	//p2出错 在Person类中重载== 让底层find直到如何 对比person数据类型 
	vector<Person>::iterator it = find_if(v.begin(), v.end(), GreaterNt());
	
	if(it == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到:" << it->m_Name << " " << it->m_Age << endl;
	}
}

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

adjacent_find

功能描述
  • 查找相邻重复元素
函数原型
  • adjacent_find(iterator beg, iterator end);
    //查找相邻重复元素,返回响铃重复元素的第一个位置的迭代器
    //beg开始迭代器
    //end结束迭代器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


//查找指定数据类型 
void test01()
{
	vector<int> v;
	v.push_back(0);
	v.push_back(1);
	v.push_back(0);
	v.push_back(2);
	v.push_back(3);
	v.push_back(2);
	v.push_back(2);
	
	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	
	if(pos == v.end())
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到:" << *pos << endl;//找到3 
	}
}


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

binary_search

功能描述
  • 查找指定元素是否存在
函数原型
  • bool binary_search(iterator beg, iterator end, value);
    //查找指定的元素,查到返回true 否则返回false
    //beg开始迭代器
    //end结束迭代器
    //value查找的元素
    

注意只能在有序序列中使用,原理是二分查找法


示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


//查找指定数据类型 
void test01()
{
	vector<int> v;
	for(int i = 0; i < 10; i++) 
	{
		v.push_back(i);
	}
	
	bool temp = binary_search(v.begin(), v.end(), 9);
	
	if(!temp)
	{
		cout << "没有找到!" << endl;
	}
	else
	{
		cout << "找到了" << endl;
	}
}


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

count

功能描述
  • 统计元素个数
函数原型
  • count(iterator beg, iterator end, value);
    //beg开始迭代器
    //end结束迭代器
    //value统计的元素
    

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


//统计内置数据类型 
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(1);
	v.push_back(1);
	v.push_back(2);
	v.push_back(1);
	
	cout << count(v.begin(), v.end(), 1) << endl;//4
	
}

//统计自定义数据类型

class Person
{
public:
	
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	
	//同find 底层都在if语句里面 
	bool operator== (const Person& p)
	{
		//只统计年龄一样的 若还加上年龄一样则还需this->m_Name = p.m_Name 
		if(this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	string m_Name;
	int m_Age;
};
 
void test02()
{
	Person p1("aaa", 10);
	Person p2("bbb", 10);
	Person p3("ccc", 10);
	Person p4("ddd", 20);
	
	vector<Person> v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	
	Person p5("eee", 10);
	//同find一样里面放的变量 
	cout << count(v.begin(), v.end(), p5) << endl;//3
}


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

count_if

功能描述
  • 按条件统计元素个数
函数原型
  • count_if(iterator beg, iterator end, _Pred);
    //按条件统计元素出现个数
    //beg开始迭代器
    //end结束迭代器
    //_Pred谓词
    

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


//统计内置数据类型 

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

void test01()
{
	vector<int> v;
	for(int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	
	cout << count_if(v.begin(), v.end(), Greater5()) << endl;
	
}

//统计自定义数据类型

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

class Greaterten
{
public:
	bool operator()(const Person& p)
	{
		return p.m_Age > 10;
	}
};
 
void test02()
{
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	
	vector<Person> v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	
	cout << count_if(v.begin(), v.end(), Greaterten()) << endl;
}


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

排序算法

  • sort//对容器内元素进行排序
    random_shuffle//洗牌 指定存储到另一容器中
    merge//容器元素合并,并存到另一容器中
    reverse//反转指定范围元素
    

sort

函数原型
  • sort(iterator beg, iterator end, _Pred);
    //beg开始迭代器
    //end结束迭代器
    //_Pred谓词(不加默认升序)
    

示例
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

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

//自己写一个谓词 
class Greater
{
public:
	bool operator()(int val1, int val2)
	{
		return val1 > val2;
	}
};

void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(50);
	v.push_back(70);
	v.push_back(30);
	v.push_back(40);
	
	sort(v.begin(), v.end(), greater<int>());//#include<functional>
//	sort(v.begin(), v.end());//没有谓词默认升序 
	
//	for_each(v.begin(), v.end(), Myprint);//遍历算法 
	for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
}



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

random_shuffle

函数原型
  • random_shuffle(iterator beg, iterator end);
    //指定范围内元素随机调整次序
    //beg开始迭代器
    //end结束迭代器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;


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(vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
}



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

merge

函数原型
  • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
    //beg1开始迭代器
    //end1结束迭代器
    //beg2开始迭代器
    //end2结束迭代器
    //目标容器开始迭代器
    

传入的两组数据必须是有序的数据,排完之后仍然是一个有序序列

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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);
	}
	
	for(int i = 10; i < 20; i++)
	{
		v2.push_back(i);
	}
	
	
	vector<int> v3;
	//v3最初为空 程序运行会错 
	v3.resize(v1.size()+v2.size());
	
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	
	for_each(v3.begin(), v3.end(), myprint);
}



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

reverse

  • reverse(iterator beg, iterator end);
    //反转指定范围的元素
    //beg开始迭代器
    //end结束迭代器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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;
	
	reverse(v1.begin(), v1.end());
	
	cout << "反转后:" << endl;
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
}



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

拷贝和替换算法

  • copy//容器内指定范围的元素拷贝到另一容器中
    replace//将容器内指定范围的旧元素替换为新元素
    replace_if//容器内指定范围满足条件的元素替换为新元素
    swap//互换两个容器的元素
    

copy

函数原型
  • copy(iterator beg, iterator end, iterator dest);
    //beg开始迭代器
    //end结束迭代器
    //目标容器的迭代器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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());//v2不能为空 提前开辟空间
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
}



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

replace

函数原型
  • replace(iterator beg, iterator end, oldvalue, newvalue);
    //将区间内旧元素替换成新元素
    //beg开始迭代器
    //end结束迭代器
    //oldvalue旧元素
    //newvalue新元素
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

void test01()
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(50);
	v1.push_back(10);
	v1.push_back(10);
	v1.push_back(10);
	
	cout << "替换前:" << endl;
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
	
	cout << "替换后:" << endl;
	replace(v1.begin(), v1.end(), 10, 100);
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
}



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

replace_if

函数原型
  • replace_if(iterator beg, iterator end, _Pred, newvalue);
    //按条件替换元素
    //beg开始迭代器
    //end结束迭代器
    //_Pred谓词
    //newvalue新元素
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

class Greater10
{
public:
	bool operator()(int val)
	{
		if(val > 10)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

void test01()
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(50);
	v1.push_back(10);
	v1.push_back(10);
	v1.push_back(10);
	
	cout << "替换前:" << endl;
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
	
	cout << "替换后:" << endl;
	replace_if(v1.begin(), v1.end(), Greater10(), 100);
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
}



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

swap

函数原型
  • swap(container c1, container c2);
    //互换两个容器
    //c1容器
    //c2容器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

class Greater10
{
public:
	bool operator()(int val)
	{
		return val > 10;
	}
};

void test01()
{
	vector<int> v1;
	for(int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	
	vector<int> v2;
	for(int i = 0; i < 10; 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;
	
	swap(v1, v2);
	cout << "交换后:" << endl;
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
}



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

算术生成算法

  • accumulate//计算容器元素累计总和
    fill//向容器中添加元素
    

accumulate

函数原型
  • accumulate(iterator beg, iterator end, value);
    //计算容器总和
    //beg开始迭代器
    //end结束迭代器
    //value为起始值
    

注意包含头文件<numeric>

示例
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;


void test01()
{
	vector<int> v1;
	for(int i = 0; i <= 100; i++)
	{
		v1.push_back(i);
	}
	
	//参数三为累加起始值 
	int value = accumulate(v1.begin(), v1.end(), 0);//5050 
	cout << value << endl;
}



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

fill

函数原型
  • fill(iterator beg, iterator end, value);
    //向容器中填充元素
    //beg开始迭代器
    //end结束迭代器
    //value填充的值
    
示例
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

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

void test01()
{
	vector<int> v1;
	v1.resize(10);//填充10个0 
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
	
    //重新填充 变为100
	fill(v1.begin(), v1.end(), 100);
	
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
}



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

常用集合算法

  • set_intersection//求两个容器的交集
    set_union//求两个容器的并集
    set_difference//求两个容器的差集
    

注意:需要两个容器都是有序序列

set_intersection

函数原型
  • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
    //beg1开始迭代器
    //end1结束迭代器
    //beg2开始迭代器
    //end2结束迭代器
    //dest目标容器开始迭代器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

void test01()
{
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;//目标容器 
	
	for(int i = 0; i < 10; i++) 
	{
		v1.push_back(i);//0~9
		v2.push_back(i+5);//5~14
	}
	
	//取小算法min包含头文件<algorithm>
	//最特殊情况 A是B的子集 
	v3.resize(min(v1.size(), v2.size()));
	
	//返回的是交集元素的最后一个迭代器itEnd 
	vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	
	//使用v3.end()的话输出5 6 7 8 9 0 0 0 0 0 因为resize之后可能比交集元素空间大 
	for_each(v3.begin(), itEnd, myprint);
}



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

set_intersection返回的是最后一个交集的迭代器。注意填itEnd,而不是v3.end()


set_union

函数原型
  • set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
    //求两个集合的并集
    //beg1开始迭代器
    //end1结束迭代器
    //beg2开始迭代器
    //end2结束迭代器
    //dest目标容器开始迭代器
    
示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

void test01()
{
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;//目标容器 
	
	for(int i = 0; i < 10; i++) 
	{
		v1.push_back(i);//0~9
		v2.push_back(i+5);//5~14
	}
	
	//最特殊情况 A和B没有交集 
	v3.resize(v1.size()+v2.size());
	
	//返回的是的最后一个迭代器itEnd 
	vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	 
	for_each(v3.begin(), itEnd, myprint);
}



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

set_difference

函数原型
  • set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
    //求两个集合的差集
    //beg1开始迭代器
    //end1结束迭代器
    //beg2开始迭代器
    //end2结束迭代器
    //dest目标容器开始迭代器
    

分为两种情况:A和B的差集与B和A的差集是不相同的

示例
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

void test01()
{
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;//目标容器 
	
	for(int i = 0; i < 10; i++) 
	{
		v1.push_back(i);//0~9
		v2.push_back(i+5);//5~14
	}
	
	//最特殊情况 A和B没有交集 取连个容器中大的那个 
	v3.resize(max(v1.size(), v2.size()));
	
	//返回的是的最后一个迭代器itEnd 
	vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	
	cout << "v1和v2的差集为:"  << endl;
	for_each(v3.begin(), itEnd, myprint);
	cout << endl;
	
	itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
	
	cout << "v2和v1的差集为:"  << endl;
	for_each(v3.begin(), itEnd, myprint);
	cout << endl;
}



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

舞台再大,你不上台,永远是个观众;平台再好,你不参与,永远是个局外人。能力再大,你不行动,只能看着别人成功!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值