# 5 STL common algorithm (p243)

• summary:
1. The algorithm is mainly composed of header file < algorithm > < functional > < numeric >
2. < algorithm > is the largest of all STK header files, covering comparison, exchange, search, traversal, copy, modification, etc
3. The volume of numeric is very small, including only a few template functions that perform simple mathematical operations on the sequence
4. < functional > defines some template classes to declare function objects

## 5.1 common traversal algorithms

• Learning objectives: master the commonly used traversal algorithms
• Algorithm Introduction:
1. for_each / / traverse the container
2. transform / / move the container to another container

### 5.1.1 for_each

• Function Description: implement traversal of containers
• Function prototype: for_each(iterator beg, iterator end, _func);
//Traversal algorithm traverses container elements
//beg start iterator
//End end iterator
//_ func function or function object
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Common traversal algorithms for_each

//Ordinary function
void print01(int val)
{
cout << val << " ";
}

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

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;
}
```

The output result is:

```0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Please press any key to continue. . .
```
• Summary:
1. The third parameter, if it is an ordinary function, is the function name passed in
2. If it is an imitation function, pass in the function object
3. for_each is the most commonly used traversal algorithm in practical development, which needs to be mastered skillfully

### 5.1.2 transform(p244)

• Function Description: transport to another container
• Function prototype: transform(iterator beg1, iterator end1, iterator beg2, _func);
1. beg1: source container start iterator
2. end1: source container end iterator
3. beg2: target container start iterator
4. _ func: function or function object
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

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

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

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

vector<int> vTarget;//Target container
vTarget.resize(v.size());//The target container needs to open up space in advance

transform(v.begin(), v.end(), vTarget.begin(), Transform());

for_each(vTarget.begin(), vTarget.end(), MyPrint());
cout << endl;
}

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

The running result is

```0 1 2 3 4 5 6 7 8 9
```
• Conclusion: the target container to be handled must be opened up in advance, otherwise it cannot be handled normally

## 5.2 common search algorithms (p245)

• Learning objective: master common search algorithms
• Algorithm Introduction
1. Find find element
2. find_if find elements by criteria
4. binary_search binary search method
5. count counts the number of elements
6. count_if counts the number of elements by condition

### 5.2.1 find

• Function Description: find the specified element, find the iterator that returns the specified element, and cannot find the return end iterator end()
• Function prototype: find(iterator beg, iterator end, value);
1. Find the element by setting, find the iterator that returns the specified position, and the position that returns the end iterator cannot be found
2. beg start iterator
3. End end iterator
4. value lookup element
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Common search algorithms
//find

//Find built-in data types
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}

//Find out if there is 5 this element in the container
vector<int>::iterator it = find(v.begin(), v.end(), 5);
if (it == v.end())
{
cout << "Can't find" << endl;
}
else
{
cout << "Found:" << *it << endl;
}
}

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

//Overload = = the underlying find knows how to compare Person data types
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;
};

//Find custom data types
void test02()
{
vector<Person>v;
//Create data
Person p1("p1", 10);
Person p2("p2", 20);
Person p3("p3", 30);
Person p4("p4", 40);

//Put it in a container
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);

Person pp("p2", 20);

vector<Person>::iterator it = find(v.begin(), v.end(), pp);
if (it == v.end())
{
cout << "Can't find" << endl;
}
else
{
cout << "Element name found:" << it->m_Name << "Age: " << it->m_Age << endl;
}
}

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

The output result is:

```Found: 5
Element name found: p2 Age: 20
```
• Summary: find can find the specified element in the container, and the return value is the iterator

### 5.2.2 find_if

• Function Description: find elements by conditions
• Function prototype: find_if(iterator beg, iterator end, _Pred);
1. Find the element by value, find the iterator at the specified position of the return value, and the iterator position of the return result cannot be found
2. beg start iterator
3. End end iterator
4. _ Return type of prel or bood function
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//Common search algorithm find_if

//1. Find built-in data types
class GreaterFive
{
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(), GreaterFive());
if (it == v.end())
{
}
else
{
cout << "Found:" << *it << endl;
}

}

//2. Find custom data types

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

class Greater20
{
public:
bool operator()(Person &p)
{
return p.m_Age > 20;
}
};

void test02()
{
vector<Person> v;
//Create data
Person p1("p1", 10);
Person p2("p2", 20);
Person p3("p3", 30);
Person p4("p4", 40);

//insert data
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);

//Find people older than 20
vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());

if (it == v.end())
{
}
else
{
cout << "Found: name" << it->m_Name << " Age: " << it->m_Age << endl;
}
}

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

Output:

```Found: 6
Found: name p3 Age: 30
```

• Function Description: find adjacent repeating elements
• Function prototype adjacent_find(iterator beg, iterator end);
1. Find adjacent repeating elements and return the iterator of the first position of adjacent elements
2. beg start iterator
3. End end iterator
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

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

if (it == v.end())
{
cout << "No adjacent duplicate elements found" << endl;
}
else
{
cout << "Adjacent duplicate elements found:"<< *it << endl;
}
}

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

The output result is:

```Adjacent duplicate elements found: 3
```

### 5.2.4 binary_search(p248)

• Function Description: find whether the specified element exists
• Function prototype: bool binary_search(iterator beg, iterator end, value);
1. Find the specified element and return true, otherwise false
2. Note: not available in unordered sequences
3. beg start iterator
4. End end iterator
5. value lookup element
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//Common search algorithm binaey_search

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

//v.push_back(2);// The insertion becomes unordered, and the search result is unknown
//Find out if there are 9 elements in the container
//Note: containers must be ordered sequences
bool ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "Element found" << endl;
}
else
{
}
}

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

Output is;

```Element found
```
• Summary: binary search has high efficiency. It is worth noting that the elements in the container directly searched must be an ordered sequence

### 5.2.5 count(p249)

• Function Description: count the number of elements
• Function principle: count(iterator beg, iterator end, value);
1. Count the number of occurrences of the element
2. beg start iterator
3. End end iterator
4. Element of value statistics
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//Common search algorithms_ count

//1. Statistics built-in data type
void test01()
{
vector<int>v;

v.push_back(10);
v.push_back(40);
v.push_back(40);
v.push_back(40);
v.push_back(10);

int num = count(v.begin(), v.end(), 40);
cout << " 40 The number of elements is: " << num << endl;
}

//2. Statistics custom data type

class Person
{
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)
return true;
else
return false;
}

string m_Name;
int m_Age;
};

void test02()
{
vector<Person>v;

Person p1("p1", 10);
Person p2("p2", 20);
Person p3("p3", 20);
Person p4("p4", 30);
Person p5("p5", 20);

//Insert the person into the container
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);

Person p("p6", 20);

int num = count(v.begin(), v.end(), p);
cout << "and p6 The number of persons of the same age is: " << num << endl;

}

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

The output result is:

``` 40 Number of elements: 3
and p6 Number of personnel of the same age: 3
```
• Summary: when counting user-defined data types, it is necessary to overload the operator==

### 5.2.6 count_if(p250)

• Function Description: count the number of elements by conditions
• Function prototype: count_if(iterator beg, iterator end, _Pred);
1. Count the occurrence times of elements by conditions
2. Iterator beg in
3. End end iterator
4. _ Pred predicate
```#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

//Common search algorithm count_if

//Statistics built-in data type
class Graeter20
{
public:
bool operator()(int val)
{
return val > 20;
}
};

void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);

int num = count_if(v.begin(), v.end(), Graeter20());
cout << "The number of elements greater than 20 is: " << num << endl;
}

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

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

void test02()
{
vector<Person>v;
Person p1("p1", 10);
Person p2("p2", 20);
Person p3("p3", 30);
Person p4("p4", 40);
Person p5("p5", 50);

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

//Count the number of people greater than 20
int num = count_if(v.begin(), v.end(), AgeGreater20());
cout << "The number of people over 20 is:" << num << endl;
}

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

The output result is:

```Number of elements greater than 20: 3
Number of people over 20: 3
```

## 5.3 common sorting algorithms

• Learning objectives: master common sorting algorithms
• Algorithm Introduction:
1. Sort / / sort the elements in the container
2. random_shuffle / / shuffle refers to randomly adjusting the order of elements in the range
3. Merge / / merge container elements and store them in another container
4. reverse / / reverses the elements in the specified range

### 5.3.1 sort

• Function Description: sort the elements in the container
• Function prototype: sort(iterator beg, iterator end, _Pred)
1. Find the element by value, find the iterator that returns the specified position, and the iterator that returns the end cannot be found
2. beg start iterator
3. End end iterator
4. _ Pred predicate, optional, from small to large by default
```#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <functional>

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

//Common sorting algorithm sort
void test01()
{
vector<int>v;

v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(40);

//Ascending order using sort
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;

//Change to descending order
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}

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

Output is:

```10 20 30 40 50
50 40 30 20 10
```
• Summary: sort is one of the most commonly used algorithms in development and needs to be mastered

### 5.3.2 random_shuffle(p252)

• Function Description: shuffle the elements within the specified range and adjust the order randomly
• Function prototype: random_shuffle(iterator beg, iterator end);
1. Randomly adjust the order of elements within the specified range
2. beg start iterator
3. End end iterator
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <ctime>

//Commonly used random sorting algorithm_ shuffle

void myprint(int val)
{
cout << val << " ";
}
void test01()
{
srand((unsigned int) time(NULL));

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

//Shuffle the order using shuffle algorithm
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myprint);
cout << endl;

}

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

Output is:

```6 3 0 8 5 9 7 4 1 2
```
• Summary: random_ Shuffle shuffle algorithm is more practical. Remember to add random number seeds when using it

### 5.3.3 merge

• Function Description: two container elements are merged and stored in another container
• Function prototype: merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
1. The container is merged and stored in another container
2. beg1 container 1 start iterator
3. end1 container 1 end iterator
4. beg2 container 2 start iterator
5. end2 container 2 end iterator
6. dest destination container start iterator
• Allocate memory to the target container in advance
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Common sorting algorithm merge
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.push_back(i+1);
}

//Target container
vector<int> vTarget;

//Allocate memory to the target container in advance
vTarget.resize(v1.size() + v2.size());

merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

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

}

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

The output result is:

```0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
```
• Summary: the two containers merged by a merge must be an ordered sequence

### 5.3.4 reverse(p254)

• Function Description: invert the elements in the container
• Function prototype: reverse(iterator beg, iterator end);
1. Inverts the elements of the specified range
2. beg start iterator
3. End end iterator
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Common sorting algorithm reverse
void myPrint(int val)
{
cout << val << " ";
}

void test01()
{
vector<int>v1;

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

reverse(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), myPrint);
cout << endl;

}

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

The output result is:

```0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
```
• Conclusion: reserve reverses the elements in the interval, and the interview questions may be involved

## 5.4 common copy and replace algorithms (p255)

• Learning objectives: master common copy and replacement algorithms
• Algorithm Introduction:
1. copy the specified range of elements in the container to another container
2. replace modifies the old element of the specified range in the container to a new element
3. replace_if the elements in the specified scope that meet the conditions in the container are replaced with new elements
4. swap swaps the elements of two containers

### 5.4.1 copy

• Function Description: copy the elements in the specified range in the container to another container
• Function prototype: copy(iterator beg, iterator end, iteartor dest);
1. Find the element by value, find the iterator that returns the specified position, and the iterator that returns the result cannot be found
2. beg start iterator
3. End end iterator
4. dest target start iterator
• Allocate memory to the target container in advance
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//copy and replace algorithm
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;
}
```

The output result is:

```0 1 2 3 4 5 6 7 8 9
```
• Conclusion: when using copy algorithm to copy, the target container should remember to open up space in advance

### 5.4.2 replace

• Function Description: modify the old element of the specified range in the container to a new element
• Function prototype: replace(iteartor beg, iterator end, oldvalue, newvalue);
1. Replace the old element in the interval with a new element
2. beg start iterator
3. End end iterator
4. oldvalue old element
5. newvalue new element
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Search copy and replace algorithm
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};

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

void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(20);
v.push_back(40);

cout << "Before replacement" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;

//Replace 20 with 2000
replace(v.begin(), v.end(), 20, 2000);
cout << "After replacement" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;

}

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

Output is:

```Before replacement
10 20 30 20 40
After replacement
10 2000 30 2000 40
```
• Summary: replace will replace the qualified elements in the interval

### 5.4.3 replace_if(p257)

• Function Description: replace the qualified elements in the interval with the specified elements
• Function prototype: replace_if(iterator beg, iterator end, _Pred, newvalue);
1. Replace the element according to the condition, and replace the qualified element with the specified element
2. beg start iterator
3. End end iterator
4. _ Pred predicate
5. New element replaced by newvalue
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Search copy and replace algorithm_ if

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

class Greater20
{
public:
bool operator()(int val)
{
return val > 20;
}
};

void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);

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

replace_if(v.begin(), v.end(), Greater20(), 2000);
for_each(v.begin(), v.end(), myprint);
cout << endl;
}

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

The output result is:

```10 20 30 40
10 20 2000 2000
```
• Summary: replace_if search by conditions, you can use the imitation function to flexibly filter the conditions that are met

### 5.4.4 swap(p258)

• Function Description: exchange the elements of two containers
• Function prototype: swap (container C1, container C2);
1. Swap elements of two containers
2. c1 container 1
3. c2 container 2
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Common copy and replace algorithm swap
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.push_back(i + 100);
}

cout << "Before exchange" << endl;
for_each(v1.begin(), v1.end(), myprint);
cout << endl;
for_each(v2.begin(), v2.end(), myprint);
cout << endl;

swap(v1, v2);
cout << "After exchange" << endl;
for_each(v1.begin(), v1.end(), myprint);
cout << endl;
for_each(v2.begin(), v2.end(), myprint);
cout << endl;
}

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

The output result is:

```Before exchange
0 1 2 3 4 5 6 7 8 9
100 101 102 103 104 105 106 107 108 109
After exchange
100 101 102 103 104 105 106 107 108 109
0 1 2 3 4 5 6 7 8 9
```
• Summary: when swap containers, pay attention to the same type of containers to be exchanged

## Common arithmetic generation algorithms

• Learning objective: master the commonly used arithmetic generation algorithm
• Note: the arithmetic generation algorithm is a small algorithm, and the header file included when using is: #include < numeric >
• Algorithm Introduction:
1. Calculate calculates the cumulative sum of container elements
2. fill add element to container

### 5.5.1 accumulate

• Function Description: calculate the cumulative sum of interval content container elements
• Function prototype: accumulate(iterator beg, iterator end, value);
1. Calculate the cumulative sum of container elements
2. beg start iterator
3. End end iterator
4. Value starting value
```#include <iostream>
using namespace std;
#include <vector>
#include <numeric>

//Common arithmetic generation algorithms

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

//Parameter 3 initial cumulative value
int total = accumulate(v.begin(), v.end(), 0);

cout << "total = " << total << endl;

}

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

The output result is:

```total = 5050
```
• Summary: when using accumulate, the header file should be numeric. This algorithm is very practical

### 5.5.2 fill(p260)

• Function Description: fills the container with the specified elements
• Function prototype: fill(iterator beg, iterator end, value);
1. Fill the container with elements
2. beg start iterator
3. End end iterator
4. Value filled value
```#include <iostream>
using namespace std;
#include <vector>
#include <numeric>
#include <algorithm>

//Common arithmetic generation algorithm fill
void myPrint(int val)
{
cout << val << " ";
}

void test01()
{
vector<int> v;
v.resize(10);//10 zeros
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//Post refill
fill(v.begin(), v.end(), 100);

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

}

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

The output result is:

```0 0 0 0 0 0 0 0 0 0
100 100 100 100 100 100 100 100 100 100
```
• Fill can be used to fill the elements in the container interval with the specified value

## 5.6 common set algorithm (p261)

• Learning objective: master common set algorithms
• Algorithm Introduction
1. set_intersection finding the intersection of two containers
2. set_union to find the union of two containers
3. set_difference finding the difference set 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);
1. Find the intersection of two sets
2. Note: the two sets must be an ordered sequence
3. beg1 container 1 start iterator
4. end1 container 1 end iterator
5. beg2 container 2 start iterator
6. end2 container 2 end iterator
7. dest destination container start iterator
```#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

//Common set algorithm set_intersection
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);//0~9
v2.push_back(i + 5);//5~14
}

vector<int>vTarget;
//The target container opens up space in advance
//In the most special case, a large container contains a small container. Open up space and take the size of the small container
vTarget.resize(min(v1.size(), v2.size()));

//Get intersection
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;
}
```

The output result is:

```5 6 7 8 9
```
• Summary:
1. The two sets of intersection must be an ordered sequence
2. The target container needs to take a small value from the two containers to open up space
3. set_ The return value of intersection is the position of the last element in the intersection

### 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);
1. Find the union of two sets
2. Note: the two sets must be an ordered sequence
3. beg1 container 1 start iterator
4. end1 container 1 end iterator
5. beg2 container 2 start iterator
6. end2 container 2 end iterator
7. dest destination container start iterator
```#include <iostream>
using namespace std;
#include <vector>
//Common algorithm set_union
#include <algorithm>

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.push_back(i+5);
}

vector<int>vTarget;

//The target container opens up space in advance
//In the most special case, there is no intersection between the two containers, and the union is the addition of the size s of the two container vector s
vTarget.resize(v1.size() + v2.size());

vector<int>::iterator itEnd = set_union(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;
}
```

The output result is:

```0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
```
• Summary:
1. The two sets of union sets must be ordered sequences
2. The target container needs to add two containers to open up space
3. set_ The union return value is the position of the last element in the union set

### 5.6.3 set_difference(p263)

• Function Description: find the difference set of two sets
• Function prototype: set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
1. Find the difference set of two sets
2. Note: the two sets must be an ordered sequence
3. beg1 container 1 start iterator
4. end1 container 1 end iterator
5. beg2 container 2 start iterator
6. end2 container 2 end iterator
7. dest destination container start iterator
```#include <iostream>
using namespace std;
#include <vector>
//Common algorithm set_union
#include <algorithm>

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.push_back(i+5);
}

vector<int>vTarget;

//The target container opens up space in advance
//In the most special case, there is no intersection between the two containers. Take the larger size of the two containers as the target container to open up space
vTarget.resize(max(v1.size(), v2.size()));

vector<int>::iterator itEnd = set_difference(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;
}
```

The output result is:

```0 1 2 3 4
```
• Summary:
1. The two sets of the difference set must be an ordered sequence
2. The target container needs to take a larger value from the two containers to open up space
3. set_ The return value of difference is the position of the last element in the difference set

# After reading it, I gained a lot

However, it is not enough to just look at the dark horse. It can be said that the dark horse is to lay a foundation for students who have no foundation. The next step is to look at C++primer plus

Tags: C++ STL VS

Posted by rayner75 on Wed, 13 Apr 2022 06:57:35 +0300