C++ STL notes 13: STL common algorithms 3: common sorting algorithms;

Algorithm Introduction:

  • Sort / / sort the elements in the container
  • random_shuffle / / shuffle and randomly adjust the order of elements within the specified range.
  • merge / / the container elements are merged and stored in another container.
  • reverse / / reverses the elements of the specified range.

sort

Function Description:

  • Sort the elements in the container

Function prototype:

  • sort(iterator beg, iterator end, _Pred);
  • //Find the element by value, find the iterator that returns the specified position, and the iterator that returns the end cannot be found
  • //beg start iterator
  • //End end iterator
  • // _ Pred predicate
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 sorts from small to large by default
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	//Sort from large to small
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

greater: relational affine function.

Summary: sort is one of the most commonly used algorithms in development and needs to be mastered

random_shuffle

Function Description:

  • Shuffle: randomly adjust the order of elements within a specified range.
  • When adjusting random numbers, you also need a random number seed. It can be added to the main() function or above the call shuffle.

Function prototype:

  • random_shuffle(iterator beg, iterator end);
  • //Randomly adjust the order of elements within the specified range
  • //beg start iterator
  • //End end iterator
class myPrint
{
public:
	void operator()(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);
	}
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;

	//Disorder order
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

Summary: random_shuffle shuffle algorithm is more practical. Remember to add random number seeds when using it

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 dest);
    • //Container elements are merged and stored in another container
    • //Note: the two containers must be in order
  • //beg1 container 1 start iterator
  • //end1 container 1 end iterator
  • //beg2 container 2 start iterator
  • //end2 container 2 end iterator
  • //dest destination container start iterator
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 + 1);
	}

	vector<int> vtarget;
	//The target container needs to open up space in advance
	vtarget.resize(v1.size() + v2.size());
	//Merging requires two ordered sequences
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
	for_each(vtarget.begin(), vtarget.end(), myPrint());
	cout << endl;
}

Summary: the two containers merged by merge must be an ordered sequence. After merging, they are still an ordered sequence. The target container needs to open up space in advance.

reverse

Function Description:

  • Invert the elements in the container. Head to tail.

Function prototype:

  • reverse(iterator beg, iterator end);
    • //Inverts the elements of the specified range
    • //beg start iterator
    • //End end iterator
class myPrint
{
public:
	void operator()(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;
}

Conclusion: reverse reverses the elements in the interval, which may be involved in the interview question.

Keywords: C++ Algorithm

Added by tommy445 on Wed, 16 Feb 2022 06:48:33 +0200