C++ Development of Generic Programming Lecture 17 [Common Copy and Replacement Algorithms]

One, copy

Function description:

  • Copy the elements of the specified range in the container to another container

Function prototype:

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

    // Find the element by value, find the iterator to return to the specified position, find the iterator to return to the end position

    // beg starts iterator

    // end ends the iterator

    // dest target start iterator

Example:

#include <algorithm>
#include <vector>

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

void test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i + 1);
	}
	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 using the copy algorithm to copy, the target container remembers to open up space in advance

二、replace

Function description:

  • Modify the old element in the specified range of the container to the new element

Function prototype:

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

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

    // beg starts iterator

    // end ends the iterator

    // oldvalue old element

    // newvalue new element

Example:

#include <algorithm>
#include <vector>

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(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() {

	test01();

	system("pause");

	return 0;
}

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

三、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 conditions, and replace those that meet the conditions with specified elements

    // beg starts iterator

    // end ends the iterator

    // _pred predicate

    // new element replaced by newvalue

Example:

#include <algorithm>
#include <vector>

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

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

};

void test01()
{
	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(), ReplaceGreater30(), 3000);
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

Summary: replace_if is searched by conditions, you can use functors to flexibly filter the conditions that are met

Four, swap

Function description:

  • Swap the elements of the two containers

Function prototype:

  • swap(container c1, container c2);

    // Exchange the elements of the two containers

    // c1 container 1

    // c2 container 2

Example:

#include <algorithm>
#include <vector>

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 << "交换前: " << 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() {

	test01();

	system("pause");

	return 0;
}

Summary: When swapping containers, pay attention to the same type of containers.

 

Guess you like

Origin blog.csdn.net/Kukeoo/article/details/114108554