C++ STL (12): Commonly used traversal algorithms (for_each, transform)


0 STL algorithm

STL algorithm consists of <algorithm> <functional> <numeric>such as headers composition.
<algorithm>: Include function templates for traversing, searching, sorting, comparing, swapping, copying, and modifying operations.
<functional>: Include class templates related to function objects/functors .
<numeric>: It is small in size and only includes function templates related to simple mathematical operations .


1 Introduction to commonly used traversal algorithms [for_each, transform]

Algorithm introduction ::
for_eachTraverse container elements and unified logic processing.
transform: Copy all the elements of the source container to the target container.


2 for_each [traversal of container elements and unified logic processing]

Role : Traverse container elements and unified logic processing.

Note: When using the for_eachalgorithm, you need to include the header file include <algorithm>.

Function prototype:
for_each(iterator begin, iterator end, _func);

Parameter explanation :: the
beginstarting position of the
enditerator;: the ending position
_funcof the iterator;: the processing of the container element currently traversed.
①Ordinary callback function;
②Function object/functor;
③Anonymous function (lambda expression).

Example :

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用for_each算法

//普通回调函数
void print(int val) {
    
    
	cout << val << " ";
}

//函数对象/仿函数
class Printer{
    
    
public:
	void operator()(int val) {
    
    
		cout << val << " ";
	}
};

int main() {
    
    
	vector<int> v;

	v.push_back(9);
	v.push_back(1);
	v.push_back(7);
	v.push_back(6);
	v.push_back(3);

	/* 遍历容器元素 */
	//1.for_each算法 + 普通回调函数————传递函数名
	for_each(v.begin(), v.end(), print);		//9 1 7 6 3
	
	//2.for_each算法 + 函数对象/仿函数————传递匿名函数对象
	for_each(v.begin(), v.end(), Printer());	//9 1 7 6 3
	
	//3.for_each算法 + 匿名函数(lambda表达式)
	for_each(v.begin(), v.end(), [](int val) {
    
    cout << val << " "; });	//9 1 7 6 3

	/* 对容器元素的统一逻辑处理 */ 
	//全部容器元素的值减1后输出
	for_each(v.begin(), v.end(), [](int val) {
    
    cout << val - 1 << " "; });	//8 0 6 5 2

	return 0;
}

3 transform [Copy all the elements of the source container to the target container]

Function : Copy all the elements of the source container to the target container.

Note 1: When using the transformalgorithm, it is necessary to include the header file include <algorithm>.
Note 2: When using the transformalgorithm, it needs to be the target containerOpen up memory space in advance, Such as dest.resize(src.size());, otherwise the program crashes when running.

Function prototype :
transform(iterator begin1, iterator end1, iterator begin2, _func);

Parameter explanation :: the
begin1starting position of the
end1source container iterator;: the ending position of the source container iterator
begin2;: the starting position of the target container iterator
_func;: the logical operation processing of the current traversal container element.
①Ordinary callback function;
②Function object/functor;
③Anonymous function (lambda expression).

Example :

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>	//使用transform算法

//普通回调函数
int print(int val) {
    
    
	//元素值加10
	return val + 10;
}

//函数对象/仿函数
class Printer {
    
    
public:
	int operator()(int val) {
    
    
		//直接返回元素
		return val;
	}
};

int main() {
    
    
	vector<int> src;

	for(int i = 0; i < 5; i++){
    
    
		src.push_back(i);
	}

	/* 拷贝容器的全部元素,必须为目标容器提前开辟内存空间 */
	vector<int> dest;
	dest.resize(src.size());

	//1.transform算法 + 普通回调函数————传递函数名
	transform(src.begin(), src.end(), dest.begin(), print);
	for_each(dest.begin(), dest.end(), [](int val) {
    
    cout << val << " "; });	//10 11 12 13 14

	//2.transform算法 + 函数对象/仿函数————传递匿名函数对象
	transform(src.begin(), src.end(), dest.begin(), Printer());
	for_each(dest.begin(), dest.end(), [](int val) {
    
    cout << val << " "; });	//0 1 2 3 4

	//3.transform算法 + 匿名函数(lambda表达式)
	transform(src.begin(), src.end(), dest.begin(), [](int val) {
    
    return val + 20; });
	for_each(dest.begin(), dest.end(), [](int val) {
    
    cout << val << " "; });	//20 21 22 23 24

	return 0;
}

Guess you like

Origin blog.csdn.net/newson92/article/details/114242754