STL-常用算法 遍历/查找/排序/拷贝和替换/算数生成/集合算法

STL常用算法

常用的遍历算法

for_each

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


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

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

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

	// 普通函数遍历
	for_each(v.begin(),v.end(),myPrint);
	cout << endl;

	// 函数对象遍历
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

transform

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


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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}

};
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	
	vector<int> v2;//目标容器
	v2.resize(v.size());//提前开辟空间
	transform(v.begin(), v.end(), v2.begin(), Transform());
	for_each(v2.begin(), v2.end(), Print());
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

常用的查找算法

对于自定义数据类型,一般需要重载==号 operator==

find

自定义数据类型

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

// 内置数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator pos = find(v.begin(), v.end(), 5);
	if (pos == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << *pos << endl;
	}
}

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

// 自定义数据类型
void test02()
{
	vector<Person> v;
	v.push_back(Person("111",10));
	v.push_back(Person("222", 20));
	v.push_back(Person("333", 30));
	v.push_back(Person("444", 40));
	v.push_back(Person("555", 50));
	v.push_back(Person("666", 60));
	v.push_back(Person("777", 70));

	vector<Person>::iterator pos = find(v.begin(), v.end(), Person("222", 20));
	if (pos == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl
			<< "姓名:" << pos->name << "  年龄:" << pos->age << endl;
	}
}
int main()
{
	// test01();
	test02();
	system("pause");
	return 0;
}

find_if

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

class GreaterFive
{
public:
	bool operator()(int v)
	{
		return v > 8;
	}
};
// 内置数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator pos = find_if(v.begin(),v.end(), GreaterFive());
	if (pos == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << *pos << endl;
	}
}

class Person
{
public:
	Person() {};
	bool operator()(Person p)
	{
		return p.age > 50;
	}
	Person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};

// 自定义数据类型
void test02()
{
	vector<Person> v;
	v.push_back(Person("111", 10));
	v.push_back(Person("222", 20));
	v.push_back(Person("333", 30));
	v.push_back(Person("444", 40));
	v.push_back(Person("555", 50));
	v.push_back(Person("666", 60));
	v.push_back(Person("777", 70));

	vector<Person>::iterator pos = find_if(v.begin(), v.end(), Person());
	if (pos == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl
			<< "姓名:" << pos->name << "  年龄:" << pos->age << endl;
	}
}
int main()
{
	// test01();
	test02();
	system("pause");
	return 0;
}

adjacent_find

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

void test01()
{
	vector<int> v;
	v.push_back(0);
	v.push_back(1);
	v.push_back(2);
	v.push_back(1);
	v.push_back(3);
	v.push_back(5);
	v.push_back(5);
	v.push_back(0);
	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << *pos << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

binary_search

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

void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	// 必须是有序的序列才能使用二分查找
	bool ret = binary_search(v.begin(), v.end(), 5);
	if (!ret)
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" <<endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

count

第三个参数中放的是统计的元素,自定义数据类型,元素内要放operator==

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

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

	int ret = count(v.begin(), v.end(), 5);
	cout << ret << endl;
}

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


// 自定义数据类型
void test02()
{
	vector<Person> v;
	v.push_back(Person("111", 10));
	v.push_back(Person("222", 40));
	v.push_back(Person("333", 30));
	v.push_back(Person("444", 40));
	v.push_back(Person("555", 40));
	v.push_back(Person("666", 40));
	v.push_back(Person("777", 70));

	int ret = count(v.begin(), v.end(), Person("888",40));
	cout << ret << endl;
	
}
int main()
{
	// test01();
	test02();
	system("pause");
	return 0;
}

count_if

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

class Greater5
{
public:
	bool operator()(int v)
	{
		return v > 5;
	}
};
// 内置数据类型
void test01()
{
	vector<int> v;
	for (int i = 0; i < 15; i++)
	{
		v.push_back(i);
	}
	v.push_back(5);
	v.push_back(5);
	v.push_back(5);

	cout << count_if(v.begin(), v.end(), Greater5()) << endl;
}

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


// 自定义数据类型
void test02()
{
	vector<Person> v;
	v.push_back(Person("111", 10));
	v.push_back(Person("222", 40));
	v.push_back(Person("333", 30));
	v.push_back(Person("444", 40));
	v.push_back(Person("555", 40));
	v.push_back(Person("666", 40));
	v.push_back(Person("777", 70));

	int ret = count_if(v.begin(), v.end(), Person());
	cout << ret << endl;

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

常用排序算法

sort

自定义类型

https://blog.csdn.net/qq_41575507/article/details/105936466

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>

void print(int v)
{
	cout << v << "  ";
}
void test01()
{
	vector<int> v;
	for (int i = 0; i < 15; i++)
	{
		v.push_back(i);
	}
	v.push_back(5);
	v.push_back(5);
	v.push_back(5);

	// 默认从大到小
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), print);
	cout << endl;

	sort(v.begin(), v.end(),greater<int>());
	for_each(v.begin(), v.end(), print);
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

random_shuffle

z自定义数据类型的乱序使用

和内置数据类型同样使用

https://learn.microsoft.com/zh-cn/troubleshoot/developer/visualstudio/cpp/libraries/use-random-shuffle-stl

pointer_to_unary_function类

作用:将一元函数指针转换为灵活的一元函数。

https://learn.microsoft.com/zh-cn/previous-versions/047wx24c(v=vs.120)

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>

void print(int v)
{
	cout << v << "  ";
}
void test01()
{
	vector<int> v;
	for (int i = 0; i < 15; i++)
	{
		v.push_back(i);
	}
	random_shuffle(v.begin(),v.end());
	for_each(v.begin(), v.end(), print);
	cout << endl;
}
int main()
{
	srand((unsigned int)time(NULL));
	test01();
	system("pause");
	return 0;
}

merge

自定义数据类型还没高明白

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

void print(int v)
{
	cout << v << "  ";
}
// 内置数据类型
void test01()
{
	vector<int> v;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
		v2.push_back(i + 2);
	}
	vector<int> vTarget;
	vTarget.resize(v.size() + v2.size());

	// 合并后仍然是有序的
	merge(v.begin(), v.end(), v2.begin(), v2.end(), vTarget.begin());
	for_each(vTarget.begin(),vTarget.end(), print);
	cout << endl;
}

class Person
{
public:
	Person() {};

	bool operator<(const Person& p)const
	{
		return false;
	}

	bool operator==(Person p)
	{
		if (this->name == p.name && this->age == p.age)
		{
			return true;
		}
		return false;
	}
	Person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};


//class Compare
//{
//public:
//	bool operator()(Person p1, Person p2)
//	{
//		return p1.age > p2.age;
//	}
//};
//void print2(Person p)
//{
//	cout << "姓名:\t" << p.name << "\t"
//		<< "年龄:" << p.age << endl;
//}
 自定义数据类型
//void test02()
//{
//	vector<Person> v;
//	v.push_back(Person("111", 20));
//	v.push_back(Person("222", 20));
//	v.push_back(Person("333", 30));
//	v.push_back(Person("444", 40));
//	v.push_back(Person("555", 50));
//	v.push_back(Person("666", 60));
//	v.push_back(Person("777", 70));
//	vector<Person> v2;
//	v2.push_back(Person("888", 30));
//	v2.push_back(Person("101", 40));
//	v2.push_back(Person("999", 90));
//
//	vector<Person> vTarget;
//	vTarget.resize(v.size()+v2.size());
//
//	merge(v.begin(), v.end(), v2.begin(), v2.end(), vTarget.begin(), Person());
//
//	for_each(vTarget.begin(), vTarget.end(), print2);
//	cout << endl;
//}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

reverse

常用的拷贝和替换算法

copy

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int v)
{
	cout << v << "  ";
}
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;

	vector<int> v2;
	v2.resize(v.size());
	copy(v.begin(),v.end(),v2.begin());
	for_each(v2.begin(), v2.end(), print);
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

replace

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(20);
	v.push_back(60);
	v.push_back(80);
	cout << "替换前" << endl;
	for_each(v.begin(),v.end(), Print());
	cout << endl;

	replace(v.begin(),v.end(),20,22);
	for_each(v.begin(), v.end(), Print());
	cout << endl;

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

replace_if

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};

class greater30
{
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(20);
	v.push_back(20);
	v.push_back(60);
	v.push_back(80);
	cout << "替换前" << endl;
	for_each(v.begin(), v.end(), Print());
	cout << endl;

	cout << "替换后" << endl;
	replace_if(v.begin(), v.end(), greater30(), 11);
	for_each(v.begin(), v.end(), Print());
	cout << endl;

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

swap

两个容器必须同一种类型

大小可以不一样

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};

void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(20);
	v.push_back(60);
	v.push_back(80);
	vector<int> v2;
	v2.push_back(111);
	cout << "替换前" << endl;
	for_each(v.begin(), v.end(), Print());
	cout << endl;
	for_each(v2.begin(), v2.end(), Print());
	cout << endl;

	cout << "替换后" << endl;
	swap(v, v2);
	for_each(v.begin(), v.end(), Print());
	cout << endl;
	for_each(v2.begin(), v2.end(), Print());
	cout << endl;

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

常用算数生成算法

numeric 英文 数字的

accumulate

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<string>


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

	cout << accumulate(v.begin(), v.end(), 0) << endl;

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

对于自定义数据类型的应用

https://www.jb51.net/article/242034.htm

fill

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};

void test01()
{
	vector<int> v;
	v.resize(10);
	for_each(v.begin(), v.end(), Print());
	cout << endl;

	fill(v.begin(), v.end(), 100);
	for_each(v.begin(), v.end(), Print());
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

常用的集合算法

set_intersection

返回交集的最后一个元素的下一个位置(我的是正确的,图片里是错误的)

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};

void test01()
{
	vector<int> v;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
		v2.push_back(i + 5);
	}
	vector<int> v3;
	v3.resize(min(v.size(), v2.size()));
	vector<int>::iterator pos = set_intersection(v.begin(),v.end(),v2.begin(),v2.end(), v3.begin());

	// pos 指向最后一个元素的下一个位置
	cout << *pos << endl;
	for_each(v3.begin(), pos, Print());
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

ste_union

返回交集的最后一个元素的下一个位置(我的是正确的,图片里是错误的)

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};

void test01()
{
	vector<int> v;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
		v2.push_back(i + 5);
	}
	vector<int> v3;
	v3.resize(v.size()+v2.size());
	vector<int>::iterator pos = set_union(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());

	// pos 指向最后一个元素的下一个位置
	cout << *pos << endl;
	for_each(v3.begin(), pos, Print());
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

set_difference

求谁的差集把谁的迭代器放前面

返回差集的最后一个元素的下一个位置(我的是正确的,图片里是错误的)

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

class Print
{
public:
	void operator()(int v)
	{
		cout << v << "  ";
	}
};

void test01()
{
	vector<int> v;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
		v2.push_back(i + 5);
	}
	vector<int> v3;
	v3.resize(max(v.size(),v2.size()));
	vector<int>::iterator pos = set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());

	// pos 指向最后一个元素的下一个位置
	// cout << *pos << endl;

	cout << "v的差集为:" << endl;
	for_each(v3.begin(), pos, Print());
	cout << endl;

	vector<int>::iterator pos2 = set_difference(v2.begin(), v2.end(), v.begin(), v.end(), v3.begin());
	cout << "v2的差集为:" << endl;
	for_each(v3.begin(), pos2, Print());
	cout << endl;

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值