[Dark Horse Programmer Course Record] C++ Improvement Part 12

5 STL-Common Algorithms

Overview :

  • Algorithms are mainly composed of header files <algorithm> <functional> <numeric>.

  • <algorithm>It is the largest of all STL header files, and its scope involves comparison, exchange, search, traversal operations, copying, modification, etc.

  • <numeric>Small in size, only includes a few template functions to perform simple mathematical operations on sequences

  • <functional>Some template classes are defined for declaring function objects.

5.1 Commonly used traversal algorithms

learning target:

  • Master common traversal algorithms

Algorithm introduction:

  • for_each// traverse the container
  • transform// move the container to another container

5.1.1 for_each

Function description:

  • implement traversal container

Function prototype:

  • for_each(iterator beg, iterator end, _func);

    // The traversal algorithm traverses the container elements

    // beg starts the iterator

    // end end iterator

    // _func function or function object

Example:

#include <algorithm>
#include <vector>

//普通函数
void print01(int val) 
{
    
    
	cout << val << " ";
}
//函数对象
class print02 
{
    
    
 public:
	void operator()(int val) 
	{
    
    
		cout << val << " ";
	}
};

//for_each算法基本用法
void test01() {
    
    

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

	//遍历算法
	for_each(v.begin(), v.end(), print01);
	cout << endl;

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

int main() {
    
    

	test01();

	system("pause");

	return 0;
}

Summary: for_each is the most commonly used traversal algorithm in actual development and needs to be mastered

5.1.2 transform

Function description:

  • Move a container to another container

Function prototype:

  • transform(iterator beg1, iterator end1, iterator beg2, _func);

//beg1 source container start iterator

//end1 source container end iterator

//beg2 target container start iterator

//_func function or function object

Example:

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

//常用遍历算法  搬运 transform


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

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


void test1()
{
    
    
	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(), myprint());
}



int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: The target container to be transported must open up space in advance, otherwise it cannot be transported normally

5.2 Common search algorithms

learning target:

  • Master common search algorithms

Algorithm introduction:

  • find//find element
  • find_if// find element by condition
  • adjacent_find// find adjacent duplicate elements
  • binary_search// binary search method
  • count// Count the number of elements
  • count_if// Count the number of elements by condition

5.2.1 find

Function description:

  • Find the specified element, find the iterator that returns the specified element, if not found, return the end iterator end()

Function prototype:

  • find(iterator beg, iterator end, value);

    // Find the element by value, if found, return the iterator at the specified position , if not found, return the end iterator position

    // beg starts the iterator

    // end end iterator

    // value to find the element

Example:

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

//查找算法
//find

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

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

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

	bool operator==(const person &p)
	{
    
    
		if (this->age == p.age && this->name == p.name)
		{
    
    
			return true;
		}
		else
		{
    
    
			return false;
		}
	}
	string name;
	int age;
};
void test2()
{
    
    
	vector<person>v;

	person p1("aaa", 10);
	person p2("bbb", 20);
	person p3("ccc", 30);
	person p4("ddd", 40);

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

	person pp("bbb", 20);
	vector<person>::iterator pos = find(v.begin(), v.end(), pp);

	if (pos == v.end())
	{
    
    
		cout << "没有找到!" << endl;
	}
	else
	{
    
    
		cout << "找到姓名:" << pos->name << " 年龄: " << pos->age << endl;
	}
}

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

Summary: Use find to find the specified element in the container, and the return value is an iterator

5.2.2 find_if

Function description:

  • find element by condition

Function prototype:

  • find_if(iterator beg, iterator end, _Pred);

    // Find the element by value, if found, return the iterator at the specified position, if not found, return the end iterator position

    // beg starts the iterator

    // end end iterator

    // _Pred function or predicate (function function returning bool type)

Example:

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

//1.内置数据类型
class greaterfive
{
    
    
public:
	bool operator()(int v)
	{
    
    
		return v > 5;
	}
};
void test1()
{
    
    
	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 << "找到了" << *it << endl;
	}
}

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

	string name;
	int age;
};

class mycompater
{
    
    
public:
	bool operator()(person& p)
	{
    
    
		return p.age > 20;
	}
};

void test2()
{
    
    
	vector<person>v;

	person p1("aaa", 10);
	person p2("bbb", 20);
	person p3("ccc", 30);
	person p4("ddd", 40);

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

	vector<person>::iterator it = find_if(v.begin(), v.end(), mycompater());
	if (it == v.end())
	{
    
    
		cout << "没有找到" << endl;
	}
	else
	{
    
    
		cout << "找到姓名:" << it->name << " 年龄: " << it->age << endl;
	}

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

Summary: find_if finds by condition to make the search more flexible, and the functor provided can change different strategies

5.2.3 adjacent_find

Function description:

  • Find adjacent duplicate elements

Function prototype:

  • adjacent_find(iterator beg, iterator end);

    // Find adjacent repeated elements and return an iterator to the first position of the adjacent element

    // beg starts the iterator

    // end end iterator

Example:

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

//查找相邻重复元素 adjacent_find
void test1()
{
    
    
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(4);
	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()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: If you find adjacent repeated elements in the interview question, remember to use the adjacent_find algorithm in STL

5.2.4 binary_search

Function description:

  • Find if the specified element exists

Function prototype:

  • bool binary_search(iterator beg, iterator end, value);

    // Find the specified element, return true if found, otherwise false

    // note: not available in unordered sequences

    // beg starts the iterator

    // end end iterator

    // value to find the element

Example:

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


void test1()
{
    
    
	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(), 9);
	
	if (ret)
	{
    
    
		cout << "找到了" << endl;
	}
	else
	{
    
    
		cout << "未找到" << endl;
	}
}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: The binary search method is very efficient. It is worth noting that the elements in the searched container must have an ordered sequence

5.2.5 count

Function description:

  • Number of statistical elements

Function prototype:

  • count(iterator beg, iterator end, value);

    // Count the number of occurrences of the element

    // beg starts the iterator

    // end end iterator

    // elements of value statistics

Example:

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

//内置数据类型
void test1()
{
    
    
	vector<int> v;

	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);
	
	int num = count(v.begin(), v.end(), 4);
	
	cout << "4的元素个数: " << num << endl;

}

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

	bool operator==(const person& p)
	{
    
    
		if (this->age == p.age)
		{
    
    
			return true;
		}
		else
		{
    
    
			return false;
		}
	}
	string name;
	int age;
};
void test2()
{
    
    
	vector<person>v;

	person p1("刘备", 35);
	person p2("关羽", 35);
	person p3("张飞", 35);
	person p4("赵云", 30);
	person p5("曹操", 25);

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

	person p("诸葛亮", 35);
	int num = count(v.begin(), v.end(), p);
	cout << "num = " << num << endl;
}

int main()
{
    
    
	test1();
	test2();

	system("pause");
	return 0;
}

Summary: When counting custom data types, you need to cooperate with overloadingoperator==

5.2.6 count_if

Function description:

  • Count the number of elements by condition

Function prototype:

  • count_if(iterator beg, iterator end, _Pred);

    // Count the number of occurrences of elements by condition

    // beg starts the iterator

    // end end iterator

    // _Pred predicate

Example:

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

//内置数据类型
class greater3
{
    
    
public:
	bool operator()(int &v)
	{
    
    
		return v > 3;
	}
};
void test1()
{
    
    
	vector<int> v;

	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);

	int num = count_if(v.begin(), v.end(), greater3());
	
	cout << ">3的元素个数: " << num << endl;

}

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

	string name;
	int age;
};

class greater30
{
    
    
public:
	bool operator()(const person& p)
	{
    
    
		return p.age > 30;
	}
};
void test2()
{
    
    
	vector<person>v;

	person p1("刘备", 35);
	person p2("关羽", 35);
	person p3("张飞", 35);
	person p4("赵云", 30);
	person p5("曹操", 25);

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

	int num = count_if(v.begin(), v.end(), greater30());
	cout << "num = " << num << endl;
}

int main()
{
    
    
	test1();
	test2();

	system("pause");
	return 0;
}

Summary: count by value and count_if by condition

5.3 Common sorting algorithms

learning target:

  • Master common sorting algorithms

Algorithm introduction:

  • sort//Sort the elements in the container
  • random_shuffle//Shuffle the elements in the specified range to randomly adjust the order
  • merge // The container elements are merged and stored in another container
  • reverse// Reverse the specified range of elements

5.3.1 sort

Function description:

  • Sort the elements in the container

Function prototype:

  • sort(iterator beg, iterator end, _Pred);

    // Find the element by value, if found, return the iterator at the specified position, if not found, return the end iterator position

    // beg starts the iterator

    // end end iterator

    // _Pred predicate

Example:

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

//sort
void myprint(int &v)
{
    
    
	cout << v << " ";
}
void test1()
{
    
    
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	//sort升序
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

	//降序
	sort(v.begin(), v.end(),greater<int>());
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: sort is one of the most commonly used algorithms in development and needs to be mastered

5.3.2 random_shuffle

Function description:

  • Shuffle the elements in the specified range to randomly adjust the order

Function prototype:

  • random_shuffle(iterator beg, iterator end);

    // Randomly adjust the order of elements in the specified range

    // beg starts the iterator

    // end end iterator

Example:

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


//random_shuffle
void myprint(int &v)
{
    
    
	cout << v << " ";
}
void test1()
{
    
    
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
    
    
		v.push_back(i);
	}

	//利用洗牌打乱
	random_shuffle(v.begin(), v.end());

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

}


int main()
{
    
    
	srand((unsigned int)time(NULL));
	test1();

	system("pause");
	return 0;
}

Summary: The random_shuffle shuffling algorithm is more practical, remember to add random number seeds when using it

5.3.3 merge

Function description:

  • The two container elements are merged and stored in another container

Function prototype:

  • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

    // The container elements are merged and stored in another container

    // Note: the two containers must be in order

    // beg1 container 1 start iterator
    // end1 container 1 end iterator
    // beg2 container 2 start iterator
    // end2 container 2 end iterator
    // dest destination container start iterator

Example:

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

//merge
//注意:两个必须是有序序列,且顺序一样
void myprint(int &v)
{
    
    
	cout << v << " ";
}
void test1()
{
    
    
	vector<int> v1;
	vector<int> v2;

	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
		v2.push_back(i + 1);
	}

	//目标容器
	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(), myprint);
	cout << endl;

}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: The ordered sequence of the two containers merged by merge

5.3.4 reverse

Function description:

  • Reverse the elements in the container

Function prototype:

  • reverse(iterator beg, iterator end);

    // Reverse the specified range of elements

    // beg starts the iterator

    // end end iterator

Example:

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

void myprint(int &v)
{
    
    
	cout << v << " ";
}
void test1()
{
    
    
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	cout << "反转前: " << endl;
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

	cout << "反转后: " << endl;

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


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: reverse reverses the elements in the interval, interview questions may involve

5.4 Common Copy and Replace Algorithms

learning target:

  • Master common copy and replace algorithms

Algorithm introduction:

  • copy// Copy the elements of the specified range in the container to another container
  • replace// Modify the old elements in the specified range in the container to new elements
  • replace_if // Elements in the specified range in the container that meet the conditions are replaced with new elements
  • swap// Swap the elements of the two containers

5.4.1 copy

Function description:

  • Copies the specified range of elements in a container to another container

Function prototype:

  • copy(iterator beg, iterator end, iterator dest);

    // Find the element by value, if found, return the iterator at the specified position, if not found, return the end iterator position

    // beg starts the iterator

    // end end iterator

    // dest target starting iterator

Example:

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

//常用拷贝和替换算法 copy
void myprint(int val)
{
    
    
	cout << val << " ";
}

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

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

	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
}

int main() {
    
    

	test01();

	system("pause");

	return 0;
}

Summary: When copying using the copy algorithm, the target container remembers to open up space in advance

5.4.2 replace

Function description:

  • Modify the old elements in the specified range in the container to new elements

Function prototype:

  • replace(iterator beg, iterator end, oldvalue, newvalue);

    // Replace the old elements in the interval with new elements

    // beg starts the iterator

    // end end iterator

    // oldvalue old element

    // newvalue new element

Example:

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

void myprint(int &v)
{
    
    
	cout << v << " ";
}
void test1()
{
    
    
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);
	v.push_back(10);
	v.push_back(20);

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

	//将容器中的20 替换成 2000
	cout << "替换后:" << endl;
	replace(v.begin(), v.end(), 20, 2000);
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: replace will replace the elements in the interval that meet the conditions

5.4.3 replace_if

Function description:

  • Replace the elements in the interval that meet the conditions with the specified elements

Function prototype:

  • replace_if(iterator beg, iterator end, _pred, newvalue);

    // Replace elements according to the conditions, and replace the elements that meet the conditions with the specified elements

    // beg starts the iterator

    // end end iterator

    // _pred predicate

    // The new element replaced by newvalue

Example:

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

void myprint(int &v)
{
    
    
	cout << v << " ";
}

class greater30
{
    
    
public:
	bool operator()(int& v)
	{
    
    
		return v >= 30;
	}
};
void test1()
{
    
    
	vector<int> v;
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);
	v.push_back(10);
	v.push_back(20);

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

	//将容器中大于等于的30 替换成 3000
	cout << "替换后:" << endl;
	replace_if(v.begin(), v.end(), greater30(), 3000);
	for_each(v.begin(), v.end(), myprint);
	cout << endl;

}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: replace_if searches by conditions, and you can use the functor to flexibly filter the conditions that meet

5.4.4 swap

Function description:

  • Swaps the elements of two containers

Function prototype:

  • swap(container c1, container c2);

    // Swap the elements of the two containers

    // c1 container 1

    // c2 container 2

Example:

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

void myprint(int &v)
{
    
    
	cout << v << " ";
}


void test1()
{
    
    
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++) {
    
    
		v1.push_back(i);
		v2.push_back(i + 100);
	}

	cout << "交换前: " << endl;
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;

	cout << "交换后: " << endl;
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myprint);
	cout << endl;
	for_each(v2.begin(), v2.end(), myprint);
	cout << endl;
}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: When swapping containers, note that the swapped containers must be of the same type

5.5 Common Arithmetic Generation Algorithms

learning target:

  • Master commonly used arithmetic generation algorithms

Notice:

  • The arithmetic generation algorithm is a small algorithm, and the header file included when using it is#include <numeric>

Algorithm introduction:

  • accumulate// Calculate the cumulative sum of the container elements

  • fill// add elements to the container

5.5.1 accumulate

Function description:

  • Computes the cumulative sum of the container elements in the range

Function prototype:

  • accumulate(iterator beg, iterator end, value);

    // Calculate the cumulative sum of the container elements

    // beg starts the iterator

    // end end iterator

    // value start value

Example:

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


void test1()
{
    
    
	vector<int>v;

	for (int i = 0; i <= 100; i++)
	{
    
    
		v.push_back(i);
	}

	//参数3是起始累加值
	int total = accumulate(v.begin(), v.end(), 0);
	cout << "total = " << total << endl;
}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: Note that the header file is numeric when using accumulate, this algorithm is very practical

5.5.2 fill

Function description:

  • Fills the container with the specified elements

Function prototype:

  • fill(iterator beg, iterator end, value);

    // fill the container with elements

    // beg starts the iterator

    // end end iterator

    // value filled value

Example:

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


class myprint
{
    
    
public:
	void operator()(int val)
	{
    
    
		cout << val << " ";
	}
};
void test1()
{
    
    
	vector<int>v;
	v.resize(10);
	//填充
	fill(v.begin(), v.end(), 100);
	for_each(v.begin(), v.end(), myprint());
}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summary: Use fill to fill the elements in the container interval with the specified value

5.6 Common Set Algorithms

learning target:

  • Master commonly used set algorithms

Algorithm introduction:

  • set_intersection// Find the intersection of two containers

  • set_union// Find the union of two containers

  • set_difference // Find the difference of two containers

5.6.1 set_intersection

Function description:

  • find the intersection of two containers

Function prototype:

  • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

    // Find the intersection of two sets

    // Note: The two collections must be ordered sequences

    // beg1 container 1 start iterator
    // end1 container 1 end iterator
    // beg2 container 2 start iterator
    // end2 container 2 end iterator
    // dest destination container start iterator

Example:

#include <vector>
#include <algorithm>

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

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

	vector<int> vTarget;
	//取两个里面较小的值给目标容器开辟空间
	vTarget.resize(min(v1.size(), v2.size()));

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

	for_each(vTarget.begin(), itEnd, myPrint());
	cout << endl;
}

int main() {
    
    

	test01();

	system("pause");

	return 0;
}

Summarize:

  • the ordered sequence necessary for the intersection of two sets
  • The target container needs to take the small value from the two containers to open up space
  • The return value of set_intersection is the position of the last element in the intersection (the iterator of the last element)

5.6.2 set_union

Function description:

  • find the union of two sets

Function prototype:

  • set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

    // find the union of two sets

    // Note: The two collections must be ordered sequences

    // beg1 container 1 start iterator
    // end1 container 1 end iterator
    // beg2 container 2 start iterator
    // end2 container 2 end iterator
    // dest destination container start iterator

Example:

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

//set_union
class myprint
{
    
    
public:
	void operator()(int val)
	{
    
    
		cout << val << " ";
	}
};
void test1()
{
    
    
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	vector<int>v3;
	//目标容器需要提前开辟空间
	//取两个容器的和给目标容器开辟空间
	v3.resize(v1.size() + v2.size());

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


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summarize:

  • The ordered sequence necessary to find the union of two sets
  • The target container needs to add two containers to open up space
  • The return value of set_union is the position of the last element in the union

5.6.3 set_difference

Function description:

  • Find the difference of two sets

Function prototype:

  • set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

    // Find the difference of two sets

    // Note: The two collections must be ordered sequences

    // beg1 container 1 start iterator
    // end1 container 1 end iterator
    // beg2 container 2 start iterator
    // end2 container 2 end iterator
    // dest destination container start iterator

Example:

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

//set_difference
class myprint
{
    
    
public:
	void operator()(int val)
	{
    
    
		cout << val << " ";
	}
};

void test1()
{
    
    
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
    
    
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	vector<int>v3;
	//目标容器需要提前开辟空间
	//取两个容器最大给目标容器开辟空间
	v3.resize(max(v1.size(),v2.size()));

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

	cout << "v2与v1的差集为: " << endl;
	pos_end = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
	for_each(v3.begin(), pos_end, myprint());
	cout << endl;
}


int main()
{
    
    
	test1();

	system("pause");
	return 0;
}

Summarize:

  • The ordered sequence necessary for two sets to be subtracted
  • The target container needs to take the larger value from the two containers to open up space
  • The return value of set_difference is the position of the last element in the difference set

Guess you like

Origin blog.csdn.net/m0_53953432/article/details/127593468