STL common algorithms

summary:
The algorithm is mainly composed of header files < algorithm > < functional > < numeric >
< algorithm > is the largest of all STL header files, covering comparison, exchange, traversal, assignment, modification, etc
< functional > the volume is very small, including only a few template functions that perform simple mathematical operations on the sequence
< numeric > defines some template classes to declare function objects

1 common traversal algorithms

1.1 for_each

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

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

1.2 transform
Function Description:
Transport container to another container

Function prototype:

transform(iterator beg1,iterator end1,iterator beg2,_func);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

// Common traversal algorithm transform

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

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

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	
	vector<int>vTarget;	// Target container
	vTarget.resize(v.size());
	transform(v.begin(), v.end(), vTarget.begin(), Transform());
	for_each(vTarget.begin(), vTarget.end(), MyPrint());
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

2 common search algorithms

Algorithm Introduction:

find			// Find element
find_if			// Find elements by criteria
adjacent_find	// Find adjacent duplicate elements
binary_search	// Binary search method
count			// Number of statistical elements
count_if		// Count the number of elements by condition

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);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// 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 the person data type
	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("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

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

	vector<Person>::iterator it = find(v.begin(), v.end(), p2);
	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;
}

2.2 find_if
Function Description:
Find elements by criteria

Function prototype:

find_if(iterator beg,iterator end, _Pred);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// 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())
	{
		cout << "No,";
	}
	else
	{
		cout << "The number found greater than 5 is:" << *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("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

	// Put it in a container
	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(), Greater20());
	if (it == v.end())
	{
		cout << "Can't find" << endl;
	}
	else
	{
		cout << "Name found:" << it->m_Name << " Age:" << it->m_Age << endl;
	}
}

int main()
{
	//test01();
	test02();

	system("pause");
	return 0;
}

2.3 adjacent_find
Function Description:
Find adjacent duplicate elements

Function prototype:

adjacent_find(iterator beg, iterator end);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// Common search algorithm adjacent_find
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);

	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());

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

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

2.4 binary_search
Function Description:
Finds whether the specified element exists

Function prototype:

bool binary_search(iterator beg, iterator end, value);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// Common search algorithm binary_search
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//v.push_back(2);  If it is an unordered sequence, the result is unknown!
	// Find if there are 9 elements in the container
	bool ret = binary_search(v.begin(), v.end(), 9);
	
	if (ret)
	{
		cout << "Element found" << endl;
	}
	else
	{
		cout << "not found" << endl;
	}
}

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

2.5 count
Function Description:
Number of statistical elements

Function prototype:

count(iterator beg, iterator end, value);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// 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(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);

	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_Name == p.m_Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person>v;
	Person p1("Liu Bei", 35);
	Person p2("Guan Yu", 35);
	Person p3("Fei Zhang", 35);
	Person p4("Zhao Yun", 30);
	Person p5("Cao Cao", 40);

	// 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("Zhuge Liang", 35);

	int num = count(v.begin(), v.end(), p);

	cout << "The number of people of the same age as Zhuge Liang is:" << num << endl;
}

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

2.6 count_if
Function Description:
Count the number of elements by condition

Function prototype:

count_if(iterator beg, iterator end, _pred)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// Common search algorithm count_if

// Statistics built-in data type
class Greater20
{
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(),Greater20());

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

	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	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("Liu Bei", 35);
	Person p2("Guan Yu", 35);
	Person p3("Fei Zhang", 35);
	Person p4("Zhao Yun", 40);
	Person p5("Cao Cao", 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);

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

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

3 common sorting algorithms

Learning objectives:
Master common sorting algorithms

Algorithm Introduction:

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

3.1 sort
Function Description:
Sort the elements in the container

Function prototype:

sort(iterator beg, iterator end, _pred);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

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

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);

	// Sort 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;
}

3.2 random_shuffle
Function Description:
Shuffle the elements in the specified range to adjust the order randomly

Function prototype:

random_shuffle(iterator beg, iterator end);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <ctime>

using namespace std;

// Common sorting algorithm random_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;
}

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 dst);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <ctime>

using namespace std;

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

3.4 reverse
Function Description:
Invert the elements in the container

Function prototype

reverse(iterator beg, iterator end);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <ctime>

using namespace std;

// Common sorting algorithm merge

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

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);

	cout << "Before reversing:" << endl;
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	cout << "After reversal:" << endl;
	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

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

4 common copy and replacement algorithms

Learning objectives:
Master common copy and replacement algorithms

Algorithm Introduction:

copy		// Copies the specified range of elements in a container to another container
replace		// Modify the old element of the specified scope of the container to a new element
replace_if	// Replace the elements within the container that meet the conditions of the specified range with new elements
swap		// Swap elements of two containers

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);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <ctime>

using namespace std;

// Common copy and replacement 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);
}

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

4.2 replace
Function Description:
Modifies the old element of the specified range in the container to a new element

Function prototype:

replace(iterator beg, iterator end, oldvalue, newvalue);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <ctime>

using namespace std;

// Common copy and replace algorithms replace

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

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

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

4.3 replace_if
Function Description:

replace_if(iterator beg, iterator end, _pred, newvalue);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// Common copy and replace algorithms replace_if

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

class Greater30
{
public:
	bool operator()(int val)
	{
		return val >= 30;
	}
};

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

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

	// Replace 30 or more with 3000
	cout << "After replacement:" << endl;
	replace_if(v.begin(), v.end(), Greater30(), 3000);
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

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

4.4 swap
Function Description:
Swap elements of two containers

Function prototype:

swap(container c1, container c2);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std;

// Common copy and replace algorithm swap

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 + 100);
	}

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

	cout << "--------------------------------------------" << endl;
	cout << "After exchange:" << endl;

	swap(v1, v2);
	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;
}

5 common arithmetic generation algorithms

Learning objectives:
Master common arithmetic generation algorithms

be careful:
The arithmetic generation algorithm belongs to a small algorithm. When used, the header file included is #include < numeric >

Algorithm Introduction:

accumulate		// Calculate cumulative sum of container elements
fill			// Add element to container

5.1 accumulate
Function Description:
Calculate the cumulative sum of interval content container elements

Function prototype:

accumulate(iterator beg, iterator end, value+)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>

using namespace std;

// Common arithmetic generation algorithms

void test01()
{
	vector<int>v;

	for (int i = 0; i <= 100; i++)
	{
		v.push_back(i);
	}
	int total = accumulate(v.begin(), v.end(), 0);
	cout << "total = " << total << endl;
}

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

5.2 fill
Function Description:
Fills the container with the specified element

Function prototype:

fill(iterator beg, iterator end, value)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>

using namespace std;

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

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

	// Post refill
	fill(v.begin(), v.end(), 100);

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

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

6 common set algorithms

Learning objectives:
Master common 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 set of two containers

6.1 set_intersection
Function Description:
Find the intersection of two containers

Function prototype:

set_intersection(interator beg1, interator end1, interator beg2, interator end2, interator dest)
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>

using namespace std;

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

	vector<int>vTarget;
	// The target container needs to open up space in advance
	// In the most special case, a large container contains a small container, and the size of the small container can be taken
	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;
}

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);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>

using namespace std;

// Common set algorithm set_union
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 needs to open up space in advance
	// In the most special case, two containers do not intersect. Union is the addition of two containers
	vTarget.resize(v1.size()+v2.size());

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

6.3 set_difference
Function Description:
Find the difference set of two sets

Function prototype:

set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>

using namespace std;

// Common set algorithm set_difference
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 needs to open up space in advance
	// In the most special case, the two containers do not intersect, and the large size in the two containers is used as the target container to open up space
	vTarget.resize(max(v1.size(),v2.size()));

	cout << "v1 and v2 The difference set of is:" << endl;
	vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

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

	cout << "v2 and v1 The difference set of is:" << endl;
	itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());

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

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

Tags: C++ Algorithm

Posted on Sun, 26 Sep 2021 22:17:38 -0400 by angeljyt