C++中STL的常用算法总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++中STL的常用算法总结

爱学习的小灵子   2022-12-01 我要评论

算法主要由头文件<algorithm><functional><numeric>组成。

<algorithm>是所有STL头文件中最大的一个,范围包括比较、交换、查找、遍历、复制、修改等。

<functional>定义了一些模版类,常用的仿函数等。

<numeric>体积很小,只包括几个在序列上面进行简单数学运算的模版函数。

1.常用遍历算法

1.1 for_each

能力:

遍历容器的算法

函数原型:

for_each(iterator begin, iterator end, _func);

  • begin: 开始迭代器
  • end: 结束迭代器
  • _func: 函数或函数对象(即仿函数)

上面_func参数写法:

如果是普通函数:就写  函数名

如果是仿函数:需要写  类名()

示例:

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

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

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

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

	// 利用普通和函数 print1
	for_each(v.begin(), v.end(), print1);
	cout << endl;

	// 利用仿函数 print2()
	for_each(v.begin(), v.end(), print2());
}

int main() {
	test();

	system("pause");
	return 0;
}

1.2 transform

能力:

搬运容器到另一个容器中

函数原型:

transform(iterator begin1, iterator end1, iterator begin2, _func);

  • begin1:源容器开始迭代器
  • end1: 源容器结束迭代器
  • begin2: 目标容器开始迭代器
  • _func: 普通函数或仿函数

注意:

在使用transform搬运容器之前,必须要给目标容器提前开辟空间,不然会报错。通常用 resize()

示例:

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

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

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

void test() {
	vector<int> v;
	for (int i = 0; i < 5; 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(), MyPrint());
	cout << endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

2.常用查找算法

主要包含:

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

2.1 find

能力:

查找指定元素,若找到,返回指定元素迭代器;若未找到,返回结束迭代器end().

函数原型:

find(iterator begin, iterator end, value);

  • begin: 开始迭代器
  • end: 结束迭代器
  • value: 要查找的元素                

示例:

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

// 常用算法 find

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

	vector<int>::iterator pos = find(v.begin(), v.end(), 2);
	if (pos != v.end()) {
		cout << "find " << *pos << endl;
	}
	else {
		cout << "not find 2" << endl;
	}

	pos = find(v.begin(), v.end(), 10);
	if (pos != v.end()) {
		cout << "find " << *pos << endl;
	}
	else {
		cout << "not find 10" << 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;
		}
		else {
			return false;
		}
	}
	string m_name;
	int m_age;
};
void test2() {
	vector<Person> v;
	v.push_back(Person("echo", 20));
	v.push_back(Person("tom", 19));
	v.push_back(Person("lucy", 18));
	
	Person p("tom", 19);

	vector<Person>::iterator pos = find(v.begin(), v.end(), p);
	if (pos !=v.end()) {
		cout << " find p   name = "<<( * pos).m_name
			<<" age = "<<pos->m_age
			<< endl;
	}
	else {
		cout << "not find p" << endl;
	}
}

int main() {
	test2();

	system("pause");
	return 0;
}

2.2 find_if

能力:

按条件查找元素。

按值查找元素,若找到,返回指定元素迭代器,若未找到,返回结束迭代器。

函数原型:

find_if(iterator begin, iterator end, _pred);

  • begin: 开始迭代器
  • end: 结束迭代器
  • _pred: 普通函数或仿函数(需要是返回bool类型的函数)

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
// find_if

using namespace std;

// 查找内置数据类型
bool GreaterFive(int a) {
	return a > 5;
}

void test() {
	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 << "找到大于5的元素:" << *pos << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}


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

	string m_name;
	int m_age;
};


// 仿函数
class GreaterAge18 {
public:
	bool operator()(const Person& p) {
		// 年龄大于18岁
		return p.m_age > 18;
	}
};

void test2() {
	vector<Person> v;
	v.push_back(Person("tom", 18));
	v.push_back(Person("echo", 20));
	v.push_back(Person("lucy", 19));

	vector<Person>::iterator pos = find_if(v.begin(), v.end(), GreaterAge18());
	if (pos != v.end()) {
		cout << "找到年龄大于18的人。姓名:" << pos->m_name
			<< " 年龄:" << pos->m_age << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}

int main() {
	// test();
	test2();

	system("pause");
	return 0;
}

2.3 adjacent_find

能力:

查找相邻重复元素,若存在,返回指定位置迭代器,若不存在,返回迭代器end()。

函数原型:

adjacent_find(iterator begin, iterator end);

  • begin: 开始迭代器
  • end: 结束迭代器

示例:

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

// 查找相邻重复元素 adjacent_find
void test() {
	vector<int> v;
	v.push_back(0);
	v.push_back(1);
	v.push_back(0);
	v.push_back(3);
	v.push_back(4);
	v.push_back(3);
	v.push_back(3);

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

}

int main() {
	test();

	system("pause");
	return 0;
}

2.4 binary_search

能力:

查找指定元素是否存在。若存在,返回true。若不存在,返回false。

注意:

只能在有序序列才可用。

函数原型:

bool binary_search(iterator begin, iterator end, value);

  • begin: 开始迭代器
  • end: 结束迭代器
  • value: 要查找的元素

示例:

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

// binary_search 查找元素是否存在
void test() {
	vector<int> v;
	v.push_back(1);
	v.push_back(3);
	v.push_back(2);
	v.push_back(5);
	v.push_back(4);

	// 只有在有序序列中才是准确的,如果没有sort排序测试出 未找到
	sort(v.begin(), v.end());
	bool ret = binary_search(v.begin(), v.end(), 3);
	if (ret) {
		cout << "找到元素" << endl;
	}
	else {
		cout << "未找到元素" << endl;
	}
}

int main() {
	test();

	system("pause");
	return 0;
}

2.5 count

能力:

统计元素个数

函数原型:

count(iterator begin, iterator end, value);

  • begin: 开始迭代器
  • end: 结束迭代器
  • value: 要统计的元素

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 统计

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

	int n=count(v.begin(), v.end(), 1);
	cout << "容器中1的个数为:" << n << 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;
		}
		else {
			return false;
		}
	}

	string m_name;
	int m_age;
};
void test2() {
	vector<Person> v;
	v.push_back(Person("test1", 10));
	v.push_back(Person("test2", 20));
	v.push_back(Person("test1", 30));
	v.push_back(Person("test1", 10));

	Person p("test1", 10);

	int n = count(v.begin(), v.end(), p);
	cout << "name = test1, age =  10 的元素个数为:" << n << endl;
}

int main() {
	//test();
	test2();

	system("pause");
	return 0;
}

2.6 count_if 

能力:

按条件统计元素个数。

函数原型:

count_if(iterator begin, iterator end, _pred);

  • begin: 开始迭代器
  • end: 结束迭代器
  • _pred: 谓词

示例:

#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
using namespace std;
// count 统计

// 内置数据类型
class Greater3 {
public:
	bool operator()(int val) {
		return val > 3;
	}
};

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

	int n = count_if(v.begin(), v.end(), Greater3());
	cout << "容器中大于3个数为:" << n << endl;
}

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

	string m_name;
	int m_age;
};

class GreaterAge20 {
public:
	bool operator()(const Person& p) {
		// 年龄大于20
		return p.m_age > 20;
	}
};

void test2() {
	vector<Person> v;
	v.push_back(Person("test1", 10));
	v.push_back(Person("test2", 20));
	v.push_back(Person("test3", 30));
	v.push_back(Person("test4", 50));
	v.push_back(Person("test5", 40));

	Person p("test1", 10);

	int n = count_if(v.begin(), v.end(), GreaterAge20());
	cout << "年龄大于20的元素个数为:" << n << endl;
}

int main() {
	//test();
	test2();

	system("pause");
	return 0;
}

3.常用排序算法

3.1 sort

能力:

对容器内元素排序

函数原型:

sort(iterator begin, iterator end, _pred);

  • begin: 开始迭代器
  • end: 结束迭代器
  • _pred:谓词

示例:

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

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

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

	// 利用sort 默认升序
	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);
}

int main() {
	test();

	system("pause");
	return 0;
}

3.2 random_shuffe

能力:

也叫洗牌。将指定范围内的元素随机打乱次序。

函数原型:

random_shuffle(iterator begin, iterator end);//也叫洗牌。将指定范围内的元素随机打乱次序。

begin: 开始迭代器

end: 结束迭代器

注意:

使用时,需要加随机数种子,才能真实的随机起来,每次不一样。

#include<ctime>
​​​​​​​srand((unsigned int)time(NULL));
#include <iostream>
#include <string>
#include<vector>
#include<algorithm>
#include<ctime>

using namespace std;

// random_shuffle 洗牌 将制定范围内元素打乱次序

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

void test() {
	// 随机数种子,让random_shuffle真实的随机起来
	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(), print);
	cout << endl;

	// 洗牌打乱顺序
	random_shuffle(v.begin(), v.end());

	for_each(v.begin(), v.end(), print);
}

int main() {
	test();

	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
8 1 9 2 0 5 7 3 4 6 

3.3 merge

能力:

两个有序容器元素合并,并存储到目标容器中。

注意:

两个容器必须是有序的,才能合并。

两个容器的排序必须是一致的,都是升序或降序。

需要先给目标容器开辟空间。

函数原型:

merge(iterator begin1, iterator end1, iterator begin2, iterator end2, iterator target_begin);

  • begin1: 容器1开始迭代器
  • end1: 容器1结束迭代器
  • begin2:容器2开始迭代器
  • end2:容器2结束迭代器
  • target_begin:目标容器开始迭代器

示例:

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

// merge 将两个有序容器元素合并到一个目标容器中
void print(int val) {
	cout << val << " ";
}

void test() {
	vector<int> v1;
	vector<int> v2;

	// 两个容器需要是有序,且排序一致,都是升序或降序
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(100 + i);
		// v2.push_back(100 - i); // 程序会崩溃
	}

	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(), print);
}

int main() {
	test();

	system("pause");
	return 0;
}

3.4 reverse

能力:

将容器元素进行反转。

函数原型:

reverse(iterator begin, iterator end);

  • begin: 容器1开始迭代器
  • end: 容器1结束迭代器

示例:

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

// reverse 将容器元素反转

// 将string内容反转
void test() {
	string a = "abcdefg";

	reverse(a.begin(), a.end());

	cout << a << endl;
}

// vector容器元素反转
void print(int val) {
	cout << val << " ";
}

void test2() {
	vector<int>v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	reverse(v.begin(), v.end());

	for_each(v.begin(), v.end(), print);
}

int main() {
	test();
	test2();

	system("pause");
	return 0;
}

4.常用拷贝和替换算法

4.1 copy

能力:

将容器内指定范围内的元素拷贝到目标容器。

注意:

使用copy前需要提前给目标容器开辟空间。

函数原型:

copy(iterator begin, iterator end, iterator target_begin);

  • begin:容器开始迭代器
  • end:容器结束迭代器
  • target_begin:目标容器开始迭代器

示例:

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

using namespace std;

// copy 将容器内指定范围内元素拷贝到目标容器
void print(int val) {
	cout << val << " ";
}

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

	vector<int> tv;
	// 使用copy前需要提前给目标容器开辟空间
	tv.resize(v.size());
	copy(v.begin(), v.end(), tv.begin());

	for_each(tv.begin(), tv.end(), print);
}

int main() {
	test();

	system("pause");
	return 0;
}

4.2 replace

能力:

将容器指定范围的旧元素替换为新元素

函数原型:

replace(iterator begin, iterator end, oldvalue, newvalue);

  • begin: 开始迭代器
  • end: 结束迭代器
  • oldvalue: 旧元素
  • newvalue: 新元素

示例:

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

using namespace std;

// replace 将容器内旧元素替换为新元素
void print(int val) {
	cout << val << " ";
}

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

	replace(v.begin(), v.end(), 1, 10);

	for_each(v.begin(), v.end(), print);
}

int main() {
	test();

	system("pause");
	return 0;
}

4.3 replace_if

能力:

将容器区间内满足条件的元素,替换成新元素

函数原型:

replace_if(iterator begin, iterator end,  _pred, newvalue);

  • begin: 开始迭代器
  • end: 结束迭代器
  • _pred: 谓词
  • newvalue: 新元素

示例:

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

// replace_if 将容器区间内满足条件的元素,替换成新元素
bool greater3less9(int val) {
	// 大于三且小于9
	return val > 3 && val < 9;
}

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

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

	for_each(v.begin(), v.end(), print);
	cout << endl;

	// 将大于3且小于9的元素替换成100
	replace_if(v.begin(), v.end(), greater3less9,100);

	for_each(v.begin(), v.end(), print);
}

int main() {
	test();

	system("pause");
	return 0;
}

4.4 swap

能力:

互换两个容器的元素

注意:

交互的两个容器是同种类型

函数原型:

swap(container c1, container c2);

  • c1: 容器1
  • c2: 容器2

示例:

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

// swap 互换两个容器的元素

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

void test() {
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(100 + i);
	}
	for_each(v1.begin(), v1.end(), print);
	cout << endl;
	for_each(v2.begin(), v2.end(), print);
	cout << endl;

	swap(v1, v2);

	cout <<"交换容器后:" << endl;
	for_each(v1.begin(), v1.end(), print);
	cout << endl;
	for_each(v2.begin(), v2.end(), print);
	cout << endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

5.常用算术生成算法

5.1 accumulate

能力:

计算容器区间内元素之和

函数原型:

accumlate(iterator begin, iterator end, value);

  • begin: 开始迭代器
  • end: 结束迭代器
  • value: 起始值

示例:

#include <iostream>
#include <string>
#include<vector>
#include<numeric>
using namespace std;
//accumulate 计算容器区间内元素之和
void test() {
	vector<int>v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}

	vector<int>::iterator beg = v.begin();
	vector<int>::iterator end = v.begin()+5;
	// 计算前4个元素之和
	int ret=accumulate(beg,end, 0);

	cout << ret << endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

5.2 fill

能力:

向容器指定范围区间,填充指定的元素

注意:

填充的元素数据类型,与容器中元素的数据类型要保持一致。

函数原型:

fill(iterator begin, iterator end, value);

  • begin: 开始迭代器
  • end: 结束迭代
  • value: 填充的值   

示例:

#include <iostream>
#include <string>
#include<vector>
#include<numeric>
#include<algorithm>

using namespace std;
// fill 向容器中指定范围区间,填充指定元素

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

void test() {
	vector<int>v;
	v.resize(10);

	vector<int>::iterator beg = v.begin();
	vector<int>::iterator end = v.begin() + 5;

	fill(beg, end, 1000);

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

int main() {
	test();

	system("pause");
	return 0;
}

6.常用集合算法

集合的概念:

交集:包含两个集合相同的元素

并集:包含两个集合全部元素

差集:一个集合去掉也存在另一个集合中的元素,剩下来的元素。

注意,本章节三个求集合算法使用时:

求集合的两个容器,都必须是有序序列,且排序规则一致。

目标容器需要提前开辟空间。

6.1 set_intersection

能力:

求两个容器的交集

注意:

两个容器必须是有序序列,且排序规则一致;

要先开辟目标容器空间,可以取目标容器中中较小的空间。(最特殊情况,大容器包含小容器元素,目标容器空间元素个数等于小容器元素个数)

函数原型:

vector<int>::iterator taget_end= set_intersection(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);

  • taget_end:返回目标容器的最后一个元素的迭代器地址
  • begin1: 容器1开始迭代器
  • end1: 容器1结束迭代器
  • begin2:容器2开始迭代器
  • end2:容器2结束迭代器
  • target_begin:目标容器开始迭代器

示例:

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

//set_intersection 求容器交集

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

void test() {
	vector<int> v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	for_each(v1.begin(), v1.end(), print);
	cout << endl;
	for_each(v2.begin(), v2.end(), print);
	cout << endl;
	
	vector<int> vt;
	// 求交集 要先开辟目标容器空间
	// 可以取目标容器中中较小的空间
	vt.resize(min(v1.size(),v2.size()));

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

	cout << "交集:" << endl;
	for_each(vt.begin(), itEnd, print);
}

int main() {
	test();

	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
5 6 7 8 9 10 11 12 13 14
交集:
5 6 7 8 9

6.2 set_union

能力:

求两个容器的并集

注意:

两个容器必须是有序序列,且排序规则一致;

目标容器需要先开辟空间,要等于两个容器空间之和。(最特殊情况,两个容器没有交集,并集元素个数等于两个容器元素个数之和)

函数原型:

vector<int>::iterator taget_end= set_union(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);

  • taget_end:返回目标容器的最后一个元素的迭代器地址
  • begin1: 容器1开始迭代器
  • end1: 容器1结束迭代器
  • begin2:容器2开始迭代器
  • end2:容器2结束迭代器
  •  target_begin:目标容器开始迭代器

示例:

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

//set_union 求容器并集

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

void test() {
	vector<int> v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	for_each(v1.begin(), v1.end(), print);
	cout << endl;
	for_each(v2.begin(), v2.end(), print);
	cout << endl;

	vector<int> vt;
	// 求并集 需要先开辟目标容器空间
	// 可以取两个容器空间之和
	vt.resize(v1.size() + v2.size());

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

	cout << "并集:" << endl;
	for_each(vt.begin(), itEnd, print);
	cout << endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
5 6 7 8 9 10 11 12 13 14
并集:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

6.3 set_difference

能力:

求两个容器的差集

注意:

两个容器必须是有序序列,且排序规则一致;

目标容器需要先开辟空间,要等于第一个容器空间。(最特殊的情况就是两个容器没有交集,差集的元素个数与第一个容器元素一样)

函数原型:

vector<int>::iterator taget_end= set_difference(iteraor begin1, iterator end1, iterator begin2, iterator end2, iterator taget_begin);

  • taget_end:返回目标容器的最后一个元素的迭代器地址
  • begin1: 容器1开始迭代器
  • end1: 容器1结束迭代器
  • begin2:容器2开始迭代器
  • end2:容器2结束迭代器
  • target_begin:目标容器开始迭代器

示例:

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

//set_difference 求容器差集

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

void test() {
	vector<int> v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i+5);
	}
	for_each(v1.begin(), v1.end(), print);
	cout << endl;
	for_each(v2.begin(), v2.end(), print);
	cout << endl;

	// 求v1和v2的差集
	vector<int> vt1;
	// 求差集需要先给目标容器开辟空间
	// 最特殊的情况,两个容器没有差集,空间可以设定为第一个容器的大小
	vt1.resize(v1.size());

	// 返回目标容器的最后一个元素的迭代器地址
	vector<int>::iterator itEnd = set_difference(v1.begin(),v1.end(),
										v2.begin(),v2.end(),vt1.begin());
	cout << "v1和v2的差集:" << endl;
	for_each(vt1.begin(), itEnd, print);
	cout << endl;
	

	// 求v2和v1的差集:
	vector<int> vt2;
	// 求差集需要先给目标容器开辟空间
	// 最特殊的情况,两个容器没有差集,空间可以设定为第一个容器的大小
	vt2.resize(v2.size());

	// 返回目标容器的最后一个元素的迭代器地址
	vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(),
		v1.begin(), v1.end(), vt2.begin());
	cout << "v2和v1的差集:" << endl;
	for_each(vt2.begin(), itEnd2, print);
	cout << endl;
}

int main() {
	test();

	system("pause");
	return 0;
}

输出:

0 1 2 3 4 5 6 7 8 9
5 6 7 8 9 10 11 12 13 14
v1和v2的差集:
0 1 2 3 4
v2和v1的差集:
10 11 12 13 14

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们