One, 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 to return to the specified position, find the iterator to return to the end position
// beg starts iterator
// end ends the iterator
// _Pred predicate
Example:
#include <algorithm>
#include <vector>
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默认从小到大排序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//从大到小排序
sort(v.begin(), v.end(), greater<int>());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
**Summary:**sort is one of the most commonly used algorithms in development and requires proficiency
二、random_shuffle
Function description:
- Shuffle the elements within the specified range and adjust the order randomly
Function prototype:
-
random_shuffle(iterator beg, iterator end);
// Randomly adjust the order of elements in the specified range
// beg starts iterator
// end ends the iterator
Example:
#include <algorithm>
#include <vector>
#include <ctime>
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;
//打乱顺序
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
Summary: random_shuffle shuffle algorithm is more practical, remember to add random number seed when using
Three, merge
Function description:
- Two container elements are merged and stored in another container
Function prototype:
-
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// The 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 target container start iterator
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 + 1);
}
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;
}
Summary: The two containers merged by merge must be in an orderly sequence and need to open up space.
Four, reverse
Function description:
- Reverse the elements in the container
Function prototype:
-
reverse(iterator beg, iterator end);
// Reverse the elements in the specified range
// beg starts iterator
// end ends the iterator
Example:
#include <algorithm>
#include <vector>
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 << "反转前: " << endl;
for_each(v.begin(), v.end(), myPrint());
cout << endl;
cout << "反转后: " << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint());
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}