1 algorithm
1.1 函数
类型 |
函数 |
说明 |
举例 |
最大值 |
Int/double = max( a,b) |
结果类型和a、b对应 (int/ double); <cmath>也相同 |
|
最小值 |
Int/double = min( a,b) |
|
|
绝对值 |
int = abs(int) |
取绝对值(整数) |
|
交换 |
swap(x, y) |
基本数据类型均可:char 、string |
string str1="abc", str2="def"; swap(str1, str2); |
逆转 |
reverse( it1, it2 ) |
数组、string |
int arr[4] = {1,2,3,4}; reverse(arr,arr+4); string str = "abc"; reverse(str.begin(),str.end()); |
赋值 |
fill( it1, it2, x) |
在[t1, t2) 都赋值x |
int arr[4]; fill(arr, arr+4, 0); |
排序 |
sort( it1, it2, cmp) |
cmp可省略,默认升序 |
|
排列 |
next_permutation(it1 it2) |
下一轮排列数 |
string str="123"; next_permutation(str.begin(),str.end()); printf("%s", str.c_str()); |
查找(>=x) |
* 或 it = lower_bound( it1, it2, x) |
在[t1, t2) 中,返回第一个 >= x的元素在的地方(指针 or 迭代器);若不存在,返回假设存在时应该在的地方 |
|
查找(>x) |
*或it = upper_bound( it1, it2, x) |
在[t1, t2) 中,返回第一个 > x的元素在的地方(指针 or 迭代器);若不存在,返回假设存在时应该在的地方 |
|
1.2 代码示例
#include<cstdio>
#include<algorithm>
#include<string>
#include<functional>
using namespace std;
struct Node{
int x,y;
Node(int _x, int _y):x(_x), y(_y){
}
Node(){
}
}nodes[3];
bool node_cmp(Node no1, Node no2){
if(no1.x!=no2.x){
return no1.x > no2.x;
}else{
return no1.y < no2.y;
}
}
int main(){
int a[10];
int b[5][10];
fill(a, a+10, 2);
fill(b[0], b[0]+5*10, 2);
string str