STL standard library algorithm (detailed explanation of modified sequence algorithm) specific application of transform, patition, generate functions

This blog mainly introduces the correction sequence algorithm. Some operations of the correction sequence algorithm will change the contents of the container. For example, to copy part of a container to another part of the same container, or to fill a container with a specified value.

The following is the STL modified sequence algorithm table

parameter illustrate
copy(first,last,first2) copy
copy_backward(first,last,end2) reverse replication
fill(first,last,val) fills the container with the value of val
generate(first,last,func) Fills the container with the result of the specified action operation
partition(first,last,pred) Split the container by a split point (such as an even number)
random_shuffle(first,last) random rearrangement
remove(fist,last,val) remove an element without deleting it
replace(fist,last,val,val2)

replace val with val2

retate(first,middle,last) to rotate
reverse(first,last) Reverse the elements in the container
swap(it1,it2) Swaps the elements of two containers
transform(first,last,first2,func) Based on two containers, the interaction produces a third container
unique(first,last) Make repeated elements unique
swap_ranges(first,last,first) Replace the specified range

The specific implementation code is as follows:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int val)
{
	cout << val << " ";	
} 

int Sum(int a,int b)
{
	return a + b;
}

int random()
{
	return rand() % 100 + 1;
}

bool op(int i)
{
	return i % 2 == 0;
}

int main()
{
	vector<int> ivc1,ivc2;
	cout << "请输入ivc1(ivc2)的元素个数:" << endl;
	int n,x;
	cin >> n; 
	cout << "请输入ivc1的元素个数:" << endl;
	for(int i = 0; i < n; i++)
	{
		cin >> x;
		ivc1.push_back(x);
	}
	cout << "请输入ivc2的元素个数:" << endl;
	for(int i = 0; i < n; i++)
	{
		cin >> x;
		ivc2.push_back(x);
	}
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);
	cout << endl;
	cout << "ivc2:" << endl;
	for_each(ivc2.begin(),ivc2.end(),print);
	cout << endl << endl;
	
	//using copy()
	cout << "After copy():" << endl;
	copy(ivc1.begin(),ivc1.end(),ivc2.begin());
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	//将ivc1复制给ivc2 
	cout << endl;
	cout << "ivc2:"<<endl;
	for_each(ivc2.begin(),ivc2.end(),print);
	cout << endl << endl;
	
	//using reverse
	cout << "After reverse():" << endl;
	reverse(ivc1.begin(),ivc1.end());
	cout << "ivc1:" << endl;		//对ivc1进行颠倒 
	for_each(ivc1.begin(),ivc1.end(),print);
	cout << endl << endl;
	
	//using random_shuffle()
	cout << "After random_shuffle():" << endl;
	random_shuffle(ivc2.begin(),ivc2.end());
	cout << "ivc2:" << endl;		//对ivc2随机排序 
	for_each(ivc2.begin(),ivc2.end(),print);
	cout << endl << endl;
	
	//using unique()
	cout << "After unique():" << endl;
	unique(ivc1.begin(),ivc1.end());
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	//变成与末尾相同的元素 
	cout << endl << endl;
	
	//using swap()
	cout << "After swap():" << endl;
	swap(ivc1,ivc2);		//交换ivc1和ivc2中的元素 
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	
	cout << endl;
	cout << "ivc2:" << endl;
	for_each(ivc2.begin(),ivc2.end(),print);	 
	cout << endl << endl;
	
	//using transform
	cout << "After transform():" << endl;
	vector<int> ivc3;
	ivc3.resize(n);			//需要提前给ivc3一个容器大小否则transform保存不住数据 
	transform(ivc1.begin(),ivc1.end(),ivc2.begin(),ivc3.begin(),Sum); 
	cout << "ivc3:" << endl;
	for_each(ivc3.begin(),ivc3.end(),print);	
	cout << endl << endl;
	
	//using copy_backward()
	cout << "After copy_backward():" << endl;
	vector<int> ivc4;
	ivc4.resize(n);
	copy_backward(ivc1.begin(),ivc1.end(),ivc4.end());
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	
	cout << endl; 
	cout << "ivc4:" << endl;
	for_each(ivc4.begin(),ivc4.end(),print);	
	cout << endl << endl; 
	
	//using fill()
	cout << "After fill():" << endl;
	fill(ivc4.begin(),ivc4.end(),8);
	cout << "ivc4:" << endl;
	for_each(ivc4.begin(),ivc4.end(),print);	
	cout << endl << endl;
	
	//using generate()
	cout << "After generate():" << endl;
	generate(ivc4.begin(),ivc4.end(),random);	//产生随机数给ivc4 
	cout << "ivc4:" << endl;
	for_each(ivc4.begin(),ivc4.end(),print);	
	cout << endl << endl;
	
	//using remove()
	cout << "After remove():" << endl;
	remove(ivc1.begin(),ivc1.end(),3);	//移除ivc1中值为3的元素 
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	
	cout << endl << endl;
	
	//using replace()
	cout << "After replace():" << endl;
	replace(ivc1.begin(),ivc1.end(),5,77);	//将5的值改为77 
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	
	cout << endl << endl; 
	
	//using rotate()
	cout << "After rptate():" << endl;
	rotate(ivc1.begin(),ivc1.begin()+3,ivc1.end());	//旋转ivc1 
	cout << "ivc1:" << endl;
	for_each(ivc1.begin(),ivc1.end(),print);	
	cout << endl << endl;
	
	//using partition()
	/*
 *  partition 参数介绍
 *  1. begin迭代器
 *  2. end 迭代器 注意是前开后闭的 [begin, end)
 *  3. pred 是个 callable 类型, 签名如 bool(int v);
 *     partition的返回值q指向第一个不满足 pred 的位置,
 *     最终[begin, q) 符合pred, [q, end) 不符合
 * */
	cout << "After partition():" << endl;
	vector<int>::iterator it;
	vector<int>::iterator bound;
	it = partition(ivc1.begin(),ivc1.end(),op);
	cout << "odd elements:" << endl;
 	for(bound = ivc1.begin(); bound != it; bound++)
 		cout << *bound << " ";
	cout << endl;	
	cout << "even elements:" << endl;
	for( bound = it; bound != ivc1.end(); bound++)
 		cout << *bound << " ";	
	return 0;
}

The specific operation results are shown in the figure:

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_61886762/article/details/124211611