文章目录
Algorithm库常用函数总结
可以去cplusplus官方网站查询,地址http://www.cplusplus.com/reference/algorithm/
☁️algorithm是C++中Stl的标准模板库,<algorithm>专门设计用于元素范围范围是可以通过迭代器或指针(例如数组或某些STL容器的实例)访问的任何对象序列。但是请注意,算法通过迭代器直接对这些值进行操作,不以任何方式影响任何可能的容器的结构(它从不影响容器的大小或存储分配)。
Functions in <algorithm>所有函数
🔽不想看可以跳过这一段列表,这里介绍Algorithm的所有操作,每个函数都有一个链接,点击即可到官网查看🔽
向下传送门⬅️
Non-modifying sequence operations:
-
Test condition on all elements in range (function template )
-
Test if any element in range fulfills condition (function template )
-
Test if no elements fulfill condition (function template )
-
Apply function to range (function template )
-
Find value in range (function template )
-
Find element in range (function template )
-
Find element in range (negative condition) (function template )
-
Find last subsequence in range (function template )
-
Find element from set in range (function template )
-
Find equal adjacent elements in range (function template )
-
Count appearances of value in range (function template )
-
Return number of elements in range satisfying condition (function template )
-
Return first position where two ranges differ (function template )
-
Test whether the elements in two ranges are equal (function template )
-
Test whether range is permutation of another (function template )
-
Search range for subsequence (function template )
-
Search range for elements (function template )
Modifying sequence operations:
-
Copy range of elements (function template )
-
Copy elements (function template )
-
Copy certain elements of range (function template )
-
Copy range of elements backward (function template )
-
Move range of elements (function template )
-
Move range of elements backward (function template )
-
Exchange values of two objects (function template )
-
Exchange values of two ranges (function template )
-
Exchange values of objects pointed to by two iterators (function template )
-
Transform range (function template )
-
Replace value in range (function template )
-
Replace values in range (function template )
-
Copy range replacing value (function template )
-
Copy range replacing value (function template )
-
Fill range with value (function template )
-
Fill sequence with value (function template )
-
Generate values for range with function (function template )
-
Generate values for sequence with function (function template )
-
Remove value from range (function template )
-
Remove elements from range (function template )
-
Copy range removing value (function template )
-
Copy range removing values (function template )
-
Remove consecutive duplicates in range (function template )
-
Copy range removing duplicates (function template )
-
Reverse range (function template )
-
Copy range reversed (function template )
-
Rotate left the elements in range (function template )
-
Copy range rotated left (function template )
-
Randomly rearrange elements in range (function template )
-
Randomly rearrange elements in range using generator (function template )
Partitions:
-
Test whether range is partitioned (function template )
-
Partition range in two (function template )
-
Partition range in two - stable ordering (function template )
-
Partition range into two (function template )
-
Get partition point (function template )
Sorting:
-
Sort elements in range (function template )
-
Sort elements preserving order of equivalents (function template )
-
Partially sort elements in range (function template )
-
Copy and partially sort range (function template )
-
Check whether range is sorted (function template )
-
Find first unsorted element in range (function template )
-
Sort element in range (function template )
Binary search (operating on partitioned/sorted ranges):
-
Return iterator to lower bound (function template )
-
Return iterator to upper bound (function template )
-
Get subrange of equal elements (function template )
-
Test if value exists in sorted sequence (function template )
Merge (operating on sorted ranges):
-
Merge sorted ranges (function template )
-
Merge consecutive sorted ranges (function template )
-
Test whether sorted range includes another sorted range (function template )
-
Union of two sorted ranges (function template )
-
Intersection of two sorted ranges (function template )
-
Difference of two sorted ranges (function template )
-
Symmetric difference of two sorted ranges (function template )
Heap:
-
Push element into heap range (function template )
-
Pop element from heap range (function template )
-
Make heap from range (function template )
-
Sort elements of heap (function template )
-
Test if range is heap (function template )
-
Find first element not in heap order (function template )
Min/max:
-
Return the smallest (function template )
-
Return the largest (function template )
-
Return smallest and largest elements (function template )
-
Return smallest element in range (function template )
-
Return largest element in range (function template )
-
Return smallest and largest elements in range (function template )
Other:
-
Lexicographical less-than comparison (function template )
-
Transform range to next permutation (function template )
-
Transform range to previous permutation (function template )
常用的函数
注意:给大家提供一个在线编译器。叫做C++ Shell。此编译器是cplusplus官网配套协作的编译器,方便大家看完文档写代码,给大家链接http://cpp.sh/很好用
1、max()、min()、abs()比较数字
这个在math头文件也可以,浮点型的绝对值要用math的fabs
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << max(a, b) << endl;
cout << min(a, b) << endl;
cout << abs(a - b) << endl;
return 0;
}
2、*max_element()、*min_element()比较容器(数组、字符串等)
上面的max、min函数只能比较数字,如果比较数组等容器怎么办,就用*…_element()方法
// min_element/max_element example
#include <iostream> // std::cout
#include <algorithm> // std::min_element, std::max_element
bool myfn(int i, int j) { return i<j; }
struct myclass {
bool operator() (int i,int j) { return i<j; }
} myobj;
int main () {
int myints[] = {3,7,2,5,6,4,9};
// using default comparison:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n';
// using function myfn as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n';
// using object myobj as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n';
return 0;
}
运行结果
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
注意,min_element和max_element要加星号*,因为iterator迭代器,就加上就行了
下面是比较动态数组伪代码
std::vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
std::cout << *max_element(v.begin(), v.end()) << std::endl;
3、swap()交换值
这个一般不用algorithm库也可以实现swap函数,但还是比较常用
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a, b;
swap(a, b);
cout << a << " " << b << endl;
int nums[4] = {0, 1, 2, 3};
swap(nums[0], nums[3]);
for (int i = 0; i < 3; i++)
cout << nums[i] << " ";
cout << endl;
return 0;
}
4、reverse()翻转容器
reverse()函数可以将一个容器直接翻转,例如数组、动态数组和字符串等
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int>array;//动态数组
for (int i = 0; i < 5; i++)
{
int t;
cin >> t;
array.push_back(t);
}
reverse(array.begin(), array.end());
for (int i = 0; i < array.size(); i++)
cout << array[i] << " ";
cout << endl;
cout << "--------" << endl;
string str = "hello world";//字符串
reverse(str.begin(), str.end());
cout << str << endl;
cout << "--------" << endl;
int arr1[101]; //数组
for (int i = 0; i < 5; i++)
{
cin >> arr1[i];
}
reverse(arr1, arr1 + 5);
for (int i = 0; i < 5; i++)
{
cout << arr1[i] << " ";
}
cout << endl;
return 0;
}
输出结果:
1 2 3 4 5
5 4 3 2 1
--------
dlrow olleh
--------
5 2 6 4 5
5 4 6 2 5
5、快速排序——sort函数
快速排序,时间超短!
实例:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> sortv; //动态数组
for (int i = 0; i < 5; i++)
{
int t;
cin >> t;
sortv.push_back(t);
}
sort(sortv.begin(), sortv.end());
for (int i = 0; i < 5; i++)
{
cout << sortv[i] << endl;
}
//数组
int sortnums[6];
for (int i = 0; i < 5; i++)
cin >> sortnums[i];
sort(sortnums, sortnums + 5);
for (int i = 0; i < 5; i++)
cout << sortnums[i] << endl;
//此sort是升序排序
return 0;
}
运行结果:
1 5 4 3 8
1
3
4
5
8
1 5 6 4 8
1
4
5
6
8
怎么把sort函数改成降序呢?
在命名空间下方添加一个函数——cmp(名字任意)
bool cmp(int a, int b)
{
return a > b;
}
sort函数改成这样
sort(v.begin(), v.end(), cmp);
sort(sortv.begin(), sortv.end());
for (int i = 0; i < 5; i++)
{
cout << sortv[i] << endl;
}
//数组
int sortnums[6];
for (int i = 0; i < 5; i++)
cin >> sortnums[i];
sort(sortnums, sortnums + 5);
for (int i = 0; i < 5; i++)
cout << sortnums[i] << endl;
//此sort是升序排序
return 0;
}
运行结果:
1 5 4 3 8
1
3
4
5
8
1 5 6 4 8
1
4
5
6
8
怎么把sort函数改成降序呢?
在命名空间下方添加一个函数——cmp(名字任意)
```cpp
bool cmp(int a, int b)
{
return a > b;
}
sort函数改成这样
sort(v.begin(), v.end(), cmp);
就可以了。